PHP中图像的常用处理函数
在PHP中可以通过GD扩展库实现对图像的处理,不仅可以创建新图像,而且可以处理已有图像
声明该文件为图像文件:header('Content-Type:image/png');(gif,jpeg,png,wbmp)
1、创建新的图像
imagecreatetruecolor (int $width,int $height)
返回一个图像标识符,代表了一幅大小为$width乘以$height的黑色图像
imagecreate (int $width,int $height)
返回一个图像标识符,代表了一幅大小为$width乘以$height的空白图像
第一次调用imagecolorallocate()该颜色自动填充为背景色
2、打开一张图像
imagecreatefromjpeg($filename)
imagecreatefrompng($filename)
imagecreatefromgif($filename)
imagecreatefromwbmp($filename)
3、获取图像信息
imagesx(resource $image):获取画布的宽
imagesy(resource $image):获取画布的高
getimagesize(resource $image):获取图像的大小
4、绘制图像
imagecolorallocate (resource $image,int $red,int $green,int $blue)//分配颜色
imagefill (resource $image,int $x ,int $y,int $color)//区域填充
imagesetpixel (resource $image,int $x,int $y,int $color)//画一个单一像素(一个点)
imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )//画一条线段
用color颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段
imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )//画一个矩形
用color颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2
imagestring ( resource $image,int $font,int $x,int $y,string $s,int $color)//水平地画一行字符串
用color颜色将字符串 s 画到图像的x,y坐标处(这是字符串左上角坐标)
如果font是 1,2,3,4 或 5,则使用内置字体
imagefttext (resource $image,float $size,float $angle,int $x ,int $y,int $color,string $fontfile,string $text)
用 TrueType 字体向图像写入文本
size:字体的尺寸。根据GD的版本,为像素尺寸(GD1)或点(磅)尺寸(GD2)。
angle:角度制表示的角度,0 度为从左向右读的文本。更高数值表示逆时针旋转
x,y:由 x,y所表示的坐标定义了第一个字符的基本点(大概是字符的左下角)。Y坐标设定了字体基线的位置,不是字符的最底端。
fontfile:是想要使用的 TrueType 字体的路径
text:写入的UTF-8编码的文本字符串。如果字符串中使用的某个字符不被字体支持,一个空心矩形将替换该字符
imagettfbbox ( float $size , float $angle , string $fontfile , string $text )
计算 TrueType 文字所占区域
imagecopy (resource $dst_im,resource $src_im,int $dst_x ,int $dst_y ,int $src_x, int $src_y, int $src_w, int $src_h)
拷贝图像的一部分
将图像中坐标从src_x,src_y开始,宽度为src_w,高度为src_h的一部分拷贝到图像中坐标为dst_x 和 dst_y 的位置上
imagecopymerge (resource $dst_im,resource $src_im,int $dst_x,int $dst_y,int $src_x,int $src_y,int $src_w,int $src_h,int $pct )
拷贝并合并图像的一部分
将根据pct来决定合并程度,其值范围从 0 到 100
为0时,实际上什么也没做(透明度100%)
为100时和imagecopy()效果完全一样
imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )
重采样拷贝部分图像并调整大小
将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值
因此,减小了图像的大小而仍然保持了极大的清晰度
如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸
imagerotate(resource $dst_image , float $angle, int $bgd_color)
把图片逆时针旋转$angle角度,旋转后的空白处用$bgd_color填充
该函数不会修改$dst_image,会返回一个修改后的$dst_image资源
三、输出图像:
imagegif(resource $image[,string $filename])
imagejpeg (resource $image[,string $filename[,int $quality]])
imagepng(resource $image[,string $filename])
imagewbmp(resource $image[,string $filename])
filename:文件保存的路径和文件名,如果未设置或为 NULL,将会直接输出原始图象流
quality 为可选项,范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大)。默认为 大约 75。
四、释放资源:
imagedestroy( resource $image )
五、图片的base64编码:
使用php对图片进行base64编码:
$data=file_get_contents("2.jpg");
$img_base64_str=base64_encode($data);
$img_base64='data:image/jpg;base64,'.$img_base64_str;
实际使用时,需要添加头信息
使用php输出base64编码的图片
header("Content-Type:image/jpg");
$i=base64_decode($img_base64_str);
echo $i;直接使用echo输出即可
file_put_contents('./test.jpg', $i); 保存图片到指定的目录,该函数返回图片大小的字节数