PHP: dimension of a rescaled image
<p>This is a followup question to <a href="http://stackoverflow.com/questions/4639961/php-if-more-than-px-resize-to">PHP: if more than ..px resize to..</a></p>

<p>As you can see it resizes if:</p>

<pre><code>if($width &gt; 604 &amp;&amp; $height &gt; 453) {
</code></pre>

<p>This works fine is both the width and height are large.</p>

<p>However, if $width is over 604, and $height is under 453 (e.g. 604x300)m then this will skip the resize procedure. The opposite is the same(width is under, height is over). Also, if the dimensions on a picture is 500x900, and it gets resized, it gets really ugly resized. any great fix?</p>

<p>Any good suggestion on how I should handle this please?</p>

<p>edit:</p>

<pre><code>$rel_difference = array('width'=&gt;0, 'height'=&gt;0);

if($width &gt; 604 || $height &gt; 453) {
    if($width &gt; 604) $rel_difference['width'] = ($width-604)/604;
    if($height &gt; 453) $rel_difference['height'] = ($height-453)/453;

asort($rel_difference);

$newwidth = $width/(1+end($rel_difference));
$newheight = $height/(1+end($rel_difference)); 
$newwidth = round($newwidth);
$newheight = round($newheight);
$jpeg_quality = 90;
$src = "images/users/status/".$org_name;
$path_thumbs = "images/users/status/"; 
$thumb_path = $path_thumbs . '/' . $newfilename;

switch(exif_imagetype($move_me)) {
    case IMAGETYPE_GIF:
        $img_r = imagecreatefromgif($src);
    break;
    case IMAGETYPE_JPEG:
        $img_r = imagecreatefromjpeg($src);
    break;
    case IMAGETYPE_PNG:
        $img_r = imagecreatefrompng($src);
    break;
    default:
            echo "{";
            echo        "error: 'Not a image!'";
            echo "}";
        exit(0);
    break;
}
$dst_r = ImageCreateTrueColor( $newwidth, $newheight );

imagecopyresampled($dst_r, $img_r, 0, 0, 0, 0, $newwidth , $newheight, $width, $height);

imagejpeg($dst_r,$thumb_path,$jpeg_quality);

unlink($move_me);
    }
</code></pre>
