PHP Image size recalculation function I got a function to recalculate the image size based on two settings.


Force ratio, force the requested ratio, default the original ratio will be used.
Keepsmaller, the uploaded image may never become bigger


If keep smaller is not enabled, by default we will focus calculation on the first reached, it's hard to explain see enclosed excel screen you will probebly understand.

&lt;?php
function recalc($oldWidth, $oldHeight, $reqWidth, $reqHeight, $forceRatio = false, $keepSmaller = false){
    if ($keepSmaller) {
        $maxWidth   = ($oldWidth &lt; $reqWidth)   ? $oldWidth  : $reqWidth;
        $maxHeight  = ($oldHeight &lt; $reqHeight)  ? $oldHeight : $reqHeight;
    } else {
        $maxWidth   = $reqWidth;
        $maxHeight  = $reqHeight;
    }

    $ratio      = ($forceRatio) ? ($reqWidth / $reqHeight) : ($oldWidth / $oldHeight);

    if ($ratio != ($maxWidth / $maxHeight))
    {
        if ($keepSmaller &amp;&amp; $ratio &gt; ($maxWidth / $maxHeight)){
            $maxHeight = $maxWidth / $ratio;
        } elseif (!$forceRatio &amp;&amp; $ratio &lt; ($maxWidth / $maxHeight)) {
            $maxWidth  = $maxHeight * $ratio;
        }
    }

    echo $maxWidth . ', ' . $maxHeight;
}

recalc( 480,  360, 1440, 1080); echo "&lt;br /&gt;";
recalc(1920, 1440, 1440, 1080); echo "&lt;br /&gt;";
recalc( 480,  360, 1920, 1080); echo "&lt;br /&gt;";
recalc(3840, 2880, 1920, 1080); echo "&lt;br /&gt;";
echo "&lt;br/&gt;";

recalc( 480,  360, 1440, 1080, false, true); echo "&lt;br /&gt;";
recalc(1920, 1440, 1440, 1080, false, true); echo "&lt;br /&gt;";
recalc( 480,  360, 1920, 1080, false, true); echo "&lt;br /&gt;";
recalc(3840, 2880, 1920, 1080, false, true); echo "&lt;br /&gt;";
echo "&lt;br/&gt;";

recalc( 480,  360, 1440, 1080, true); echo "&lt;br /&gt;";
recalc(1920, 1440, 1440, 1080, true); echo "&lt;br /&gt;";
recalc( 480,  360, 1920, 1080, true); echo "&lt;br /&gt;";
recalc(3840, 2880, 1920, 1080, true); echo "&lt;br /&gt;";
echo "&lt;br/&gt;";

recalc( 480,  360, 1440, 1080, true, true); echo "&lt;br /&gt;";
recalc(1920, 1440, 1440, 1080, true, true); echo "&lt;br /&gt;";
recalc( 480,  360, 1920, 1080, true, true); echo "&lt;br /&gt;";
recalc(3840, 2880, 1920, 1080, true, true); echo "&lt;br /&gt;";
echo "&lt;br/&gt;";


This are the input, params and outputs used for testing.
However some scenarios are still unwatched.


The 1st table states itself
the second contains the original sizes
the third the requested/target sizes
the last one is what 'we' expect for it to become
Complexity

It looks to me that even with this simple function, the complexity inside takes you for a ride. You don't have it under control. The reason is obvious: You try to do too much in one function. 

If your program can set two flags, it can also choose between different functions: One for simple resizing, one for resizing while keeping the aspect ratio the same, etc. As you can guess there could be some overlap between these function. Therefore:

Classes

Classes are the ideal structures to put these multiple functions in, we call them 'methods', and let them share some common code. Let me give an example, that does some of the things that your function does, and some it doesn't:

function greatestCommonDivisor($int1,$int2)
// return greatest common divisor, euclidean algorithm
{
   if ($int2 == 0) return $int1;
   else return greatestCommonDivisor($int2,$int1 % $int2);
}


class ImageHandle
{
  private $outputSize;
  private $originalSize;

  public function __construct($width,$height)
  // constructor, set initial size
  {
    $this-&gt;setSize($width,$height);
    $this-&gt;originalSize = $this-&gt;getSize();
  }

  public function getSize()
  // get the Size of the rectangle
  {
    return $this-&gt;outputSize;
  }

