skip to Main Content

here is what i have so far. i am not sure if it’s accurate, and even if it is, is there a better way to do this? i want to check images and determine if it’s tall, wide, square, or kind of square. then i want to place a class name on the div wrapping the image. kind of square is blank (no class name), a tall image has class name of tall, a wide image has class name of wide, and a square image has class name of big. i put the variables in the alt parameter of image to see what’s going on to then make any needed adjustments/tweaks.

<?php
function Hypotenuse($side1, $side2)
{
    return sqrt(($side1 * $side1) + ($side2 * $side2));
}
$class="";
$subtraction="";
$result="";
$percent=0;
$image_info = getimagesize("path/image.png");
$image_width = $image_info[0];
$image_height = $image_info[1];
if ($image_width>$image_height){ //wide image
    $class="wide";
    $subtraction=$image_width-$image_height;
} else { //tall image
    $class="tall";
    $subtraction=$image_height-$image_width;
}
$result = Hypotenuse($image_width, $image_height);
if ($subtraction>0) {
    $percent = $subtraction/$result * 100;
} else {
    $percent = 0/$result * 100;
}
//if ($subtraction<200) //old way
if ($percent<19) //close to being a square
{
    $class="";
}
if ($image_width==$image_height){ //an exact square
    $class="big";
}
?>
<div class="<?php echo $class; ?>">
    <img src="path/image.png" alt="<?php echo $image_width."x".$image_height.", ".$subtraction.", ".$result.", ".$percent; ?>" />
</div>

EDIT:
here is new script. is this better? the stuff before and after this is still the same.

if ($image_width==$image_height){ //an exact square
    $class="big";
} else {
    if ($image_width>$image_height){ //wide image
        $class="wide";
        $subtraction=$image_width-$image_height;
    } else { //tall image
        $class="tall";
        $subtraction=$image_height-$image_width;
    }
    $result = Hypotenuse($image_width, $image_height);
    $percent = $subtraction/$result * 100;
    if ($percent<19) { //close to being a square
        $class="";
    }
}

2

Answers


  1. Here’s the same PHP logic, just reorganised a little for clarity:

    $image_info = getimagesize("path/image.png");
    $image_width = $image_info[0];
    $image_height = $image_info[1];
    
    if ($image_width == $image_height) { //an exact square
        $class = 'big';
    } else {
        $subtraction = abs($image_height - $image_width);
        $hypotenuse = sqrt($image_width ** 2 + $image_height ** 2);
        $percent = $subtraction / $hypotenuse * 100;
    
        if ($percent < 19) { //close to being a square
            $class = '';
        } else {
            $class = ($image_width > $image_height) ? 'wide' : 'tall';
        }
    }
    
    Login or Signup to reply.
  2. If you wanted to use the angle of the hypotenuse instead, this is an even more compact solution (answer below assumes anything between 40 degrees and 50 degrees is ‘square-ish’):

    if ($image_width == $image_height) { //an exact square
        $class = 'big';
    } else {
        // calculate angle of hypotenuse, in degrees
        $angle = atan($image_height / $image_width) * 180 / pi();
        $class = match (true) {
            $angle < 40 => 'wide',
            $angle > 50 => 'tall',
            default => '', // square-ish
        };
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search