gd库
PHP图像处理(图像的生成)
步骤共七步
1.检查php.ini是否加载了GD库
如果没有,网上下载,将gd2.dll放进extension_dir,然后再配置php.ini
2.设置php文件的输出形式(设置html的头)
(1).<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />其中content属性的值决定了网页的输出形式
例如:content="text/html":表示网页的内容以文本的形式输出
content="application/pdf":以pdf的形式输出
content="image/gif"(image/jpg | image/png | image/wbmp)
:以图片的形式(位图、矢量图)
(2).header()(属于php的函数)
header("Content-type: image/png");
3.创建绘图区域(imagecreate | imagecreatetruecolor)
4.调制颜色(imagecolorallocate())
5.绘制相应的图形(画点,划线,画方形,画椭圆......及图像的填充)再绘图区域填写字符串
6.图像的输出imagepng()| imagegif | imagejpg |imagewbmp
7.利用imagedestroy()销毁图像释放内存
这个利用gd库生成一个笑脸我感觉挺有意思的,所以分享一下。
<?php
$arr_points = array(10,10,50,10,50,30,100,300,50,100,10,10);
$im = imagecreatetruecolor(300,300);//创建绘图区域
$bk = imagecolorallocate( $im,200,255,255);//调制蓝色
$red = imagecolorallocate($im,0xff,0x00,0);//调制红色
$green = imagecolorallocate($im,0,0xff,0);
imagefill($im,100,100,$bk);//填充颜色
imagefilledellipse($im,150,150,200,200,$red);
imagearc($im,100,130,50,50,200,340,$green);
imagearc($im,200,130,50,50,200,340,$green);
imagearc($im,150,150,100,100,30,150,$green);
header('Content-type:image/jpeg');//声明网页的输出形式
imagejpeg($im);//输出图像
imagedestroy($im);//销毁图像释放内存
?>