cgm225 Posted February 13, 2008 Share Posted February 13, 2008 If I have a directory full of JPG's, I want to know if there is a way to determine if they all have the same aspect ratio? Thank you all in advance! Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/ Share on other sites More sharing options...
cooldude832 Posted February 13, 2008 Share Posted February 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/#findComment-466097 Share on other sites More sharing options...
cgm225 Posted February 13, 2008 Author Share Posted February 13, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/#findComment-466107 Share on other sites More sharing options...
cooldude832 Posted February 13, 2008 Share Posted February 13, 2008 yeah take what I gave y ou and add <?php if(count(array_unique($ratios)) == 1){ #Only 1 ratio } else{ #multiple ratios } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/#findComment-466110 Share on other sites More sharing options...
cgm225 Posted February 13, 2008 Author Share Posted February 13, 2008 @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? Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/#findComment-466133 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 <?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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/#findComment-466150 Share on other sites More sharing options...
Psycho Posted February 13, 2008 Share Posted February 13, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/#findComment-466188 Share on other sites More sharing options...
cgm225 Posted February 13, 2008 Author Share Posted February 13, 2008 Thank you both. That solved my problem. I really appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/90944-determining-if-all-jpegs-in-directory-have-the-same-aspect-ratio/#findComment-466202 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.