Page 1 of 1

all images same size

Posted: Tue Jul 12, 2005 8:08 am
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.'">';
        }
    }

Posted: Tue Jul 12, 2005 9:48 am
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.