通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
[TOC] >[success]PHP很强大可以轻松快捷的处理多种数据格式,在图像处理上也拥有 强大而且简单的实现方式 PHP处理图像需要扩展库GD库的支持 可以通过PHPINFO()函数查看GD库是否开启,如果没有开启在 PHP.INI中开启 检测扩展库是否加载: ~~~ extension_loaded("GD") ~~~ ## 图像处理步骤 1. 发送HTTP头文件,声明内容为图像 2. 创建画布 3. 创建绘图所需要的颜色 4. 绘图(填充画布、画圆、画方块、画线条、画布上写字) 5. 输出图像 6. 释放图像资源 ### 发送HTTP头文件 发送图像文件给浏览器,我们首先需要告诉Web浏览器我们输出的是一 个图像而不是文本或HTML。这可以通过调用header()函数指定图像的 MIME类型输出完成。 ~~~ <?php header("Content-type: image/gif"); header("Content-type: image/gif"); header("Content-type: image/jpeg"); header("Content-type: image/png"); ?> ~~~ ### 创建画布 ~~~ <?php $src = imageCreateTrueColor(200,100); ?> ~~~ 传入的两个参数分别为画布的宽和高,在绘图时超出宽高的部分将 不予显示,且此尺寸即为生成图片文件时的尺寸。返回值为资源类型。 ### 设置颜色 ~~~ <?php $color = imageColorAllocate($src,255,255,255); ?> ~~~ 颜色从属于某个图像资源而存在。颜色实际上是一个整形数值。颜色的后三个参数需传入值的范围是0~255,返回布尔值。 ### 填充颜色 ~~~ <?php imageFill($src,0,0,$color); ?> ~~~ ### 绘制空心矩形 ~~~ bool imagerectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $col ) ~~~ imagerectangle() 用 col 颜色在 image 图像中画一个矩形,其左上角坐标为 x1, y1,右下角坐标为 x2, y2。图像的左上角坐标为 0, 0。 ### 绘制实心矩形 ~~~ bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) ~~~ imagefilledrectangle() 在 image 图像中画一个用 color 颜色填充了的矩形,其左上角坐标为 x1,y1,右下角坐标为 x2,y2。0, 0 是图像的最左上角。 ### 绘制空心圆形 ~~~ bool imageellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color ) ~~~ 在指定的坐标上画一个椭圆。 ### 绘制实心圆 ~~~ bool imagefilledellipse ( resource $image , int $cx , int $cy , int $width , int $height , int $color ) ~~~ 画一椭圆并填充到指定的 image。 ### 绘制线条 ~~~ bool imageline ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color ) ~~~ imageline() 用 color 颜色在图像 image 中从坐标 x1,y1 到 x2,y2(图像左上角为 0, 0)画一条线段 ### 绘制单一像素 ~~~ bool imagesetpixel ( resource $image , int $x , int $y , int $color ) ~~~ imagesetpixel() 在 image 图像中用 color 颜色在 x,y 坐标(图像左上角为 0,0)上画一个点。 ### 用特殊字体输入文本 ~~~ array imagettftext ( resource $image , float $size , float $angle , int $x , int $y , int $color , string $fontfile , string $text ) ~~~ 使用 TrueType 字体将 指定的 text 写入图像。 ### 输出图像 ~~~ <?php //输出不同格式的图像用不同的方法: imagegif(img_resource[,filename]); imagejpeg(img_resource[,filename]); imagepng(img_resource[,filename]); imagebmp(img_resource[,filename]); //第二个可选参数为文件名时,文件被另存同名会覆盖 ?> ~~~ ### 释放图像资源 ~~~ <?php imageDestroy(img_resource) //图像输出完毕及时释放资源,把内存空间留给更需要的程序。 ?> ~~~ ## 操作已有图像 通过我们需要对图片进行剪切,加水印,缩放等。 ### 打开外部图像 ~~~ <?php imageCreateFromgd(filename/url); imageCreateFromgif(filename/url); imageCreateFromjpeg(filename/url); imageCreateFrompng(filename/url); imageCreateFrombmp(filename/url); //返回一个资源类型 ?> ~~~ ### 获得图像信息 ~~~ <?php imagesx(img_resource)//取得图像宽度 imagesy(img_resource)//取得图像高度 getimagesize(img_file)//取得图像详细信息,数组形式 ?> ~~~ ### 拷贝图像的一部分 ~~~ bool 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 的位置上。 ### 拷贝并合并图像的一部分 ~~~ bool 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 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。 ### 拷贝部分图像并调整大小 ~~~ bool imagecopyresized ( 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 ) ~~~ imagecopyresized() 将一幅图像中的一块矩形区域拷贝到另一个图像中。dst_image 和 src_image 分别是目标图像和源图像的标识符。 >[danger] 练习:创建网站验证码类。