AtlasOnEarth
AtlasOnEarth

Reputation: 83

PHP populate images

I'm looking to take a folder worth of images and have php determine their size and then write their code depending on whether the width or height is higher.

<?php

    for($i = 1; $i <= 5; $i++)
    {
        $filename = $i.".png";
        list($width, $height) = getimagesize($filename);
        if($width > $height)
        {
            echo '<img src="' .$filename. '" width="' .$width. '" height="75px" />';
            break;
        }
        elseif ($height > $width)
        {
            echo '<img src="' .$filename. '" width="75px" height="' .$height. '" />';
            break;
        }
        else
        {
            echo '<img src="' .$filename. '" width="' .$width. '" height="' .$height. '" />';
            break;
        }
    }

?>

The problem is, is that it writes the code after the "if" statement. I'm not sure if I'm coding something incorrectly. Is there a better way to do what I'm trying to do?

Cheers

Upvotes: 0

Views: 88

Answers (1)

peacemaker
peacemaker

Reputation: 2591

Remove the break; statements after each if, unless you want it to exit the for loop after only one time around the loop, which I don't think you do.

<?php

for($i = 1; $i <= 5; $i++)
{
    $filename = $i.".png";
    list($width, $height) = getimagesize($filename);
    if($width > $height){
        echo '<img src="' .$filename. '" width="' .$width. '" height="75px" />';
    }elseif ($height > $width){
        echo '<img src="' .$filename. '" width="75px" height="' .$height. '" />';
    }else{
        echo '<img src="' .$filename. '" width="' .$width. '" height="' .$height. '" />';

    }
}

?>

Upvotes: 1

Related Questions