Jump to content

[SOLVED] Help with imagecopyresized()


opoohwan

Recommended Posts

I am creating my first CMS using PHP. for this feature, I want to allow a user to upload an image and PHP will upload the image, then also create a thumbnail of either 100px width or 100px height (depending on if it is portrait or landscape orientation). So far I have only one problem. The GD Function imagecopyresized will create the thumbnail for me, but instead of it being the larger image shrunk to the new size, it creates a cropped version of the larger on in the new size. here is the code please help. it is way over my head.

 

in the original file (calling from an included function.php)

createThumbnail("uploadedimages", "$image", "uploadedimages/thumbs", 100);

 

in the function.php page

	function createThumbnail($imageDirectory, $imageName, $thumbDirectory, $value) {
	$srcImg = imagecreatefromjpeg("$imageDirectory/$imageName");
	$origWidth = imagesx($srcImg);
	$origHeight = imagesy($srcImg);
	if($origWidth > $origHeight) {

	$thumbWidth = $value;
	$ratio = $thumbWidth / $origWidth;
	$thumbHeight = $origHeight * $ratio;

	$thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight);
	imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($thumbImg), imagesy($thumbImg));

	imagejpeg($thumbImg, "$thumbDirectory/$imageName");
	}else{
	$thumbHeight = $value;
	$ratio = $thumbHeight / $origHeight;
	$thumbWidth = $origWidth * $ratio;

	$thumbImg = imagecreatetruecolor($thumbHeight, $thumbWidth);
	imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $thumbHeight, $thumbWidth, imagesy($thumbImg), imagesx($thumbImg));

	imagejpeg($thumbImg, "$thumbDirectory/$imageName");
	}

}

Link to comment
https://forums.phpfreaks.com/topic/147079-solved-help-with-imagecopyresized/
Share on other sites

Ok I actually figured it out. the problem was that I put the wrong variables into the function.

 

the last to bits of information was supposed to be the original images height and width, instead I had the new images.

 

corrected code.

imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbHeight, $thumbWidth, $origHeight, $origWidth);

 

original incorrect code

imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbHeight, $thumbWidth, imagesy($thumbImg), imagesx($thumbImg));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.