PHP imagecopymerge() - 一张图片被拉伸

PHP imagecopymerge() - one image is stretched

我试图通过创建一个比源图像宽 50 像素的空图像,然后将源图像合并到它上面,来在图像的右侧添加 50 像素的白色边距。问题是源图像只是横向拉伸,所以它宽了 50 像素! 也许我使用了错误的功能来合并图像...... 这是我的代码

    $destImage = $filepath;
#echo "dest image = ".$destImage;
$sourceImage = imagecreatefrompng($filepath);
// dimensions
$src_wide = imagesx($sourceImage);
echo "src_wide=".$src_wide;
$src_high = imagesy($sourceImage);
// new image dimensions with right padding
$dst_wide = $src_wide+50;
echo "dst_wide=".$dst_wide;
$dst_high = $src_high;
// New resource image at new size
$dst = imagecreatetruecolor($dst_wide, $dst_high);
// set white padding color
$clear = array('red'=>255,'green'=>255,'blue'=>255);
// fill the image with the white padding color
$clear = imagecolorallocate( $dst, $clear["red"], $clear["green"], $clear["blue"]);
imagefill($dst, 0, 0, $clear);
// copy the original image on top of the new one
imagecopymerge($dst,$sourceImage,0,0,0,0,$src_wide,$src_high, 100);
imagepng($dst,$destImage,6);
imagedestroy($dst);
chmod($destImage,0775);

我在这里做错了什么?? 谢谢

它正在拉伸,因为您正在将它复制到目标图像的整个宽度。而是使用

imagecopyresampled($dst,$sourceImage,50,0,0,0,$src_wide,$src_high,$src_wide,$src_high);