Sorting portrait and landscape images

送分小仙女□ 提交于 2020-02-25 04:44:21

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/9081418/sorting-portrait-and-landscape-images

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!