<?php

$old1 
$_POST['old1'];
$old2 $_POST['old2'];
$base1 $_POST['base1'];
$base2 $_POST['base2'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<style type="text/css">

body{
    font-size:12px;
}

input{
    margin:2px;
}

div{
    font-size:10px;
    overflow:visible;
    background-color:#ccc;
    margin:0px 15px 15px 0px;
    text-align:center;
    float:left;
}

</style>
</head>
<body>

<p style="width:480px;">The base comparison image is an image you like the general size of and would like to match the area of your new image to, regardless of aspect ratio.</p>

<form action="scale-demo.php" method="POST">
<fieldset style="width:500px;">
<p>
<em>Base comparison dimensions:</em>
<br />Height = 
<input type="text" name="base1" value="<?php echo $base1 $base1 100?>" />
<br />Width = 
<input type="text" name="base2" value="<?php echo $base2 $base2 100?>" />
</p>

<p>
<em>Original dimensions of image to resize:</em>
<br />Height = 
<input type="text" name="old1" value="<?php echo $old1 $old1 1024?>" />
<br />Width = 
<input type="text" name="old2" value="<?php echo $old2 $old2 1920?>" />
</p>
</fieldset>

<p><input type="submit" /></p>
</form>

<?php

if($base1){

    
$result resampleArea($old1,$old2,$base1,$base2);

    echo 
"\n<br />base area = " .$base1*$base2" px";
    echo 
"\n<br />old area = " .$old1*$old2" px";
    echo 
"\n<br />new area = " .$result[0]*$result[1]. " px";
    echo 
"\n<br />area discrepancy = " .abs((100*(($base1*$base2)-($result[0]*$result[1])))/($base1*$base2)). "% [" .abs(($base1*$base2)-($result[0]*$result[1])). " px]";
    echo 
"\n<br />";
    echo 
"\n<br />old dimensions = " .$old1" x " .$old2;
    echo 
"\n<br />new dimensions = " .$result[0]. " x " .$result[1];
    echo 
"\n<br /><br />";
    
    echo 
"<div style=\"height:" .$base1"px;width:" .$base2"px;\" />base comparison image</div>";
    
    echo 
"<div style=\"height:" .$result[0]. "px;width:" .$result[1]. "px;background-color:#a9dca9;\" />new img dimensions</div>";
    
    echo 
"<div style=\"height:" .$result[1]. "px;width:" .$result[0]. "px;\" />new image rotated 90&deg;</div>";

}

?>
</body>
</html>

<?php

/*
    This function will resample an image of any size to match the 
    area of a base comparison image. For example, if you have a 
    base comparison image of 10px by 10px, the area is 100 pixels. 
    An image of 8px by 50px (= 400px area) would be input to 
    return dimensions of 4px by 25px (= 100px area).

    NOTES:
    - Height and width are interchangeable and will be returned 
      in the order they are passed.
      
    - This function does not yet include the setting of a maximum 
      ceiling for height or width. Numbers are based on raw area 
      only. This means it is possible to pass a number of extreme 
      width, e.g., and get a result of 1px*area = area.
      
    - The minimum dimension returned is 1px and will not round 
      to 0px, even if the fraction ends up being 0.0001px.
      
    - This function resamples down AND up, without regard for 
      leaving smaller images as-is.
      
    - Because of fraction rounding, the new area will seldom be 
      precisely the same as the base comparison area. The goal is 
      to have indiscernible visual difference.
*/
function resampleArea($old1,$old2,$base1,$base2=NULL){

    
// if 2nd base dimension is not set, assume 1st dimension defines a square
    
$base2 $base2 $base2 $base1;

    
// make sure all are numbers before continuing
    
if(!validatePixels($base1)){
        
$error "side 1 of base comparison image";
    }
    
    if(!
validatePixels($base2)){
        
$error = ($error $error', ' ''). "side 2 of base comparison image";
    }
    
    if(!
validatePixels($old1)){
        
$error = ($error $error', ' ''). "side 1 of image to resize";
    }
    
    if(!
validatePixels($old2)){
        
$error = ($error $error', ' ''). "side 2 of image to resize";
    }

    if(
$error){
    
        
// if any are invalid numbers, report errors and do not process
        
echo "<br /><div style=\"color:red;width:500px;\">ERROR: One or more of your dimensions appear to be of invalid pixel measurement. Pixels should be whole numbers and non-negative. This error was flagged on [<em>" .$error"</em>].</div><br />";
    
    }else{
        
        
// if no errors, process
    
        // see if base dimensions are equal, otherwise find square root of area
        
$base = ($base1 == $base2) ? $base1 sqrt($base1*$base2);
    
        
// convert old dimensions to new dimensions
        // answers will most likely be decimal values
        
$new1 $base*sqrt($old1/$old2);
        
$new2 pow($base,2)/$new1;
    
        
// round off decimals
        
$new1 round($new1);
        
$new2 round($new2);
    
        
//  make sure no dimension was rounded to zero pixels; round up to 1
        
$new1 $new1 >= $new1 1;
        
$new2 $new2 >= $new2 1;

        
// send back array of two new dimensions; $var[0], $var[1]
        
return array($new1,$new2);
    }
}

/*
    This function validates a number to make sure it is numeric ONLY, 
    is a whole number and is positive (i.e., no decimals, commas or negatives).
    It's intention is to validate pixel dimensions, but it could be 
    used otherwise.
*/
function validatePixels($check){
    if(
preg_match('/^[0-9]+$/',$check) && $check >= 1){
        return 
true;
    }else{
        return 
false;
    }
}

?>