PHP—图像处理

一、创建一个图像应该完成如下所示的四个基本步骤
1.创建图像
imagecreatetruecolor()//新建一个真彩色图像
打开服务器或网络文件中已经存在的GIF,JPEG,PNG,WBMP格式图像

imagecreatefromjpeg()

imagecreatefrompng()

imagecreatefromgif()

imagecreatefromwbmp()

创建或者打开失败的时候会返回空字符串,并且输出一条错误信息。

imagesx()//输出画布宽度

imagesy()//输出画布高度

getimagesize()//取得图像大小

2.绘制图像
图像创建完成以后,就可以通过这个图像资源,使用各种画像函数设置图像的颜色、填充图像、画点、线段、以及向图像的添加文本等

1.imagecolorallocate()//分配颜色

2.imagefill()//区域填充

3.imagesetpixel()//画一个单一像素

4.imageline()//画一条线段

5.imagerectangle()//画一个矩形

6.imagestring()//水平地画一行字符串

7.imagettftext()//用 TrueType 字体向图像写入文本

8.imagettfbbox()//计算 TrueType 文字所占区域

9.imagecopy()//拷贝图像的一部分

10.imagecopymerge()//拷贝并合并图像的一部分

11.imagecopyresampled()//重采样拷贝部分图像并调整大小

3.输出图像
header('Content-type:image/jpeg');//输出图像为jpeg时
header函数注意点
在该函数之前,不能输出任何内容
在我们的PHP代码 的函数里面,我们使用的/开头的路径 这个/不是指 web根目录,而是操作系统的 文件的根目录!

4.释放资源
二、验证码
PS:验证码的目的是为了防止暴力注入

<?php
header('Content-type:image/jpeg');
$width=120;
$height=40;
$element=array('a','b','c','d','e','f','g','h','i','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$string='';
for ($i=0;$i<5;$i++){
	$string.=$element[rand(0,count($element)-1)];
}
$img=imagecreatetruecolor($width, $height);
$colorBg=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorBorder=imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$colorString=imagecolorallocate($img,rand(10,100),rand(10,100),rand(10,100));
imagefill($img,0,0,$colorBg);
imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);
for($i=0;$i<100;$i++){
	imagesetpixel($img,rand(0,$width-1),rand(0,$height-1),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
}
for($i=0;$i<3;$i++){
	imageline($img,rand(0,$width/2),rand(0,$height),rand($width/2,$width),rand(0,$height),imagecolorallocate($img,rand(100,200),rand(100,200),rand(100,200)));
}
//imagestring($img,5,0,0,'abcd',$colorString);
imagettftext($img,14,rand(-5,5),rand(5,15),rand(30,35),$colorString,'font/SketchyComic.ttf',$string);
imagejpeg($img);
imagedestroy($img);

三、水印

header('Content-type:text/html;charset:utf-8');//设置编码
header('Content-type:image/jpeg');//输出图像为jpeg时

$img=imagecreatefromjpeg('images/1.jpg');//打开images文件下的1.jpg图片(也可以打开浏览器上的图片,写图片地址)
$color=imagecolorallocate($img,255,255,255);//颜色

$width=imagesx($img);//取得图像宽度
$height=imagesy($img);//取得图像高度
$position=imagettfbbox(20,0,'font/Inkfree.TTF','xzq');
$stringWidth=$position[2]-$position[0];//水印宽度

imagettftext($img,20,0,$width-1-$stringWidth-($width/30),$height-1-($height/30), $color,'font/Inkfree.TTF','xzq');
//$width-1-$stringWidth-($width/30),$height-1-($height/30)把水印放在右下角
imagejpeg($img);//输出图像
imagedestroy($img);//释放资源

四、缩放与剪裁
imagecopyresampled()函数
采样某个图像资源的 某一部分 到 另外一个图像资源上面去

header('Content-type:image/jpeg');
$width=300;
$img=imagecreatefromjpeg('images/zcx.jpg');
$imgWidth=imagesx($img);
$imgHeight=imagesy($img);
$height=$width/($imgWidth/$imgHeight);
$img1=imagecreatetruecolor($width,$height);
imagecopyresampled($img1,$img,0,0,0,0,$width,$height,$imgWidth,$imgHeight);
if(imagejpeg($img1)){
	imagejpeg($img1,'images/zoom_zcx.jpg');
}
imagedestroy($img);
imagedestroy($img1);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值