opoohwan Posted February 26, 2009 Share Posted February 26, 2009 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 More sharing options...
opoohwan Posted February 26, 2009 Author Share Posted February 26, 2009 I probably worded the question wrong. Basically the code I wrote will take a 1000x1000 image and crop a 100x100 square from the image, instead of creating a 100x100 thumbnail. Any suggestions? Link to comment https://forums.phpfreaks.com/topic/147079-solved-help-with-imagecopyresized/#findComment-772186 Share on other sites More sharing options...
opoohwan Posted February 27, 2009 Author Share Posted February 27, 2009 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)); Link to comment https://forums.phpfreaks.com/topic/147079-solved-help-with-imagecopyresized/#findComment-772894 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.