企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
要激活 GD 支持,配置 PHP 时加上**\--with-gd\[=DIR\]**,DIR 是 GD 的基本安装目录。要使用推荐的绑定的 GD 库版本(首次绑定于 PHP 4.3.0),使用**\--with-gd**。要编译 GD 库,需要libpng和libjpeg。 windows启用php_gd2.dll 要想在 PHP 中使用图像处理功能,你需要连带GD库一起来编译 PHP。GD库和 PHP 可能需要其他的库, 这取决于你要处理的图像格式 * [范例](https://www.php.net/manual/zh/image.examples.php) * [使用 PHP 创建 PNG 图像](https://www.php.net/manual/zh/image.examples-png.php) * [使用 Alpha 通道为图像加水印](https://www.php.net/manual/zh/image.examples-watermark.php) * [使用 imagecopymerge 函数创建半透明水印](https://www.php.net/manual/zh/image.examples.merged-watermark.php) * [GD 和图像处理 函数](https://www.php.net/manual/zh/ref.image.php) * [gd\_info](https://www.php.net/manual/zh/function.gd-info.php)— 取得当前安装的 GD 库的信息 * [getimagesize](https://www.php.net/manual/zh/function.getimagesize.php)— 取得图像大小 * [getimagesizefromstring](https://www.php.net/manual/zh/function.getimagesizefromstring.php)— 从字符串中获取图像尺寸信息 * [image\_type\_to\_extension](https://www.php.net/manual/zh/function.image-type-to-extension.php)— 取得图像类型的文件后缀 * [image\_type\_to\_mime\_type](https://www.php.net/manual/zh/function.image-type-to-mime-type.php)— 取得 getimagesize,exif\_read\_data,exif\_thumbnail,exif\_imagetype 所返回的图像类型的 MIME 类型 * [image2wbmp](https://www.php.net/manual/zh/function.image2wbmp.php)— 以 WBMP 格式将图像输出到浏览器或文件 * [imageaffine](https://www.php.net/manual/zh/function.imageaffine.php)— 返回经过仿射变换后的图像,剪切区域可选 * [imageaffinematrixconcat](https://www.php.net/manual/zh/function.imageaffinematrixconcat.php)— Concatenate two affine transformation matrices * [imageaffinematrixget](https://www.php.net/manual/zh/function.imageaffinematrixget.php)— Get an affine transformation matrix * [imagealphablending](https://www.php.net/manual/zh/function.imagealphablending.php)— 设定图像的混色模式 * [imageantialias](https://www.php.net/manual/zh/function.imageantialias.php)— 是否使用抗锯齿(antialias)功能 * [imagearc](https://www.php.net/manual/zh/function.imagearc.php)— 画椭圆弧 * [imagebmp](https://www.php.net/manual/zh/function.imagebmp.php)— Output a BMP image to browser or file * [imagechar](https://www.php.net/manual/zh/function.imagechar.php)— 水平地画一个字符 * [imagecharup](https://www.php.net/manual/zh/function.imagecharup.php)— 垂直地画一个字符 * [imagecolorallocate](https://www.php.net/manual/zh/function.imagecolorallocate.php)— 为一幅图像分配颜色 * [imagecolorallocatealpha](https://www.php.net/manual/zh/function.imagecolorallocatealpha.php)— 为一幅图像分配颜色 + alpha * [imagecolorat](https://www.php.net/manual/zh/function.imagecolorat.php)— 取得某像素的颜色索引值 * [imagecolorclosest](https://www.php.net/manual/zh/function.imagecolorclosest.php)— 取得与指定的颜色最接近的颜色的索引值 * [imagecolorclosestalpha](https://www.php.net/manual/zh/function.imagecolorclosestalpha.php)— 取得与指定的颜色加透明度最接近的颜色 * [imagecolorclosesthwb](https://www.php.net/manual/zh/function.imagecolorclosesthwb.php)— 取得与给定颜色最接近的色度的黑白色的索引 * [imagecolordeallocate](https://www.php.net/manual/zh/function.imagecolordeallocate.php)— 取消图像颜色的分配 * [imagecolorexact](https://www.php.net/manual/zh/function.imagecolorexact.php)— 取得指定颜色的索引值 * [imagecolorexactalpha](https://www.php.net/manual/zh/function.imagecolorexactalpha.php)— 取得指定的颜色加透明度的索引值 * [imagecolormatch](https://www.php.net/manual/zh/function.imagecolormatch.php)— 使一个图像中调色板版本的颜色与真彩色版本更能匹配 * [imagecolorresolve](https://www.php.net/manual/zh/function.imagecolorresolve.php)— 取得指定颜色的索引值或有可能得到的最接近的替代值 * [imagecolorresolvealpha](https://www.php.net/manual/zh/function.imagecolorresolvealpha.php)— 取得指定颜色 + alpha 的索引值或有可能得到的最接近的替代值 * [imagecolorset](https://www.php.net/manual/zh/function.imagecolorset.php)— 给指定调色板索引设定颜色 * [imagecolorsforindex](https://www.php.net/manual/zh/function.imagecolorsforindex.php)— 取得某索引的颜色 * [imagecolorstotal](https://www.php.net/manual/zh/function.imagecolorstotal.php)— 取得一幅图像的调色板中颜色的数目 * [imagecolortransparent](https://www.php.net/manual/zh/function.imagecolortransparent.php)— 将某个颜色定义为透明色 * [imageconvolution](https://www.php.net/manual/zh/function.imageconvolution.php)— 用系数 div 和 offset 申请一个 3x3 的卷积矩阵 * [imagecopy](https://www.php.net/manual/zh/function.imagecopy.php)— 拷贝图像的一部分 * [imagecopymerge](https://www.php.net/manual/zh/function.imagecopymerge.php)— 拷贝并合并图像的一部分 * [imagecopymergegray](https://www.php.net/manual/zh/function.imagecopymergegray.php)— 用灰度拷贝并合并图像的一部分 * [imagecopyresampled](https://www.php.net/manual/zh/function.imagecopyresampled.php)— 重采样拷贝部分图像并调整大小 * [imagecopyresized](https://www.php.net/manual/zh/function.imagecopyresized.php)— 拷贝部分图像并调整大小 * [imagecreate](https://www.php.net/manual/zh/function.imagecreate.php)— 新建一个基于调色板的图像 * [imagecreatefrombmp](https://www.php.net/manual/zh/function.imagecreatefrombmp.php)— 由文件或 URL 创建一个新图象。 * [imagecreatefromgd2](https://www.php.net/manual/zh/function.imagecreatefromgd2.php)— 从 GD2 文件或 URL 新建一图像 * [imagecreatefromgd2part](https://www.php.net/manual/zh/function.imagecreatefromgd2part.php)— 从给定的 GD2 文件或 URL 中的部分新建一图像 * [imagecreatefromgd](https://www.php.net/manual/zh/function.imagecreatefromgd.php)— 从 GD 文件或 URL 新建一图像 * [imagecreatefromgif](https://www.php.net/manual/zh/function.imagecreatefromgif.php)— 由文件或 URL 创建一个新图象。 * [imagecreatefromjpeg](https://www.php.net/manual/zh/function.imagecreatefromjpeg.php)— 由文件或 URL 创建一个新图象。 * [imagecreatefrompng](https://www.php.net/manual/zh/function.imagecreatefrompng.php)— 由文件或 URL 创建一个新图象。 * [imagecreatefromstring](https://www.php.net/manual/zh/function.imagecreatefromstring.php)— 从字符串中的图像流新建一图像 * [imagecreatefromwbmp](https://www.php.net/manual/zh/function.imagecreatefromwbmp.php)— 由文件或 URL 创建一个新图象。 * [imagecreatefromwebp](https://www.php.net/manual/zh/function.imagecreatefromwebp.php)— 由文件或 URL 创建一个新图象。 * [imagecreatefromxbm](https://www.php.net/manual/zh/function.imagecreatefromxbm.php)— 由文件或 URL 创建一个新图象。 * [imagecreatefromxpm](https://www.php.net/manual/zh/function.imagecreatefromxpm.php)— 由文件或 URL 创建一个新图象。 * [imagecreatetruecolor](https://www.php.net/manual/zh/function.imagecreatetruecolor.php)— 新建一个真彩色图像 * [imagecrop](https://www.php.net/manual/zh/function.imagecrop.php)— Crop an image to the given rectangle * [imagecropauto](https://www.php.net/manual/zh/function.imagecropauto.php)— Crop an image automatically using one of the available modes * [imagedashedline](https://www.php.net/manual/zh/function.imagedashedline.php)— 画一虚线 * [imagedestroy](https://www.php.net/manual/zh/function.imagedestroy.php)— 销毁一图像 * [imageellipse](https://www.php.net/manual/zh/function.imageellipse.php)— 画一个椭圆 * [imagefill](https://www.php.net/manual/zh/function.imagefill.php)— 区域填充 * [imagefilledarc](https://www.php.net/manual/zh/function.imagefilledarc.php)— 画一椭圆弧且填充 * [imagefilledellipse](https://www.php.net/manual/zh/function.imagefilledellipse.php)— 画一椭圆并填充 * [imagefilledpolygon](https://www.php.net/manual/zh/function.imagefilledpolygon.php)— 画一多边形并填充 * [imagefilledrectangle](https://www.php.net/manual/zh/function.imagefilledrectangle.php)— 画一矩形并填充 * [imagefilltoborder](https://www.php.net/manual/zh/function.imagefilltoborder.php)— 区域填充到指定颜色的边界为止 * [imagefilter](https://www.php.net/manual/zh/function.imagefilter.php)— 对图像使用过滤器 * [imageflip](https://www.php.net/manual/zh/function.imageflip.php)— Flips an image using a given mode * [imagefontheight](https://www.php.net/manual/zh/function.imagefontheight.php)— 取得字体高度 * [imagefontwidth](https://www.php.net/manual/zh/function.imagefontwidth.php)— 取得字体宽度 * [imageftbbox](https://www.php.net/manual/zh/function.imageftbbox.php)— 给出一个使用 FreeType 2 字体的文本框 * [imagefttext](https://www.php.net/manual/zh/function.imagefttext.php)— 使用 FreeType 2 字体将文本写入图像 * [imagegammacorrect](https://www.php.net/manual/zh/function.imagegammacorrect.php)— 对 GD 图像应用 gamma 修正 * [imagegd2](https://www.php.net/manual/zh/function.imagegd2.php)— 将 GD2 图像输出到浏览器或文件 * [imagegd](https://www.php.net/manual/zh/function.imagegd.php)— 将 GD 图像输出到浏览器或文件 * [imagegetclip](https://www.php.net/manual/zh/function.imagegetclip.php)— Get the clipping rectangle * [imagegif](https://www.php.net/manual/zh/function.imagegif.php)— 输出图象到浏览器或文件。 * [imagegrabscreen](https://www.php.net/manual/zh/function.imagegrabscreen.php)— Captures the whole screen * [imagegrabwindow](https://www.php.net/manual/zh/function.imagegrabwindow.php)— Captures a window * [imageinterlace](https://www.php.net/manual/zh/function.imageinterlace.php)— 激活或禁止隔行扫描 * [imageistruecolor](https://www.php.net/manual/zh/function.imageistruecolor.php)— 检查图像是否为真彩色图像 * [imagejpeg](https://www.php.net/manual/zh/function.imagejpeg.php)— 输出图象到浏览器或文件。 * [imagelayereffect](https://www.php.net/manual/zh/function.imagelayereffect.php)— 设定 alpha 混色标志以使用绑定的 libgd 分层效果 * [imageline](https://www.php.net/manual/zh/function.imageline.php)— 画一条线段 * [imageloadfont](https://www.php.net/manual/zh/function.imageloadfont.php)— 载入一新字体 * [imageopenpolygon](https://www.php.net/manual/zh/function.imageopenpolygon.php)— Draws an open polygon * [imagepalettecopy](https://www.php.net/manual/zh/function.imagepalettecopy.php)— 将调色板从一幅图像拷贝到另一幅 * [imagepalettetotruecolor](https://www.php.net/manual/zh/function.imagepalettetotruecolor.php)— Converts a palette based image to true color * [imagepng](https://www.php.net/manual/zh/function.imagepng.php)— 以 PNG 格式将图像输出到浏览器或文件 * [imagepolygon](https://www.php.net/manual/zh/function.imagepolygon.php)— 画一个多边形 * [imagepsbbox](https://www.php.net/manual/zh/function.imagepsbbox.php)— 给出一个使用 PostScript Type1 字体的文本方框 * [imagepsencodefont](https://www.php.net/manual/zh/function.imagepsencodefont.php)— 改变字体中的字符编码矢量 * [imagepsextendfont](https://www.php.net/manual/zh/function.imagepsextendfont.php)— 扩充或精简字体 * [imagepsfreefont](https://www.php.net/manual/zh/function.imagepsfreefont.php)— 释放一个 PostScript Type 1 字体所占用的内存 * [imagepsloadfont](https://www.php.net/manual/zh/function.imagepsloadfont.php)— 从文件中加载一个 PostScript Type 1 字体 * [imagepsslantfont](https://www.php.net/manual/zh/function.imagepsslantfont.php)— 倾斜某字体 * [imagepstext](https://www.php.net/manual/zh/function.imagepstext.php)— 用 PostScript Type1 字体把文本字符串画在图像上 * [imagerectangle](https://www.php.net/manual/zh/function.imagerectangle.php)— 画一个矩形 * [imageresolution](https://www.php.net/manual/zh/function.imageresolution.php)— Get or set the resolution of the image * [imagerotate](https://www.php.net/manual/zh/function.imagerotate.php)— 用给定角度旋转图像 * [imagesavealpha](https://www.php.net/manual/zh/function.imagesavealpha.php)— 设置标记以在保存 PNG 图像时保存完整的 alpha 通道信息(与单一透明色相反) * [imagescale](https://www.php.net/manual/zh/function.imagescale.php)— Scale an image using the given new width and height * [imagesetbrush](https://www.php.net/manual/zh/function.imagesetbrush.php)— 设定画线用的画笔图像 * [imagesetclip](https://www.php.net/manual/zh/function.imagesetclip.php)— Set the clipping rectangle * [imagesetinterpolation](https://www.php.net/manual/zh/function.imagesetinterpolation.php)— Set the interpolation method * [imagesetpixel](https://www.php.net/manual/zh/function.imagesetpixel.php)— 画一个单一像素 * [imagesetstyle](https://www.php.net/manual/zh/function.imagesetstyle.php)— 设定画线的风格 * [imagesetthickness](https://www.php.net/manual/zh/function.imagesetthickness.php)— 设定画线的宽度 * [imagesettile](https://www.php.net/manual/zh/function.imagesettile.php)— 设定用于填充的贴图 * [imagestring](https://www.php.net/manual/zh/function.imagestring.php)— 水平地画一行字符串 * [imagestringup](https://www.php.net/manual/zh/function.imagestringup.php)— 垂直地画一行字符串 * [imagesx](https://www.php.net/manual/zh/function.imagesx.php)— 取得图像宽度 * [imagesy](https://www.php.net/manual/zh/function.imagesy.php)— 取得图像高度 * [imagetruecolortopalette](https://www.php.net/manual/zh/function.imagetruecolortopalette.php)— 将真彩色图像转换为调色板图像 * [imagettfbbox](https://www.php.net/manual/zh/function.imagettfbbox.php)— 取得使用 TrueType 字体的文本的范围 * [imagettftext](https://www.php.net/manual/zh/function.imagettftext.php)— 用 TrueType 字体向图像写入文本 * [imagetypes](https://www.php.net/manual/zh/function.imagetypes.php)— 返回当前 PHP 版本所支持的图像类型 * [imagewbmp](https://www.php.net/manual/zh/function.imagewbmp.php)— 以 WBMP 格式将图像输出到浏览器或文件 * [imagewebp](https://www.php.net/manual/zh/function.imagewebp.php)— 将 WebP 格式的图像输出到浏览器或文件 * [imagexbm](https://www.php.net/manual/zh/function.imagexbm.php)— 将 XBM 图像输出到浏览器或文件 * [iptcembed](https://www.php.net/manual/zh/function.iptcembed.php)— 将二进制 IPTC 数据嵌入到一幅 JPEG 图像中 * [iptcparse](https://www.php.net/manual/zh/function.iptcparse.php)— 将二进制 IPTC 块解析为单个标记 * [jpeg2wbmp](https://www.php.net/manual/zh/function.jpeg2wbmp.php)— 将 JPEG 图像文件转换为 WBMP 图像文件 * [png2wbmp](https://www.php.net/manual/zh/function.png2wbmp.php)— 将 PNG 图像文件转换为 WBMP 图像文件 示例: ``` /** * 裁剪图片 * @param $img 原图路径 'uploads/ty/20200408/11111.png'; * @param $is_show 是否直接展示 '1' * @param $save_path 结果图保存路径 'uploads/ty/20200408'; */ function doImg($img, $is_show=0, $save_path) { if ( !file_exists($img) ) die('图片不存在'); //创建源图的实例 返回的是一个资源 $src = imagecreatefromstring(file_get_contents($img)); // 获取原图尺寸 索引 0 包含图像宽度的像素值,索引1包含图像高度的像素值、 索引2是图像类型的标记、索引3是文本字符串 $info = getimagesize($img); //var_dump($info); // 裁剪开区域的点的坐标 (这里我从最左侧 高二分之一点位开始) $x = 150; $y = 0; // 裁剪区域的宽和高 (这里我保留原图宽高原二分之一尺寸) $width = $info[0]/2; $height = $info[1]/2; // 最终保存成图片的宽和高,和源要等比例,否则会变形 $new_img_with = 300; //最终高度=指定宽度*(原始图片的宽高比),这样图片不会变形 $new_img_height = round($new_img_with * ($height / $width)); //将裁剪区域复制到新图片上 // 新建一个真彩色图像 $res_image = imagecreatetruecolor($new_img_with, $new_img_height); //重采样拷贝部分图像并调整大小,将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度 //这里是将$src中的部分图像拷贝到新建的res_image图片中 imagecopyresampled($res_image, $src, 0, 0, $x, $y, $new_img_with, $new_img_height, $width, $height); if ( $is_show == 1 ){ // 浏览器 输出图片 header('Content-Type: image/jpeg'); //输出图象到浏览器或文件 imagejpeg($res_image); imagedestroy($src); imagedestroy($res_image); }else{ // 保存图片 $ext = pathinfo($img, PATHINFO_EXTENSION); $name = time().rand(1000, 9999); $path = $save_path . '/' . $name . '.' . $ext; //输出图象到浏览器或文件 imagejpeg($res_image, $path); imagedestroy($src); imagedestroy($res_image); die( '新图片:'. $path ); } } ``` ![](https://img.kancloud.cn/7b/44/7b44a4f19414572e7029ce7708c5972c_481x458.png)