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); ?> Quote Link to comment 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 Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.