Miroslav Danov
Miroslav Danov

Reputation: 11

Sorting portrait and landscape images

Is it possible to sort an array of images with PHP.

As example I have array with mixed landscape and portrait images. 140 width for portrait and 280 for landscapes.

I want to create a function which check if position 1 is portrait and position 2 landscape, then position 3 must be portrait to prevent gaps in the gallery.

Upvotes: 1

Views: 2794

Answers (2)

Josh
Josh

Reputation: 71

use getimagesize to check portrait or landscape

PHP Code:

$size = getimagesize($filename); 
$width = $size[0]; 
$height = $size[1]; 
$aspect = $height / $width; 
if ($aspect >= 1) $mode = "vertical"; 
else $mode = "horizontal";  

If you want to get the dimensions of a GD image resource rather than an image file, use this instead of getimagesize():

PHP Code:

$width = imagesx($img); 
$height = imagesy($img); 

finaly, implement it to your code and just do a wished sort

Upvotes: 7

James Adam
James Adam

Reputation: 2324

I don't see why you couldn't. Query the image size of each picture in your array using something like getimagesize().

Once you have the width of each image, sorting should be simple.

Upvotes: 0

Related Questions