图像函数

PHP的GD库GD函数详解-获得信息

(2011-11-18 11:53:34)

本博客所有文章为本人原创,转载请注明出处:http://senir.cublog.cn。

<wbr></wbr>
如果您想从头看起,请点击基础篇。
一.gd_info<wbr> -<wbr>取得当前安装的GD库的信息</wbr></wbr>
1.格式:array gd_info(void)
2.解析:返回一个数组,包含了GD库的版本,支持的图像格式等信息,如:
<wbr></wbr>

<?php
var_dump(gd_info());
?>

//输出
array(9)<wbr><span style="color:#00cc">{</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"GD Version"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>string<span style="color:#00cc">(</span>24<span style="color:#00cc">)</span><wbr><span style="color:#ff0ff">"bundled (2.0compatible)"</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"FreeType Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>false<span style="color:#00cc">)</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"T1Lib Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>false<span style="color:#00cc">)</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"GIF Read Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>true<span style="color:#00cc">)</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"GIF Create Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>false<span style="color:#00cc">)</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"JPG Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>false<span style="color:#00cc">)</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"PNG Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>true<span style="color:#00cc">)</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"WBMP Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>true<span style="color:#00cc">)</span><br style="font-size:10pt"><wbr><wbr><span style="color:#00cc">[</span><span style="color:#ff0ff">"XBM Support"</span><span style="color:#00cc">]</span><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><wbr><wbr>bool<span style="color:#00cc">(</span>false<span style="color:#00cc">)</span><br style="font-size:10pt"><span style="color:#00cc">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>


["PNG Support"]=><wbr><wbr>bool<span style="color:#00cc">(</span>true<span style="color:#00cc">)<span style="color:#000000">说明当前的GD库支持PNG格式的图片,</span></span></wbr></wbr>

["JPG Support"]=><wbr><wbr>bool<span style="color:#00cc">(</span>false<span style="color:#00cc">)<span style="color:#000000">说明当前的GD库不支持JPG格式的图片。</span></span></wbr></wbr>

3.用法:通过对这个函数返回信息的一些判断,可以写出更通用的程序,如GD库不支持JPG时抛出一个错误:


$arr1_gd_info = gd_info();
if ( !$arr1_gd_info['JPG Support'])
{
<wbr><wbr><wbr><wbr>echo<span style="color:#ff0ff">'当前GD库不支持JPG格式的图片'</span><span style="color:#00cc">;</span><br><span style="color:#00cc">}</span></wbr></wbr></wbr></wbr>


二.getimagesize
<wbr>-<wbr> 取得图像大小<br> 1.格式:array <strong>getimagesize</strong> ( string filename[, array &amp;imageinfo] )<br> 2.解析:stringfilename表示要传入一个字符串格式的文件名,路径必须能访问到。第二个参数可以不用。如果函数执行成功,则返回一个含有四个单元的数组;如果执行失败,返回false。返回数组例:<br><table width="95%" bgcolor="#F1F1F1" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse"><tbody><tr style="font-size:10pt"> <td style="font-size:10pt"> <p style="font-size:10pt; line-height:19px; margin-top:5px; margin-right:5px; margin-bottom:5px; margin-left:5px"> <code><span style="color:#000000"><span style="color:#00cc">&lt;</span><span style="color:#00cc">?</span><span style="color:#00ff">php</span><br style="font-size:10pt"><span style="color:#ff00">print_r</span><span style="color:#00cc">(</span><span style="color:#ff00">getimagesize</span><span style="color:#00cc">(</span><span style="color:#ff0ff">'test.jpg'</span><span style="color:#00cc">)</span><span style="color:#00cc">)</span><span style="color:#00cc">;</span><br style="font-size:10pt"><span style="color:#00cc">?</span><span style="color:#00cc">&gt;</span><br style="font-size:10pt"><span style="color:#00cc">/</span><span style="color:#00cc">/</span>输出<br style="font-size:10pt"> Array<br style="font-size:10pt"><span style="color:#00cc">(</span><br style="font-size:10pt"><wbr><wbr><wbr><wbr><span style="color:#00cc">[</span>0<span style="color:#00cc">]</span><wbr><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><wbr>600<br style="font-size:10pt"><wbr><wbr><wbr><wbr><span style="color:#00cc">[</span>1<span style="color:#00cc">]</span><wbr><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><wbr>443<br style="font-size:10pt"><wbr><wbr><wbr><wbr><span style="color:#00cc">[</span>2<span style="color:#00cc">]</span><wbr><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><wbr>1<br style="font-size:10pt"><wbr><wbr><wbr><wbr><span style="color:#00cc">[</span>3<span style="color:#00cc">]</span><wbr><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><wbr><span style="color:#ff00">width</span><span style="color:#00cc">=</span><span style="color:#ff0ff">"600"</span><wbr><span style="color:#ff00">height</span><span style="color:#00cc">=</span><span style="color:#ff0ff">"443"</span><br style="font-size:10pt"><wbr><wbr><wbr><wbr><span style="color:#00cc">[</span>bits<span style="color:#00cc">]</span><wbr><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><wbr>8<br style="font-size:10pt"><wbr><wbr><wbr><wbr><span style="color:#00cc">[</span>channels<span style="color:#00cc">]</span><wbr><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><wbr>3<br style="font-size:10pt"><wbr><wbr><wbr><wbr><span style="color:#00cc">[</span>mime<span style="color:#00cc">]</span><wbr><span style="color:#00cc">=</span><span style="color:#00cc">&gt;</span><wbr>image<span style="color:#00cc">/</span>gif<br style="font-size:10pt"><span style="color:#00cc">)</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></code></p> <div><span style="font-family:monospace; color:#0000CC"><span style="line-height:19px"><br></span></span></div> </td> </tr></tbody></table> <br> 索引0给出的是图像的宽度的像素值;<br> 索引1给出的是图像的高度的像素值;<br> 索引2给出的是图像的类型,返回的是数字,其中1 = GIF,2 = JPG,3 = PNG,4 = SWF,5 = PSD,6 =BMP,7 = TIFF(intel byte order),8 = TIFF(motorola byte order),9 =JPC,10 = JP2,11 = JPX,12 = JB2,13 = SWC,14 = IFF,15 = WBMP,16 =XBM;<br> 索引3给出的是一个宽度和高度的字符串,可以直接用于HTML的&lt;image&gt;标签。<br> 索引bits给出的是图像的每种颜色的位数,二进制的;(低版本的GD可能不返回这个值)<br> 索引channels给出的是图像的通道值,RGB图像默认是3.(低版本的GD可能不返回这个值)<br> 索引mime给出的是图像的MIME信息,此信息可以用来在 HTTP Content-type 头信息中发送正确的信息,如: <table width="95%" bgcolor="#F1F1F1" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse"><tbody><tr> <td> <p style="margin:5px; line-height:150%"><code><span style="color:#000000">header<span style="color:#00cc">(</span><span style="color:#ff0ff">"Content-type:image/png"</span><span style="color:#00cc">)</span><span style="color:#00cc">;</span></span></code></p> </td> </tr></tbody></table> (低版本的GD可能不返回这个值),对于低版本的GD获得MIME,请使用下面的函数image_type_to_mime_type().<br> 用法:获得图像的宽度和高度<br><br><br><table width="95%" bgcolor="#F1F1F1" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse"><tbody><tr> <td> <p style="margin:5px; line-height:150%"><code><span style="color:#000000">$image_info <span style="color:#00cc">=</span> getimagesize<span style="color:#00cc">(</span><span style="color:#ff0ff">'test.jpg'</span><span style="color:#00cc">)</span><span style="color:#00cc">;</span><br> echo <span style="color:#ff0ff">'图像的宽度是:'</span><span style="color:#00cc">.</span><span style="color:#00cc">$</span>image_info<span style="color:#00cc">[</span>0<span style="color:#00cc">]</span><span style="color:#00cc">.</span><span style="color:#ff0ff">'px&lt;br&gt;'</span><span style="color:#00cc">;</span><br> echo <span style="color:#ff0ff">'图像的高度度是:'</span><span style="color:#00cc">.</span><span style="color:#00cc">$</span>image_info<span style="color:#00cc">[</span>1<span style="color:#00cc">]</span><span style="color:#00cc">.</span><span style="color:#ff0ff">'px&lt;br&gt;'</span><span style="color:#00cc">;</span></span></code></p> </td> </tr></tbody></table> <br> 三.<strong>image_type_to_mime_type</strong><wbr><wbr>-<wbr>取得图像的MIME类型<span style="font-size:16px; border-collapse:collapse"><br></span> <div>1.格式:<strong>image_type_to_mime_type</strong> ( intimagetype )</div> <div>2.解析:取得图像的MIME类型,传入的参数是getimagesize函数返回的数组中的索引2的值,如:</div> <div><span style="font-size:16px"><span style="font-size:12px"><br></span></span></div> <table width="95%" bgcolor="#F1F1F1" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse"><tbody><tr> <td> <p style="line-height:150%; margin:5px"><code><span style="color:#000000"><span style="color:#0000CC">&lt;</span><span style="color:#0000CC">?</span><span style="color:#0000FF">php</span><br><span style="color:#0000FF">$</span><span style="color:#008080">img_size</span> <span style="color:#0000CC"> =</span> <span style="color:#FF0000">getimagesize</span><span style="color:#0000CC">(</span><span style="color:#FF00FF">'gd_image/gif.gif'</span><span style="color:#0000CC">)</span><span style="color:#0000CC">;</span><br><span style="color:#0000FF">$</span><span style="color:#008080">mime_type</span> <span style="color:#0000CC"> =</span> <span style="color:#FF0000">image_type_to_mime_type</span><span style="color:#0000CC">(</span><span style="color:#0000FF">$</span><span style="color:#008080">img_size</span><span style="color:#0000CC">[</span>2<span style="color:#0000CC">]</span><span style="color:#0000CC">)</span><span style="color:#0000CC">;</span><br><span style="color:#FF0000">echo</span> <span style="color:#0000FF">$</span><span style="color:#008080">mime_type</span><span style="color:#0000CC">;</span><br><span style="color:#0000CC">?</span><span style="color:#0000CC">&gt;</span><br><span style="color:#0000CC">/</span><span style="color:#0000CC">/</span>输出<br> image<span style="color:#0000CC">/</span>gif</span></code></p> </td> </tr></tbody></table> <br><div>四.imagesx - 取得图像的宽度像素值</div> <div><span style="font-size:16px"><span style="font-size:12px">1.格式:<strong>imagesx</strong> ( resource image )</span></span></div> <div><span style="font-size:16px"><span style="font-size:12px">2.解析:取得图像的宽度像素值,传入的参数是一个资源类型的变量,通过imagecreate,imagecreatetruecolor,imagecreatefromgif等函数的返回值。如:</span></span></div> <div><span style="font-size:16px"><span style="font-size:12px"><br></span></span></div> <table width="95%" bgcolor="#F1F1F1" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse"><tbody><tr> <td> <p style="line-height:150%; margin:5px"><code><span style="color:#000000"><span style="color:#0000CC">&lt;</span><span style="color:#0000CC">?</span><span style="color:#0000FF">php</span><br><span style="color:#0000FF">$</span><span style="color:#008080">im_gif</span> <span style="color:#0000CC"> =</span> <span style="color:#FF0000">imagecreatefromgif</span><span style="color:#0000CC">(</span><span style="color:#FF00FF">'gd_image/gif.gif'</span><span style="color:#0000CC">)</span><span style="color:#0000CC">;</span><br><span style="color:#0000FF">$</span><span style="color:#008080">x</span> <span style="color:#0000CC"> =</span><span style="color:#FF0000">imagesx</span><span style="color:#0000CC">(</span><span style="color:#0000FF">$</span><span style="color:#008080">im_gif</span><span style="color:#0000CC">)</span><span style="color:#0000CC">;</span><br><span style="color:#FF0000">echo</span> <span style="color:#FF00FF">'图片的宽度是 :'</span><span style="color:#0000CC">.</span><span style="color:#0000FF">$</span><span style="color:#008080">x</span><span style="color:#0000CC">.</span><span style="color:#FF00FF">'px&lt;br&gt;'</span><span style="color:#0000CC">;</span><br><span style="color:#0000CC">?</span><span style="color:#0000CC">&gt;</span><br><span style="color:#0000CC">/</span><span style="color:#0000CC">/</span>输出<br> 图片的宽度是 <span style="color:#0000CC">:</span>600px</span></code></p> </td> </tr></tbody></table> 这个函数只能在创建画布之后调用,不能通过传入一个图片的文件名来调用。 <div><br></div> <div><span style="font-size:16px"><span style="font-size:12px">五.imagesy -取得图像的高度像素值</span></span></div> <div><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px">1.格式:<strong>imagesy</strong><wbr>( resource image)</wbr></span></span></span></span></div> <div><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px">2.解析:基本同imagesx,只不过是获得的图像的高度值</span></span></span></span></span></span></div> <div><br></div> <div><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px">六.imagetypes -<wbr>返回当前 PHP版本所支持的图像类型</wbr></span></span></span></span></span></span></span></span></div> <div><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px">1.格式:<strong>imagetypes</strong> ( void)</span></span></span></span></span></span></span></span></span></span></div> <div><font size="3"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px"><span style="font-size:16px"><span style="font-size:12px">2.解析:返回的是PHP版本所支持的图像类型,是一个数字,如:</span></span></span></span></span></span></span></span></span></span></span></font></div> <div><br></div> <table width="95%" bgcolor="#F1F1F1" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse"><tbody><tr> <td> <p style="line-height:150%; margin:5px"><code><span style="color:#000000"><span style="color:#0000CC">&lt;</span><span style="color:#0000CC">?</span><span style="color:#0000FF">php</span><br><span style="color:#FF0000">echo</span> <span style="color:#FF0000">imagetypes</span><span style="color:#0000CC">(</span><span style="color:#0000CC">)</span><span style="color:#0000CC">;</span><br><span style="color:#0000CC">?</span><span style="color:#0000CC">&gt;</span><br><span style="color:#0000CC">/</span><span style="color:#0000CC">/</span>输出<br> 15</span></code></p> </td> </tr></tbody></table> 3.用法:判断是否支持JPG <div> <div><br></div> </div> <table width="95%" bgcolor="#F1F1F1" border="1" cellpadding="0" cellspacing="0" style="border-collapse:collapse"><tbody><tr> <td> <p style="line-height:150%; margin:5px"><code><span style="color:#000000"><span style="color:#0000CC">&lt;</span><span style="color:#0000CC">?</span><span style="color:#0000FF">php</span><br><span style="color:#0000FF">if</span> <span style="color:#0000CC">(</span><span style="color:#FF0000">imagetypes</span><span style="color:#0000CC">(</span><span style="color:#0000CC">)</span><span style="color:#0000CC">&amp;</span> <span style="color:#0000FF">IMG_PNG</span><span style="color:#0000CC">)</span><br><span style="color:#0000CC">{</span><br><wbr><wbr><wbr><wbr><span style="color:#FF0000">echo</span><span style="color:#FF00FF">"PNG Support isenabled"</span><span style="color:#0000CC">;</span><br><span style="color:#0000CC">}</span><br><span style="color:#0000FF">else</span><br><span style="color:#0000CC">{</span><br><wbr><wbr><wbr><wbr><span style="color:#FF0000">echo</span> <span style="color:#FF00FF"> "PNG Support is not enabled"</span><span style="color:#0000CC">;</span><br><span style="color:#0000CC">}</span><br><span style="color:#0000CC">?</span><span style="color:#0000CC">&gt;</span><br><span style="color:#0000CC">/</span><span style="color:#0000CC">/</span>输出<br> PNG Support is enabled</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span></code></p> </td> </tr></tbody></table></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(函数)