imagecopymerge()
是 PHP 中用于合并两个图像的函数
$sourceImage = imagecreatefromjpeg('path/to/source-image.jpg');
$destinationImage = imagecreatefromjpeg('path/to/destination-image.jpg');
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
$destinationWidth = imagesx($destinationImage);
$destinationHeight = imagesy($destinationImage);
$scaleX = $destinationWidth / $sourceWidth;
$scaleY = $destinationHeight / $sourceHeight;
imagecopyresampled()
函数缩放原始图像:$scaledImage = imagecreatetruecolor($destinationWidth, $destinationHeight);
imagecopyresampled($scaledImage, $sourceImage, 0, 0, 0, 0, $destinationWidth, $destinationHeight, $sourceWidth, $sourceHeight);
imagecopymerge()
函数将缩放后的图像合并到目标图像上:imagecopymerge($destinationImage, $scaledImage, 0, 0, 0, 0, $destinationWidth, $destinationHeight, 100);
imagejpeg($destinationImage, 'path/to/merged-image.jpg');
imagedestroy($sourceImage);
imagedestroy($destinationImage);
imagedestroy($scaledImage);
将以上代码整合在一起,可以实现图像的缩放和合并功能。注意替换 ‘path/to/source-image.jpg’、‘path/to/destination-image.jpg’ 和 ‘path/to/merged-image.jpg’ 为实际的图像路径。