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);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下