  public function setSize($width,$height)
  // set the Size of the rectangle
  {
    $this-&gt;outputSize = ['width' =&gt; $width,'height' =&gt; $height];
    return $this;
  }

  public function resetSize()
  // go back to the orginal size
  {
    $this-&gt;outputSize = $this-&gt;originalSize;
    return $this;
  }

  private function computeAspectRatio($width,$height,$prefix = '')
  // return the aspect ratio as two numbers, with prefix
  {
    $divisor = greatestCommonDivisor($width,$height);
    $name    = ($prefix == '' ? 'aspect' : $prefix.'Aspect');
    return [$name.'X' =&gt; intdiv($width,$divisor),
            $name.'Y' =&gt; intdiv($height,$divisor)];
  }

  public function getAspectRatio()
  // return the aspect ratio as two numbers for set size
  {
    extract($this-&gt;getSize());
    return computeAspectRatio($width,$height);
  }

  public function setAspectRatio($newWidth,$newHeight)
  // set a new aspect ratio while keeping within the current width and height
  {
    extract($this-&gt;getSize());
    extract(computeAspectRatio($newWidth,$newHeight,'new'));
    if ($newAspectX &gt; $aspectX) $height = $width*$newAspectY/$newAspectX;
                           else $width  = $height*$newAspectX/$newAspectY;
    return $this-&gt;setSize($width,$height);
  }

  public function rescaleSize($maxWidth,$maxHeight)
  // rescale, while keeping aspect ratio the same
  {
    extract($this-&gt;getSize());
    $newWidth  = $maxWidth;
    $newHeight = round($maxWidth*$height/$width);
    if ($newHeight &gt; $maxHeight) {
      $newWidth  = round($newWidth*$maxHeight/$newHeight);
      $newHeight = $maxHeight;
    }
    return $this-&gt;setSize($newWidth,$newHeight);
  }

  public function echoInfo($title)
  // just here for this example, should be removed
  {
    echo $title.': ('.implode(',',$this-&gt;getSize()).
                 ') '.implode(':',$this-&gt;getAspectRatio()).'&lt;br&gt;';
    return $this;
  }

}

$test = [[ 480,  360, 1440, 1080],
         [1920, 1440, 1440, 1080],
         [ 480,  360, 1920, 1080],
         [3840, 2880, 1920, 1080]];

foreach ($test as $data)
{
  $image = new ImageHandle($data[0],$data[1]);
  $image-&gt;echoInfo('Orginal')
        -&gt;setSize($data[2],$data[3])
        -&gt;echoInfo('Set')
        -&gt;resetSize()
        -&gt;rescaleSize($data[2],$data[3])
        -&gt;echoInfo('Rescale')
        -&gt;resetSize()
        -&gt;setAspectRatio($data[2],$data[3])
        -&gt;echoInfo('Aspect Ratio');
  echo '&lt;br&gt;';
}


When ran this returns:

Orginal: (480,360) 4:3
Set: (1440,1080) 4:3
Rescale: (1440,1080) 4:3
Aspect Ratio: (480,360) 4:3

Orginal: (1920,1440) 4:3
Set: (1440,1080) 4:3
Rescale: (1440,1080) 4:3
Aspect Ratio: (1920,1440) 4:3

Orginal: (480,360) 4:3
Set: (1920,1080) 16:9
Rescale: (1440,1080) 4:3
Aspect Ratio: (480,270) 16:9

Orginal: (3840,2880) 4:3
Set: (1920,1080) 16:9
Rescale: (1440,1080) 4:3
Aspect Ratio: (3840,2160) 16:9


Ok, when you're not used to classes this might seem like killing a fly with an atomic bomb. I agree. But bear with me. I didn't call this class ImageHandle for nothing. You could extend this class to include other methods, mine are just examples, or an image loader and image saver, and much more. 

However, notice the relative simplicity of each method: They don't get overburdened with functionality, so they stay relatively simple. Each one performs a single task.

Just like in your function, you could apply more than one method to change the size, but I found that to be a bit useless. Nevertheless, it will work.

PS: I intentionally left out the comments I normally put in my classes, this example is already quite long. You don't need them anyway, it isn't rocket science. :-)

PS2: I made this class slightly harder to understand, because I'm now using an array to store width and height. Some methods get very short, which is nice, but in others I use extract() to get at the data in the array. This is a method I often use. Arrays can be useful, but if you're not used to them they make it harder to read the code. Sorry for that.