imagecopymerge() 是 PHP 中一個用于合并圖像的函數
imagecopyresampled() 函數而不是 imagecopymerge() 來調整圖像大小。imagecopyresampled() 函數會在調整大小時保持圖像質量,從而減少模糊。// 加載圖像
$sourceImage = imagecreatefromjpeg('source.jpg');
$destinationImage = imagecreatefromjpeg('destination.jpg');
// 獲取圖像尺寸
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
$destinationWidth = imagesx($destinationImage);
$destinationHeight = imagesy($destinationImage);
// 計算新的尺寸以保持縱橫比
$ratio = min($destinationWidth / $sourceWidth, $destinationHeight / $sourceHeight);
$newWidth = intval($sourceWidth * $ratio);
$newHeight = intval($sourceHeight * $ratio);
// 調整圖像大小
$resizedSourceImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($resizedSourceImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
// 將調整大小后的圖像復制到目標圖像
imagecopymerge($destinationImage, $resizedSourceImage, 0, 0, 0, 0, $newWidth, $newHeight, 100);
// 銷毀圖像資源
imagedestroy($sourceImage);
imagedestroy($destinationImage);
imagedestroy($resizedSourceImage);
imagecopymerge() 時,可以嘗試使用不同的插值算法來提高圖像質量。例如,可以使用 IMAGECOPYRESAMPLE_AREA 或 IMAGECOPYRESAMPLE_CUBIC 插值算法。imagecopymerge($destinationImage, $sourceImage, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight, IMAGECOPYRESAMPLE_AREA);
請注意,這些方法可能會提高圖像質量,但并不能完全消除模糊。實際效果取決于原始圖像的質量和大小。如果可能的話,最好使用高分辨率的圖像以獲得更好的結果。