Jump to content

Feedback on picture resize function


waynew

Recommended Posts

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.'"';
	}
}

}
?>

Link to comment
Share on other sites

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
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'";
   }
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.