在PHP中可以通过GD扩展库实现对图像的处理,不仅可以创建新图像,而且可以处理已有图像。
要在php中声明该文件为图像文件:header('Content-Type:image/png');(gif,jpeg,png,wbmp)
一、创新图像:
1、创建新的图像:
imagecreatetruecolor (int $width,int $height)
返回一个图像标识符,代表了一幅大小为$width乘以$height的黑色图像。
填充背景色需要用imagefill()来填充。
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):获取图像的大小
二、绘制图像:
1.imagecolorallocate (resource $image,int $red,int $green,int $blue)//分配颜色
2.imagefill (resource $image,int $x ,int $y,int $color)//区域填充
3.imagesetpixel (resource $image,int $x,int $y,int $color)//画一个单一像素(一个点)
4.imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )//画一条线段
用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段。
5.imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col )//画一个矩形
用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。
6.imagestring ( resource $image,int $font,int $x,int $y,string $s,int $col)//水平地画一行字符串
用 col 颜色将字符串 s 画到 image 所代表的图像的 x,y 坐标处(这是字符串左上角坐标)。如果 font 是 1,2,3,4 或 5,则使用内置字体。
7.imagettftext (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编码的文本字符串。如果字符串中使用的某个字符不被字体支持,一个空心矩形将替换该字符。
8.imagettfbbox ( float $size , float $angle , string $fontfile , string $text )//计算 TrueType 文字所占区域
size 像素单位的字体大小。
angle text 将被度量的角度大小。
fontfile TrueType 字体文件的文件名(可以是 URL)。
text 要度量的字符串。
9.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_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。
10.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 )//拷贝并合并图像的一部分
将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做(透明度100%),当为 100 时对于调色板图像本函数和 imagecopy()完全一样,它对真彩色图像实现了 alpha 透明。
11.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 )//重采样拷贝部分图像并调整大小
imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,减小了图像的大小而仍然保持了极大的清晰度。 如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。
三、输出图像:
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 参数,使用NULL。
quality 为可选项,范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大)。默认为 大约 75。
四、释放资源:
imagedestroy( resource $image )
五、图片的base64编码:
使用php对图片进行base64编码:
1、$data=file_get_contents("2.jpg");使用file_get_contents函数读取图片
2、$img_base64_str=base64_encode($data);
3、$img_base64='data:image/jpg;base64,'.$img_base64_str;实际使用时,需要添加头信息
使用php输出base64编码的图片
1、header("Content-Type:image/jpg");指定输出的文件类型
2、$i=base64_decode($img_base64_str);解码base64编码
3、echo $i;直接使用echo输出即可
4、file_put_contents('./test.jpg', $i); 保存图片到指定的目录,该函数返回图片大小的字节数。
最后编辑:2019-04-03作者:qingheluo
这个作者貌似有点懒,什么都没有留下。