all images same size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

all images same size

Post by shiznatix »

ok this is kinda theoretical im just not sure how to go about it. i have a site where you can upload pictures but somtimes people want to upload huge pictures (fine) but i dont want to resize them and save them i will just resize them with the img height width way. i made a nice way of resizing them based on their size but i want to make all the pictures to the same size but i dont want to distort pictures with it. heres the code i have now

Code: Select all

while (false !== ($file = readdir($dir)))
    {
        if ($file != '.' && $file != '..')
        {
            $for_half = '750';
            $for_fourth = '1000';
            $for_small = '1500';

            $half = '0.5';
            $fourth = '0.25';
            $small = '0.15';

            list($width, $height) = getimagesize("./users/".$_SESSION['username']."/".$file);

            if ($width > $for_small || $height > $for_small)
            {
                $newwidth = $width * $small;
                $newheight = $height * $small;
            }
            else if ($width > $for_fourth || $height > $for_fourth)
            {
                $newwidth = $width * $fourth;
                $newheight = $height * $fourth;
            }
            else if ($width > $for_half || $height > $for_half)
            {
                $newwidth = $width * $half;
                $newheight = $height * $half;
            }

            echo $height.' '.$width;
            echo '<img src="./users/'.$_SESSION['username'].'/'.$file.'" height="'.$newwidth.'" width="'.$newheight.'">';
        }
    }
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

There are two approaches.
  • 1) You don't try and make all images the same width and same height, but just pick one common dimension. Say, for example, you want all images to be 400px tall. You can then just shrink the height to 400px, find the ratio of original height to 400, and use that to re-calculate the width.

    2) If for some reason you want all images to be the same height AND width, you can make a dummy background picture of the required dimensions, then paste on the uploaded picture that has been resized with the first method mentioned.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply