Calculate if image dimensions are too horizontal or vertical

694 Views Asked by At

I'm working on a PHP script that adds images to articles automatically. A common problem I've encountered is that the image dimensions can be too wide or too tall.

In terms of aspect ratio of wide images, I don't want it to be beyond 300x200 pixels:

enter image description here

So for example, the following dimensions are okay:

1000x700

100x80

200x300

The following are bad:

100x50

500x200

1200x700

Those are too wide pictures.

In terms of tall pictures, I don't want the image to be taller than a 100x200 aspect ratio:

enter image description here

So, 500x900 is fine, but 500x1005 is not.

What type of calculation can I do to determine if the picture is way too wide, way to tall, or acceptable?

I have $width and $height variables that I can use in this calculation.

1

There are 1 best solutions below

0
On

I don't know PHP, but something like the following (change the thresholds to your liking)

if $width / $height > 1.5, then
  image is too wide
else if $width / $height < 0.7, then
  image is too tall
else
  image is acceptable