Jump to content

sort images from the highest to the shortest


AviNahum

Recommended Posts

i'm trying to create a function thats sorts 4 images from the highest one to the shortest one...

 

        // array of my images
        $images = array($proj['image1'], $proj['image2'], $proj['image3'], $proj['image4']);
        
        $sorted = array(0, 0, 0, 0);

        // put all heights in array
        for ($i=0; $i<4; $i++)
        {
            list($width, $height, $type, $attr) = getimagesize("admin/images/projects/".$images[$i]);
            
            $sorted[$i] = $height;
        }

        // sort it from the bigest number to the lowest
        rsort($sorted);

        // this gonna contain the sorted images
        $si = array();

        for ($i=0; $i<4; $i++)
        {
            list($width, $height, $type, $attr) = getimagesize("admin/images/projects/".$images[$i]);

            for ($j=0; $j<4; $j++)
            {
                if ($sorted[$j] == $height)
                    $si[$j] = $images[$i];
            }
        }

 

but if there two images with the same height, it's repeat this images few times...

any ideas?

 

Thanks!

You're over-thinking this. Try:

<?php
$images = array($proj['image1'], $proj['image2'], $proj['image3'], $proj['image4']);
$sorted = array();
foreach($images as $img) {
     list($width, $height, $type, $attr) = getimagesize("admin/images/projects/".$images[$i]);
     $sorted[$img] = $height;
}
arsort($sorted);
echo '<pre>' . print_r($sorted,true) . '</pre>';
?>

 

Ken

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.