fireice87 Posted August 21, 2007 Share Posted August 21, 2007 iv been resizing images for storage on my site with a simple function but its been bugging me that portraight pictures come out so much bigger than landscape. i took a screen shot and shoved it into fireworks to check size, the max height on portraight pictures is the same as the max width on landscape when i realised its because of the screen resolution. heres my simple resize algorithym // get size of image list($width,$height)=getimagesize($uploadedfile); // set resize er size $newwidth=110; $newheight=($height/$width)*110; $tmp=imagecreatetruecolor($newwidth,$newheight); what would you do to compensat for this? 110 width is the perfect size so i dont want to change this but 110 is to tall but capping the height down would make it thinner too (unless loosing the ratio)making it to small......... obvously the only choice is a compremise if i wanted to keep my resize code as it is. so landscape style images have width set to 110 how would i make portraight images say be 90 pixels high and keep the ratio. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2007 Share Posted August 21, 2007 Don't use hard-coded height or width values or you will stretch your images. To resize to within a height/width range: 1. Determine the maximum height and width you want for an image. For instance, say ALL pictures should be no more than 110 pixels wide and no more than 90 pixels tall. 2. For each picture, determine whether it is portrait or landscape by comparing it's height to it's width. 3. A. For landscape images, find the ratio of the target WIDTH to the actual WIDTH. For instance, if the actual image is 160 pixels, the ratio is 100 pixels / 160 pixels, or 0.625. Multiply both the actual width and the actual height by 0.625 to get the new width and height, then use those values to resize the image. B. For portrait images, do the same thing, but compare the target HEIGHT to the actual HEIGHT and the change BOTH on the resize. Quote Link to comment Share on other sites More sharing options...
fireice87 Posted August 21, 2007 Author Share Posted August 21, 2007 ok i was a little confused by that but i think im with you now im not very good with php so before i tackle actually coding it and the unlimited syntax errors that i will create i knocked up some psuedo code to make sure im understanding get image size (200 x 150) if width > height { target_width = 110 target_width/width = 0.55 ratio = 0.55 width * ratio = new_width height * ratio = new_height } else { target_height = 90 target_height/height = 0.6 ratio = 0.6 width * ratio = new_width height * ratio = new_height create(newwidth,newheight) Im pretty sure this is the idea ? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2007 Share Posted August 21, 2007 yes, that is the right idea. Quote Link to comment Share on other sites More sharing options...
fireice87 Posted August 21, 2007 Author Share Posted August 21, 2007 And a good idea it was thanks For anyone who finds this thread looking for a solution heres my function to help <?php $uploadedfile = "DisplayPic/$user.jpg"; $src = imagecreatefromjpeg($uploadedfile); // get size of image list($width,$height)=getimagesize($uploadedfile); if ($width > $height) { $target_width=110; $ratio = $target_width/$width; $newwidth = $width * $ratio; $newheight = $height * $ratio; } else { $target_height = 110; $ratio = $target_height/$height; $newwidth = $width * $ratio; $newheight = $height * $ratio; $tmp=imagecreatetruecolor($newwidth,$newheight); } // resize imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); // save resized $filename = "DisplayPic_Tiny/$user.jpg"; $_FILES['userfile']['name']; imagejpeg($tmp,$filename,100); imagedestroy($src); imagedestroy($tmp); ?> Thanks again for your help Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted August 21, 2007 Share Posted August 21, 2007 NO problem! Been there, done that. Please mark this topic solved by clicking the solved button. 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.