zorensen
zorensen

Reputation: 334

PHP GD scale image

I'm trying to play around with GD, and I'm trying to get it to work with large images. I want a image that's originally 640x640 to resize to 130x130 on my image that I'm creating in GD. However, with my code it just crops 130x130 of the image from the upper left corner. In other words, I don't get the whole image in 130x130. I've been trying every snippet I could find, but still no luck in getting a hold of this. This is the code I have;

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);

I want the image specified as $image2Url to be scaled down to 130x130 no matter what size it's originally is. It's important to me that I maintain the aspect ratio though.

I've been trying different snippets I could find, but still no luck... I've been able to resize the original image to the size I want, but not within the final image in my GD script.

Upvotes: 1

Views: 1066

Answers (1)

uri2x
uri2x

Reputation: 3202

If you're using PHP version >= 5.5 you should use imagescale(). If not, use the following right after loading $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

Upvotes: 1

Related Questions