Resize an Image





11
Date Submitted Sun. Sep. 17th, 2006 9:05 AM
Revision 1 of 1
Helper ffxfiend
Tags "images" | PHP | phpcode
Comments 3 comments
You can use this function to dynamically generate html "width/height" for use with displaying a thumbnail image usingthe original image. This will help save in making dup images just for display as a thumbnail.

It takes two arguments, the path to where the image is stored and the desired width or height.

Please comment or improve this code .

/** resize_image()
*
* This functions resizes an image to any size
* specified.
*
* @ARGUMENTS: 2
*                         - $image: This is the path to the image to be resized
*                         - $target: This is the target widht/height
* @RETURNS:
*                            - html width/height string for an image tag
**/

function resize_image($image,$target)
{
        // Get the current image size
        $image = getimagesize($image);
       
        // Get the percentage to resize the image
        if ($image[0] > $image[1])
        {
                $percent = $target / $image[0];
        }
        else
        {
                $percent = $target / $image[1];
        }
       
        // Get the new width and height
        $w = round($image[0] * $percent);
        $h = round($image[1] * $percent);
       
        // Return as an html widht/height attribute
        $htmlWH = "width=\"" . $w . "\" height=\"" . $h . "\"";
        return $htmlWH;
}
 

Jeremiah Poisson

rspdesign.net
*** Stay sane inside insanity ***

Comments

Comments download time
Fri. Oct. 6th, 2006 7:03 AM    Helper BrandonReese
Comments GD Library
Wed. Oct. 11th, 2006 4:28 AM    Beginner wlasson
Comments wtf
Mon. Oct. 2nd, 2006 10:08 AM    Newbie ummmmm

Voting