PHP GD比例图像


PHP GD scale image

我正在尝试使用GD,并且我正在尝试让它与大型图像一起工作。我想要一个图像,原来是640x640调整到130x130上我的图像,我在GD中创建。然而,在我的代码中,它只是从左上角裁剪图像的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