bytesize Posted December 26, 2010 Share Posted December 26, 2010 I need to set the width and let height adjust to the proportions of width. Can someone please help me with this? I understand that $canvas_height should be 160, but then the image will crop to whichever side is greatest not by the width only. <?php $img = "image.jpg"; $canvas_width = 160; $canvas_height = 0; list($img_width, $img_height) = getimagesize($img); $ratio_orig = $img_width / $img_height; if($canvas_width / $canvas_height > $ratio_orig) { $canvas_width = $canvas_height * $ratio_orig; } else { $canvas_height = $canvas_width / $ratio_orig; } $original = imagecreatefromjpeg($img); $canvas = imagecreatetruecolor($canvas_width, $canvas_height); imagecopyresampled($canvas, $original, 0, 0, 0, 0, $canvas_width, $canvas_height, $img_width, $img_height); $dest = "medium/image.jpg"; imagejpeg($canvas, $dest, 100); ?> Link to comment https://forums.phpfreaks.com/topic/222668-crop-images-by-width/ Share on other sites More sharing options...
dragon_sa Posted December 26, 2010 Share Posted December 26, 2010 // scale to width $main_width = 160; $main_height = $img_height * ($main_width / $img_width); $original= imagecreatefromjpeg($img); you wont need the if statements this will do the trick Link to comment https://forums.phpfreaks.com/topic/222668-crop-images-by-width/#findComment-1151528 Share on other sites More sharing options...
bytesize Posted December 26, 2010 Author Share Posted December 26, 2010 Thank you dragon_sa, it works. I really appreciate your help. Thanks again! Link to comment https://forums.phpfreaks.com/topic/222668-crop-images-by-width/#findComment-1151534 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.