Home  >  Article  >  Backend Development  >  PHP脚本的10个技巧(4)

PHP脚本的10个技巧(4)

WBOY
WBOYOriginal
2016-06-01 14:30:06819browse
动态创建图象
在安装了某些第三方函数库之后,结合你的图形处理技能,你就可以用php创建和处理图像了。事实上,你也不需要太高的几何学知识。我在中学的时候这门功课总是不及格,现在不也照样会用PHP创建图像!

在使用基本的图像创建函数之前,你需要安装GD库。如果要用到和JPEG相关的图像创建函数你还需要安装jpeg-6b。在图像中使用Type 1字体的时候还必须安装t1lib。

在这里,你还需要对你的系统进行进一步地调整。首先,你必须安装t1lib以提供图象处理支持,接下来要安装jpeg-6b。第三步是安装GD函数库。你得按顺序做完这三件工作,原因是你需要编译GD库才能使用jpeg-6b库,如果jpeg-6b步首先安装,编译就会出错,到那时候你就是忙的团团转也没办法了。

在安装完以上的三个函数库之后,你还要重新配置PHP。这可是你在安装PHP的DSO版本时的拿手好戏噢!接着执行make clean,命令,然后在当前配置指示符里加入以下代码:

--with-gd=[/path/to/gd]
--with-jpeg-dir=[/path/to/jpeg-6b]
--with-t1lib=[/path/to/t1lib]

最后顺序执行make、make install命令完成配制任务。重新启动 Apache,运行phpinfo()函数检查性新功能是否正常运行。

和你安装的GD库有关,你可能或者不可能具有创建GIF或者PNG图像的能力。关键在于:如果你已经安装了gd-1.6或者早期版本,那么你可以处理GIF但不能处理PNG。如果安装了gd-1.6或者以后版本,你可以处理PNG但又不能处理GIF。

创建一个简单的图像需要采用好几个函数。我会按步骤带你学习这一过程:

输出一个文件头,其中包含了你所创建图像的MIME类型,在我们的例子中就是PNG。

header ("Content-type: image/png");

使用ImageCreate()创建一个变量存放空白图像。该函数需要以像素为单位的图像大小。格式是ImageCreate(x_size, y_size),对250-X-250像素的图像而言,用法如下:

$newImg = ImageCreate(250,250);

因为你的图像现在还是空白,所以你还要设法用某些色彩填满它,但是,首先你需要按照颜色的RGB值为每种颜色分配名字,这要用到ImageColorAllocate()函数。函数的格式是ImageColorAllocate([image], [red], [green], [blue])。如果是天蓝色,具体代码如下:

$skyblue = ImageColorAllocate($newImg,136,193,255);

接着,你需要调用ImageFill()函数为图像填充以上的颜色。ImageFill(),函数有好几个版本,比如ImageFillRectangle(), ImageFillPolygon()等等。为简单起见,我们就采用ImageFill()函数进行颜色填充,格式如下:

ImageFill([image], [start x point], [start y point], [color])
ImageFill($newImg,0,0,$skyblue);

最后,你创建了图像并破坏图像流以释放内存:

ImagePNG($newImg);
ImageDestroy($newImg); ?>

具体的代码看起来很像下面的样子:

header ("Content-type: image/png");
$newImg = ImageCreate(250,250);
$skyblue = ImageColorAllocate($newImg,136,193,255);
ImageFill($newImg,0,0,$skyblue);
ImagePNG($newImg);
ImageDestroy($newImg);
?>

如果你调用这个脚本skyblue.php 并用自己的浏览器访问它,你就会看到一个250-X-250像素大的蓝色PNG图像。

你还可以用图像创建函数处理图像,比如创建大型图像的缩微图等。

假设你打算为某个图片制作一个35-X-35像素大小的缩微图。你要做到就是创建一个新的35 X 35 像素大小的图像;制造出一个包含其原始图像内容的图像流;然后改变原始图像的大小,并把它放到新的空白图像中去。

用来达到以上目的的关键函数就是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]);

以下是代码注释。

/* send a header so that the browser knows the content-type of the file */
header("Content-type: image/png");

/* set up variables to hold the height and width of your new image */
$newWidth = 35;
$newHeight = 35;

/* create a blank, new image of the given new height and width */
$newImg = ImageCreate($newWidth,$newHeight);

/* get the data from the original, large image */
$origImg = ImageCreateFromPNG("test.png");

/* copy the resized image. Use the ImageSX() and ImageSY functions to get the x and y sizes of the orginal image. */
ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));

/* create final image and free up the memory */
ImagePNG($newImg);
ImageDestroy($newImg); ?>

如果你调用了以上脚本resized.php 并用自己的浏览器访问它,你应该能看到一个35-X-35像素大小的缩微PNG图。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP4 与 MySQL 交互使用Next article:聊天室php&mysql(四)