«
PHP脚本的8个技巧(4)动态创建图象

时间:2008-5-31    作者:Deri    分类: 分享


   <p>  在安装了某些第三方函数库之后,结合你的图形处理技能,你就可以用PHP创建和处理图像了。事实上,你也不需要太高的几何学知识。我在中学的时候这门功课总是不及格,现在不也照样会用PHP创建图像!在使用基本的图像创建函数之前,你需要安装GD库。如果要用到和JPEG相关的图像创建函数你还需要安装jpeg-6b。在图像中使用Type 1字体的时候还必须安装t1lib。</p><p>  在这里,你还需要对你的系统进行进一步地调整。首先,你必须安装t1lib以提供图象处理支持,接下来要安装jpeg-6b。第三步是安装GD函数库。你得按顺序做完这三件工作,原因是你需要编译GD库才能使用jpeg-6b库,如果jpeg-6b步首先安装,编译就会出错,到那时候你就是忙的团团转也没办法了。</p><p>  在安装完以上的三个函数库之后,你还要重新配置PHP。这可是你在安装PHP的DSO版本时的拿手好戏噢!接着执行make clean,命令,然后在当前配置指示符里加入以下代码:</p><code>--with-gd=[/path/to/gd]<br />--with-jpeg-dir=[/path/to/jpeg-6b]<br />--with-t1lib=[/path/to/t1lib]</code></p><p>  最后顺序执行make、make install命令完成配制任务。重新启动 Apache,运行phpinfo()函数检查性新功能是否正常运行。</p><p>  和你安装的GD库有关,你可能或者不可能具有创建GIF或者PNG图像的能力。关键在于:如果你已经安装了gd-1.6或者早期版本,那么你可以处理GIF但不能处理PNG。如果安装了gd-1.6或者以后版本,你可以处理PNG但又不能处理GIF。</p><p>  创建一个简单的图像需要采用好几个函数。我会按步骤带你学习这一过程:</p><p>  输出一个文件头,其中包含了你所创建图像的MIME类型,在我们的例子中就是PNG。</p><p>  <? header ("Content-type: image/png");</p>
<p> </p>

   <p>  使用ImageCreate()创建一个变量存放空白图像。该函数需要以像素为单位的图像大小。格式是ImageCreate(x_size, y_size),对250-X-250像素的图像而言,用法如下:</p><p>  $newImg = ImageCreate(250,250);</p><p>  因为你的图像现在还是空白,所以你还要设法用某些色彩填满它,但是,首先你需要按照颜色的RGB值为每种颜色分配名字,这要用到ImageColorAllocate()函数。函数的格式是ImageColorAllocate([image], [red], [green], [blue])。如果是天蓝色,具体代码如下:</p><p>  $skyblue = ImageColorAllocate($newImg,136,193,255);</p><p>  接着,你需要调用ImageFill()函数为图像填充以上的颜色。ImageFill(),函数有好几个版本,比如ImageFillRectangle(), ImageFillPolygon()等等。为简单起见,我们就采用ImageFill()函数进行颜色填充,格式如下:</p><code>ImageFill([image], [start x point], [start y point], [color])<br />ImageFill($newImg,0,0,$skyblue);</code></p><p>  最后,你创建了图像并破坏图像流以释放内存:</p><code>ImagePNG($newImg);<br />ImageDestroy($newImg); ?></code></p><p>  具体的代码看起来很像下面的样子:</p><code><? header ("Content-type: image/png");<br />$newImg = ImageCreate(250,250);<br />$skyblue = ImageColorAllocate($newImg,136,193,255);<br />ImageFill($newImg,0,0,$skyblue);<br />ImagePNG($newImg);<br />ImageDestroy($newImg);<br />?></code></p><p>  如果你调用这个脚本skyblue.php 并用自己的浏览器访问它,你就会看到一个250-X-250像素大的蓝色PNG图像。</p><p>  你还可以用图像创建函数处理图像,比如创建大型图像的缩微图等。</p><p>  假设你打算为某个图片制作一个35-X-35像素大小的缩微图。你要做到就是创建一个新的35 X 35 像素大小的图像;制造出一个包含其原始图像内容的图像流;然后改变原始图像的大小,并把它放到新的空白图像中去。</p></p><p>  用来达到以上目的的关键函数就是ImageCopyResized(),,该函数的格式如下所示:ImageCopyResized([new image handle],[original image handle],[new image X], [new Image Y], [original image X], [original image Y], [new image X], [new image Y], [original image X], [original image Y]);</p><p>  以下是代码注释。</p><code><? /* send a header so that the browser knows the content-type of the file */<br />header("Content-type: image/png");<br />/* set up variables to hold the height and width of your new image */<br />$newWidth = 35;<br />$newHeight = 35;<br />/* create a blank, new image of the given new height and width */<br />$newImg = ImageCreate($newWidth,$newHeight);<br />/* get the data from the original, large image */<br />$origImg = ImageCreateFromPNG("test.png");<br />/* copy the resized image. Use the ImageSX() and ImageSY functions to get the x and y sizes of the orginal image. */<br />ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));<br />/* create final image and free up the memory */<br />ImagePNG($newImg);<br />ImageDestroy($newImg); ?></code></p><p>  如果你调用了以上脚本resized.php 并用自己的浏览器访问它,你应该能看到一个35-X-35像素大小的缩微PNG图。</p></p>