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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.