Jump to content

Determining if all JPEG's in directory have the same aspect ratio?


Recommended Posts

not directly

 

You have to use getimagesize on the folder so try something like

<?php
$imgs = glob("IMAGEFOLODER/*.jpg");
foreach($imgs as $value){
list($width,$height) = getimagesize($value);
$ratios[$value] = $width/$height;
}
print_r($ratios);
?>

 

Should work for you to return them, then you could wokr on the array as you wish to find ratio mean, ratio most probably, ratio least probable

Thank you for your help!

 

Well, ultimately what I want to do is, if all the images have the same aspect ratio, then I am going to create thumbnails with the same aspect ratio simply by resizing them from the originals (so all the thumbs will have the same dimensions).  However, if there are images with different aspect ratios, then I want to make thumbnails that are resized and cropped from the originals, so all the thumbnails have the same dimensions.

 

So basically, I just need to return a TRUE or FALSE as to whether all the images have the same aspect ratio.  Then I can create thumbnails as I outlined above based on the result.  Any ideas?

 

Thank you again for your help!

@cooldude832 - (that benzene ring on your profile is like the next step up past xylene, but I'm not sure what it is!)

 

Follow-up question: Those examples you gave work perfectly as is.  However, let's say I have a directory full of JPG's, all with the same aspect ratio, but some are in landscape versus portrait (i.e. the aspect ratio is the same but the height and width are different).  How could I modify your example to take that into account, and return that all the ratios are the same?

<?php
  $imgs = glob("IMAGEFOLODER/*.jpg");
  foreach($imgs as $value){
    list($width,$height) = getimagesize($value);
    $ratios[$value] = ($width > $height) ? $width/$height : $height/$width;
  }
  if(count(array_unique($ratios)) == 1){
    #Only 1 ratio
  }else{
    #multiple ratios
  }
?>

However, let's say I have a directory full of JPG's, all with the same aspect ratio, but some are in landscape versus portrait (i.e. the aspect ratio is the same but the height and width are different).

 

Those would NOT have the same aspect ratio. 4:3 os not the same as 3:4. Are you planning to create thumbnails that are at a 90 degree rotaion from the original? You state that if they do not have the sme ratioi you want to create thumbnails that are cropped.

 

I would suggest that you create a "max" thumbnail height & width and make the thumbnails such that they fit within that dimension. The problem with cropping the images is that they then lose some information about the original - not just the missing pixels, but the dimension as a visual que.

 

For example let's say the max thumbnail size is 100 X 100. You have three images: image A is 200 X 400, image B which is 300 X 200, and image C which is 400 X 400. The result would be thumbnails with the following sizes (that maintain their original aspect ratio, but 'fit' within the same box):

 

Image A: 50 X 100

Image B: 100 X 67

Image C: 100 X 100

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.