PHP图像处理与GD库实战
PHP图像处理与GD库实战PHP的GD库提供了图像处理功能。虽然不如图形处理专业软件强大但处理常见的图片需求绰绰有余。GD库可以创建图片、处理已有图片、添加文字水印、生成缩略图等。先检查GD库是否安装。php// 检查GD库if (!extension_loaded(gd)) {die(GD库未安装\n);}echo GD版本: . gd_info()[GD Version] . \n;echo 支持的格式:\n;$formats [GIF, JPEG, PNG, WEBP, BMP];foreach ($formats as $format) {$func image . strtolower($format);echo $format: . (function_exists($func) ? 支持 : 不支持) . \n;}?创建缩略图是GD库最常用的功能phpfunction createThumbnail(string $source, string $dest, int $maxWidth 300, int $maxHeight 300): bool{if (!file_exists($source)) {throw new RuntimeException(源文件不存在: $source);}$info getimagesize($source);if ($info false) {throw new RuntimeException(无法获取图片信息);}[$origWidth, $origHeight, $type] $info;// 计算缩放比例$ratio min($maxWidth / $origWidth, $maxHeight / $origHeight);$newWidth (int)($origWidth * $ratio);$newHeight (int)($origHeight * $ratio);// 创建源图像$sourceImage match ($type) {IMAGETYPE_JPEG imagecreatefromjpeg($source),IMAGETYPE_PNG imagecreatefrompng($source),IMAGETYPE_GIF imagecreatefromgif($source),IMAGETYPE_WEBP imagecreatefromwebp($source),default throw new RuntimeException(不支持的图片类型: $type),};// 创建目标图像$thumb imagecreatetruecolor($newWidth, $newHeight);// 保持PNG透明if ($type IMAGETYPE_PNG) {imagealphablending($thumb, false);imagesavealpha($thumb, true);}// 重采样imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0,$newWidth, $newHeight, $origWidth, $origHeight);// 保存$result match ($type) {IMAGETYPE_JPEG imagejpeg($thumb, $dest, 85),IMAGETYPE_PNG imagepng($thumb, $dest, 9),IMAGETYPE_GIF imagegif($thumb, $dest),IMAGETYPE_WEBP imagewebp($thumb, $dest, 85),};imagedestroy($sourceImage);imagedestroy($thumb);return $result;}// 生成示例图片$width 800;$height 600;$image imagecreatetruecolor($width, $height);// 填充背景$bgColor imagecolorallocate($image, 240, 240, 240);imagefill($image, 0, 0, $bgColor);// 画一些图形$red imagecolorallocate($image, 255, 0, 0);$green imagecolorallocate($image, 0, 255, 0);$blue imagecolorallocate($image, 0, 0, 255);imagefilledrectangle($image, 50, 50, 200, 200, $red);imagefilledellipse($image, 400, 200, 150, 150, $green);imagefilledrectangle($image, 600, 100, 750, 250, $blue);imagejpeg($image, /tmp/test.jpg, 85);imagedestroy($image);// 生成缩略图createThumbnail(/tmp/test.jpg, /tmp/thumb.jpg, 200, 200);echo 缩略图已创建\n;?给图片添加水印phpfunction addWatermark(string $sourcePath, string $destPath, string $watermarkText): bool{$image imagecreatefromjpeg($sourcePath);if ($image false) {throw new RuntimeException(无法打开图片);}$width imagesx($image);$height imagesy($image);// 白色半透明文字$fontSize 20;$textColor imagecolorallocatealpha($image, 255, 255, 255, 60);// 使用内置字体$fontX $width - 200;$fontY $height - 30;imagestring($image, 5, $fontX, $fontY, $watermarkText, $textColor);$result imagejpeg($image, $destPath, 90);imagedestroy($image);return $result;}// 图片裁剪function cropImage(string $sourcePath, string $destPath, int $x, int $y, int $width, int $height): bool{$image imagecreatefromjpeg($sourcePath);if ($image false) {throw new RuntimeException(无法打开图片);}$cropped imagecrop($image, [x $x, y $y, width $width, height $height]);if ($cropped false) {imagedestroy($image);throw new RuntimeException(裁剪失败);}$result imagejpeg($cropped, $destPath, 90);imagedestroy($cropped);imagedestroy($image);return $result;}?用GD库生成验证码图片phpfunction generateCaptcha(int $width 150, int $height 50, int $length 4): string{$image imagecreatetruecolor($width, $height);// 背景色$bgColor imagecolorallocate($image, 255, 255, 255);imagefill($image, 0, 0, $bgColor);// 干扰线for ($i 0; $i 5; $i) {$lineColor imagecolorallocate($image,rand(150, 200), rand(150, 200), rand(150, 200));imageline($image,rand(0, $width), rand(0, $height),rand(0, $width), rand(0, $height),$lineColor);}// 干扰点for ($i 0; $i 100; $i) {$pointColor imagecolorallocate($image,rand(100, 200), rand(100, 200), rand(100, 200));imagesetpixel($image, rand(0, $width), rand(0, $height), $pointColor);}// 验证码文字$chars ABCDEFGHJKLMNPQRSTUVWXYZ23456789;$code ;$fontSize 20;for ($i 0; $i $length; $i) {$char $chars[rand(0, strlen($chars) - 1)];$code . $char;$textColor imagecolorallocate($image,rand(0, 100), rand(0, 100), rand(0, 100));$x 20 $i * 30 rand(-5, 5);$y 20 rand(-5, 5);imagestring($image, 5, $x, $y, $char, $textColor);}header(Content-Type: image/png);imagepng($image);imagedestroy($image);return $code;}// 如果直接访问这个脚本生成验证码if (isset($_GET[captcha])) {$code generateCaptcha();session_start();$_SESSION[captcha] $code;exit;}?用GD库生成简单的图表phpfunction createBarChart(array $data, string $destPath, int $width 600, int $height 400): void{$image imagecreatetruecolor($width, $height);$white imagecolorallocate($image, 255, 255, 255);$black imagecolorallocate($image, 0, 0, 0);$colors [imagecolorallocate($image, 52, 152, 219),imagecolorallocate($image, 46, 204, 113),imagecolorallocate($image, 231, 76, 60),imagecolorallocate($image, 241, 196, 15),imagecolorallocate($image, 155, 89, 182),];imagefill($image, 0, 0, $white);$padding 50;$chartWidth $width - 2 * $padding;$chartHeight $height - 2 * $padding;// 坐标轴imageline($image, $padding, $padding, $padding, $height - $padding, $black);imageline($image, $padding, $height - $padding, $width - $padding, $height - $padding, $black);$maxValue max($data);$barCount count($data);$barWidth ($chartWidth / $barCount) - 10;for ($i 0; $i $barCount; $i) {$barHeight ($data[$i] / $maxValue) * $chartHeight;$x $padding $i * ($barWidth 10) 5;$y $height - $padding - $barHeight;$color $colors[$i % count($colors)];imagefilledrectangle($image, $x, $y, $x $barWidth, $height - $padding, $color);// 数值标签$labelX $x ($barWidth - 20) / 2;imagestring($image, 3, $x 5, $y - 18, (string)$data[$i], $black);}imagepng($image, $destPath);imagedestroy($image);}$data [85, 92, 78, 95, 88];createBarChart($data, /tmp/chart.png);echo 图表已生成: /tmp/chart.png\n;?GD库的功能很丰富但也有一些限制。处理大图片时内存占用很高建议先检查可用内存再操作。生成图片的质量可以通过调整压缩参数来控制JPEG是0-100PNG是0-9。