waynew Posted August 26, 2008 Share Posted August 26, 2008 Is this okay or do you guys have any better suggestions? Cheers. <?php class ImageResize{ function resize($width,$height,$width_limit){ if($width > $width_limit){ $width = $width * 0.99; $height = $height * 0.99; $this->resize($width,$height,$width_limit); } if($height > 600){ $width = $width * 0.99; $height = $height * 0.99; $this->resize($width,$height,$width_limit); } else{ echo 'width = "'.$width.'" height = "'.$height.'"'; } } } ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted August 26, 2008 Share Posted August 26, 2008 First off, I would recommend to write a true resize where you resize the image itself using the GD library. This will help with both page load time as well as the resolution of the resized image. Secondly, there really is no reason to do the resize() method recursively. If your width is over the limit, just replace the width with the limit and then ratio the height to match: <?php function resize($width, $height, $width_limit) { if ($width > $width_limit) { $ratio = $width_limit / $width; $width = $width_limit; $height = round($height * $ratio); } // ...continue script } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 26, 2008 Share Posted August 26, 2008 it's a little silly to go through iterations of 0.99*width when you could do one calculation and be done with it.. why not set the width equal to the width limit, then multiply the height by the ratio of width limit to the original width? function resize($width, $height, $width_limit) { if ($width > $width_limit) { $final_width = $width_limit; $final_height = round($height * ($width_limit / $width)); } elseif ($height > 600) { $final_width = round($width * (600 / $height)); $final_height = 600; } echo 'width = "'.$final_width.'" height = "'.$final_height.'"'; } that saves you a great deal of resources over constantly iterating to reach the same end product. EDIT: obsidian beat me to it, but i don't feel like wasting all this typing. Quote Link to comment Share on other sites More sharing options...
waynew Posted August 26, 2008 Author Share Posted August 26, 2008 Thanks guys. The reason I posted it was because I just wasn't happy with it. Thanks. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 26, 2008 Share Posted August 26, 2008 No offense guys, but there are flaws in both of those scripts. In the first one the resizing on the Y axis is only done to maintain the ratio from the x axis resizing - there is no resizing to ensure the image fits in the 600 height limit. The 2nd function has a similar problem. If the image is just over the width restriction it will only resize based on the X axis even though the Y axis may be way above the height restriction - resulting ina an image that is still greater than 600 in height. I would also create a function that takes the height limit as a parameter so it will be reusable: <?php class ImageResize{ function resize($width, $height, $width_limit, $height_limit=600){ if($width > $width_limit || $height > $height_limit) { //Image is too big - need to resize $scale = ($width / $height); //Determine on which axis to resize on if ( ($width/$width_limit) > ($height/$height_limit) ) { //Scale on width $width = $width_limit; $height = round($width_limit/$scale); } else { //Scale on height $width = round($height_limit/$scale); $height = $height_limit; } } echo "width = '$width' height = '$height'"; } } ?> Quote Link to comment Share on other sites More sharing options...
DarkWater Posted August 26, 2008 Share Posted August 26, 2008 Why don't you use the GD library for true resizing, as obsidian suggested? Quote Link to comment Share on other sites More sharing options...
akitchin Posted August 26, 2008 Share Posted August 26, 2008 @mj: good point - i was mostly rewriting to avoid the iterations. Quote Link to comment Share on other sites More sharing options...
obsidian Posted August 26, 2008 Share Posted August 26, 2008 @mj: good point - i was mostly rewriting to avoid the iterations. Same here, thus the comment in my code "...continue script" 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.