starvinmarvin14 Posted January 5, 2012 Share Posted January 5, 2012 Hey, I want to be able to have a 100x100 thumbnail of pictures I am uploading while cropping the image to maintain the ratio. Right now I am stretching the image and it looks bad but it works. Here is my code... $new_thumb = "$new_pic"; $sourcefile = "$target2$pic"; $picsize = getimagesize("$target2$pic"); $source_x = $picsize[0]; $source_y = $picsize[1]; $dest_x = 100; $dest_y = 100; $targetfile = "$thumbs$pic"; $pathtofile = pathinfo($sourcefile); $extension = $pathtofile['extension']; $jpegqual = 75; if($extension=='jpg' || $extension=='jpeg' || $extension=='JPG') { $source_id = imagecreatefromjpeg("$target2$pic"); $target_id = imagecreatetruecolor($dest_x, $dest_y); $target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y); imagejpeg($target_id,"$targetfile",$jpegqual); } if($extension=='gif') { $source_id = imagecreatefromgif("$target2$pic"); $target_id = imagecreatetruecolor($dest_x, $dest_y); $target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y); imagegif($target_id,"$targetfile",$jpegqual); } if($extension=='png') { $source_id = imagecreatefrompng("$target2$pic"); $target_id = imagecreatetruecolor($dest_x, $dest_y); $target_pic = imagecopyresized($target_id,$source_id,0,0,0,0,$dest_x,$dest_y,$source_x,$source_y); imagepng($target_id,"$targetfile",$jpegqual); } How could I change this to make the thumbnails maintain the aspect ratio but still be 100x100? Link to comment https://forums.phpfreaks.com/topic/254428-maintaining-aspect-ratio-with-thumbnail/ Share on other sites More sharing options...
litebearer Posted January 5, 2012 Share Posted January 5, 2012 Resizing and Cropping are two different things. In order to have a square thumb with no distortion (or other "FIXED" dimensions) you would need to... 1. crop the image to some factor of the end dimensions (ie 200 x 200; 537 x 537 etc) 2. THEN resize the cropped image to your end dimensions. Link to comment https://forums.phpfreaks.com/topic/254428-maintaining-aspect-ratio-with-thumbnail/#findComment-1304574 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.