PHP GD Lib是PHP中一個(gè)非常強(qiáng)大的圖像處理庫(kù),它能夠處理各種各樣的圖像,比如創(chuàng)建縮略圖、加水印、裁剪、旋轉(zhuǎn)等操作,也支持將多幅圖像合并成一幅圖像。下面我將從以下幾個(gè)方面介紹PHP GD Lib。
第一:創(chuàng)建縮略圖
$src_img = "test.jpg"; $max_width = 200; $max_height = 200; $src_info = getimagesize($src_img); $src_mime = $src_info['mime']; switch ($src_mime) { case 'image/jpeg': $src_image = imagecreatefromjpeg($src_img); break; case 'image/png': $src_image = imagecreatefrompng($src_img); break; case 'image/gif': $src_image = imagecreatefromgif($src_img); break; default: exit("Unsupported image type"); } $src_width = imagesx($src_image); $src_height = imagesy($src_image); if (($max_width && $src_width >$max_width) || ($max_height && $src_height >$max_height)) { if ($src_width >$src_height) { $ratio = $max_width / $src_width; } else { $ratio = $max_height / $src_height; } $dst_width = $src_width * $ratio; $dst_height = $src_height * $ratio; $dst_image = imagecreatetruecolor($dst_width, $dst_height); imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); header('Content-Type: image/jpeg'); imagejpeg($dst_image); } else { header('Content-Type: ' . $src_mime); readfile($src_img); }
第二:添加水印
$src_img = "test.jpg"; $watermark_img = "watermark.png"; $watermark_pos = "bottom-right"; $watermark_padding = 10; $src_info = getimagesize($src_img); $src_mime = $src_info['mime']; switch ($src_mime) { case 'image/jpeg': $src_image = imagecreatefromjpeg($src_img); break; case 'image/png': $src_image = imagecreatefrompng($src_img); break; case 'image/gif': $src_image = imagecreatefromgif($src_img); break; default: exit("Unsupported image type"); } $watermark_info = getimagesize($watermark_img); $watermark_mime = $watermark_info['mime']; switch ($watermark_mime) { case 'image/jpeg': $watermark_image = imagecreatefromjpeg($watermark_img); break; case 'image/png': $watermark_image = imagecreatefrompng($watermark_img); break; case 'image/gif': $watermark_image = imagecreatefromgif($watermark_img); break; default: exit("Unsupported image type"); } $src_width = imagesx($src_image); $src_height = imagesy($src_image); $watermark_width = imagesx($watermark_image); $watermark_height = imagesy($watermark_image); switch ($watermark_pos) { case 'top-left': $x = $watermark_padding; $y = $watermark_padding; break; case 'top-right': $x = $src_width - $watermark_width - $watermark_padding; $y = $watermark_padding; break; case 'bottom-left': $x = $watermark_padding; $y = $src_height - $watermark_height - $watermark_padding; break; case 'bottom-right': $x = $src_width - $watermark_width - $watermark_padding; $y = $src_height - $watermark_height - $watermark_padding; break; default: exit("Unsupported watermark position"); } imagecopy($src_image, $watermark_image, $x, $y, 0, 0, $watermark_width, $watermark_height); header('Content-Type: image/jpeg'); imagejpeg($src_image);
第三:裁剪圖像
$src_img = "test.jpg"; $x = 150; $y = 150; $width = 200; $height = 200; $src_info = getimagesize($src_img); $src_mime = $src_info['mime']; switch ($src_mime) { case 'image/jpeg': $src_image = imagecreatefromjpeg($src_img); break; case 'image/png': $src_image = imagecreatefrompng($src_img); break; case 'image/gif': $src_image = imagecreatefromgif($src_img); break; default: exit("Unsupported image type"); } $image_crop = imagecrop($src_image, ['x' =>$x, 'y' =>$y, 'width' =>$width, 'height' =>$height]); header('Content-Type: image/jpeg'); imagejpeg($image_crop);
第四:旋轉(zhuǎn)圖像
$src_img = "test.jpg"; $degrees = 90; $src_info = getimagesize($src_img); $src_mime = $src_info['mime']; switch ($src_mime) { case 'image/jpeg': $src_image = imagecreatefromjpeg($src_img); break; case 'image/png': $src_image = imagecreatefrompng($src_img); break; case 'image/gif': $src_image = imagecreatefromgif($src_img); break; default: exit("Unsupported image type"); } $rotated_image = imagerotate($src_image, $degrees, 0); header('Content-Type: image/jpeg'); imagejpeg($rotated_image);
第五:將多幅圖像合并成一幅圖像
$src_images = [ "test1.jpg", "test2.jpg", "test3.jpg" ]; $src_info = getimagesize($src_images[0]); $src_mime = $src_info['mime']; $src_image_array = []; foreach ($src_images as $src_img) { switch ($src_mime) { case 'image/jpeg': $src_image_array[] = imagecreatefromjpeg($src_img); break; case 'image/png': $src_image_array[] = imagecreatefrompng($src_img); break; case 'image/gif': $src_image_array[] = imagecreatefromgif($src_img); break; default: exit("Unsupported image type"); } } $dst_width = 800; $dst_height = 600; $dst_image = imagecreatetruecolor($dst_width, $dst_height); $white = imagecolorallocate($dst_image, 255, 255, 255); imagefill($dst_image, 0, 0, $white); $x = 0; $y = 0; foreach ($src_image_array as $src_image) { $src_width = imagesx($src_image); $src_height = imagesy($src_image); imagecopy($dst_image, $src_image, $x, $y, 0, 0, $src_width, $src_height); $x += $src_width; } header('Content-Type: image/jpeg'); imagejpeg($dst_image);