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
Here’s the same PHP logic, just reorganised a little for clarity:
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’):