在 Web 開發中,處理使用者上傳的圖片是常見需求。本文分享兩個實用的 PHP 函式:myImageResize() 用於等比例縮放,支援固定寬、固定高或限制最大寬高;imagecropper() 則能將圖片裁切為指定尺寸,適合產生縮圖或統一比例的顯示圖。兩者皆支援 GIF、JPEG、PNG 三種常見格式。
此函式可依據需求鎖定寬度、高度,或以最大寬高為限制進行縮放,自動計算目標尺寸並輸出新檔案。
/**
* 圖片縮放函數(可設置高度固定、寬度固定或者最大寬高,支援 gif/jpg/png 三種類型)
* @param string $source_path 源圖片路徑
* @param int $target_width 目標寬度
* @param int $target_height 目標高度
* @param string $fixed_orig 鎖定模式:'width'、'height' 或空值(最大寬高)
* @return string|bool 成功回傳新檔案路徑,失敗回傳 false
*/
function myImageResize($source_path, $target_width = 200, $target_height = 200, $fixed_orig = '') {
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$ratio_orig = $source_width / $source_height;
if ($fixed_orig == 'width') {
// 寬度固定,高度依比例計算
$target_height = $target_width / $ratio_orig;
} elseif ($fixed_orig == 'height') {
// 高度固定,寬度依比例計算
$target_width = $target_height * $ratio_orig;
} else {
// 最大寬高限制
if ($target_width / $target_height > $ratio_orig) {
$target_width = $target_height * $ratio_orig;
} else {
$target_height = $target_width / $ratio_orig;
}
}
// 依 MIME 類型建立圖片資源
switch ($source_mime) {
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
break;
default:
return false;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
// 重採樣縮放
imagecopyresampled($target_image, $source_image, 0, 0, 0, 0, $target_width, $target_height, $source_width, $source_height);
// 輸出為 JPEG,檔名加上 _new
$imgArr = explode('.', $source_path);
$target_path = $imgArr[0] . '_new.' . $imgArr[1];
imagejpeg($target_image, $target_path, 100);
return $target_path;
}
myImageResize($filename, 200, 200); // 限制最大寬高 200pxmyImageResize($filename, 200, 200, 'width'); // 固定寬度 200pxmyImageResize($filename, 200, 200, 'height'); // 固定高度 200px此函式先依目標長寬比計算裁切區域(置中裁切),再縮放至指定尺寸,確保輸出圖片完全符合預期比例,不會變形。
function imagecropper($source_path, $target_width, $target_height) {
$source_info = getimagesize($source_path);
$source_width = $source_info[0];
$source_height = $source_info[1];
$source_mime = $source_info['mime'];
$source_ratio = $source_height / $source_width;
$target_ratio = $target_height / $target_width;
// 計算裁切區域
if ($source_ratio > $target_ratio) {
// 源圖過高,左右裁切
$cropped_width = $source_width;
$cropped_height = $source_width * $target_ratio;
$source_x = 0;
$source_y = ($source_height - $cropped_height) / 2;
} elseif ($source_ratio < $target_ratio) {
// 源圖過寬,上下裁切
$cropped_width = $source_height / $target_ratio;
$cropped_height = $source_height;
$source_x = ($source_width - $cropped_width) / 2;
$source_y = 0;
} else {
// 比例相同,無需裁切
$cropped_width = $source_width;
$cropped_height = $source_height;
$source_x = 0;
$source_y = 0;
}
// 建立圖片資源
switch ($source_mime) {
case 'image/gif':
$source_image = imagecreatefromgif($source_path);
break;
case 'image/jpeg':
$source_image = imagecreatefromjpeg($source_path);
break;
case 'image/png':
$source_image = imagecreatefrompng($source_path);
break;
default:
return false;
}
$target_image = imagecreatetruecolor($target_width, $target_height);
$cropped_image = imagecreatetruecolor($cropped_width, $cropped_height);
// 裁切
imagecopy($cropped_image, $source_image, 0, 0, $source_x, $source_y, $cropped_width, $cropped_height);
// 縮放
imagecopyresampled($target_image, $cropped_image, 0, 0, 0, 0, $target_width, $target_height, $cropped_width, $cropped_height);
// 輸出檔案,檔名加上 _small
$dotpos = strrpos($source_path, '.');
$imgName = substr($source_path, 0, $dotpos);
$suffix = substr($source_path, $dotpos);
$imgNew = $imgName . '_small' . $suffix;
imagejpeg($target_image, $imgNew, 100);
// 釋放資源
imagedestroy($source_image);
imagedestroy($target_image);
imagedestroy($cropped_image);
return $imgNew;
}
這兩個函式涵蓋了常見的圖片處理場景:myImageResize 適合在保持原圖比例下縮小;imagecropper 則適合需要統一尺寸的縮圖(如商品列表、使用者頭像)。實際專案中可根據需求選擇,或結合使用(先裁切再縮放)。記得在部署環境確認 GD 擴充套件已啟用。