PHP GD比例图像

PHP GD scale image

我正在尝试使用 GD,并尝试让它与大图像一起使用。我想要在 GD 中创建的图像上将最初为 640x640 的图像调整为 130x130。但是,使用我的代码,它只会从左上角裁剪 130x130 的图像。换句话说,我无法获得 130x130 的整个图像。我一直在尝试我能找到的每一个片段,但仍然没有找到这个片段。这是我的代码;

header ("Content-type: image/jpeg"); 

$image1Url = "background.jpg"; 
$image2Url = "image.jpg"; 
$image1 = imageCreateFromjpeg($image1Url); 
$image2 = imageCreateFromjpeg($image2Url);

imagecopymerge($image1, $image2, 10, 10, 0, 0, 130, 130, 100); 

$line1 = "This is the first line";
$line2 = "This is the second line";
$font = "./VERDANA.TTF";
$white = imagecolorallocate($image1, 255, 255, 255);
$yellow = imagecolorallocate($image1, 252, 205, 5);

imagefttext($image1, 14, 0, 150, 110, $yellow, $font, $line1);
imagefttext($image1, 14, 0, 150, 135, $white, $font, $line2);
Imagejpeg ($image1, NULL, 100); 

ImageDestroy ($image1);
ImageDestroy ($image2);

我希望将指定为 $image2Url 的图像缩小到 130x130,无论它的原始尺寸是多少。不过,保持纵横比对我来说很重要。

我一直在尝试我能找到的不同片段,但仍然没有成功...我已经能够将原始图像的大小调整为我想要的大小,但不在我的 GD 脚本中的最终图像中。

如果您使用的是 PHP 版本 >= 5.5,您应该使用 imagescale()。如果没有,请在加载 $image2 后立即使用以下命令:

$image3 = imagecreatetruecolor(130,130);
list($image2w, $image2h) = getimagesize($image2Url);
imagecopyresampled($image3, $image2, 0, 0, 0, 0, 130, 130, $image2w, $image2h);

// then use $image3 instead of $image2