第九章 图像处理
php中可以通过gd扩展库进行处理图像
创建图像
新建一个长和高都为100像素的真彩图像
$img=imagecreatetruecolor(200,200);
绘制图像
1.分配颜色函数 imagecolorallocate()
$color1=imagecolorallocate($img,50,50,50);
$color2=imagecolorallocate($img,299,36,36);
2.区域填充 imagefill()
imagefill($img,0,0,$color1);
3.画单一像素imagesetpixel()
4.imageline()画一条线
5.imagerectangle()画一个矩形
6.imagestring()水平画一行字符串
7.imagettftext()用turetype字体向图像写入文本
8.imagettfbbox()
9.imagecopy()
10.imagecopymerge()
11.imagecopyressampled()
输出图像
在php代码的函数里面,我们使用的/开头的路径这个/不是指web根目录,而是操作系统文件的根目录
header()
在该函数之前不能输出任何内容
imagejpeg()
header('Content-type: image/png');
imagepng($img);
//加名字变为保存图片
header('Content-type: text/html');
imagepng($img,'test.png');
释放资源
imagedestroy($img);
图形验证码
header('Content-type:image/jpeg');
$width=120;
$height=40;
$img=imagecreatetruecolor($width,$height);
$colorBG=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorReg=imagecolorallocate($img,187,255,170);
$colorPoint=imagecolorallocate($img,0,0,0);
$colorLine=imagecolorallocate($img,rand(150,255),rand(150,255),rand(150,255));
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
imagefill($img,0,0,$colorBG);
imagerectangle($img,1,1,119,39,$colorReg);
for($i=0;$i<100;$i++)
{
imagesetpixel($img,rand(10,110),rand(1,39),$colorPoint);
}
for($i=0;$i<5;$i++)
{
imageline($img,rand(0,50),rand(0,40),rand(60,120),rand(0,40),$colorLine);
}
//imagestring($img,14,20,20,'90849',$colorString);
imagettftext($img,20,0,rand(2,12),rand(20,30),$colorString,'Candara','90849');
imagejpeg($img);
imagedestroy($img);
水印
缩放与裁剪
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-3.2.29-81.el6.centos.noarch.rpm
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.30-41.el6.noarch.rpm
wget http://mirrors.163.com/centos/6/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm