Image Resize (Height & Width Proportional)

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Image Resize (Height & Width Proportional)

Post by jayshields »

Hi guys,

Right, I've been using a script for this already, but it only resizes based on width, not height. So, for example, if I have a 400x8000 (width x height) image, and I shrink it with my script proportionally to the width of 300, well, you know what I mean.

So with this scipt I've got you pass it the file to shrink, what to name the new file, and the max width. So would I need a script which will accept the same params, but height instead of width? Or can it accept both, but require only one of them?

Anyway, if you know of a simple script to do this, point me to it :)

Ps. All image type support is not necessary, but would be handy - I'm primarily going to be handling JPG's.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

adjust the script to accept both and have it check the dimensions for which fits your size needs better. There are a lot of examples in the many many thumbnail scripts that have been posted on the server here. Poke around, and you should find it.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Ok, I've been trying to adjust the script so that it does it by height instead of width, but it just does the same as before.

Old script:

Code: Select all

// $image - path to original image. 
// $new_image - path to save new image, 
// $new_width - should probably raname this max width.  
function resize_image_by_width($image, $new_image, $new_width) { 

      global $annwyn_resize_image_source, $annwyn_resize_image_dest; 

    $imagesize = getimagesize($image); 

    if($imagesize === false) { 
        return false; 
    } 
     
    register_shutdown_function("shutdown_func"); 
     
    switch($imagesize[2]) { 
        case IMAGETYPE_JPEG: 
            if(($annwyn_resize_image_source = 
                @imagecreatefromjpeg($image)) === false) { 

                return false; 

            } 
            break; 
        case IMAGETYPE_PNG: 
            if(($annwyn_resize_image_source = @imagecreatefrompng($image)) === false) { 

                return false; 

            } 
            break; 
        case IMAGETYPE_GIF: 
            if(($annwyn_resize_image_source = @imagecreatefromgif($image)) === false) { 

                return false; 

            } 
            break; 
        default: 
            return false; 
            break; 
    } 
     
    // Don't enlarge but reduce proportionally. 
    if($imagesize[0] < $new_width) { 
         
        $new_width = $imagesize[0]; 
        $new_height = $imagesize[1]; 
         
    } else { 

        $new_height = $new_width * ($imagesize[1] / $imagesize[0]); 
         
    } 
     
    // Use imagecreat() for GIF images. 
    if($imagesize[2] == IMAGETYPE_GIF) { 
        $annwyn_resize_image_dest = imagecreate($new_width, $new_height); 
    } else { 
        $annwyn_resize_image_dest = imagecreatetruecolor($new_width, $new_height); 
    } 

    // Save the alphablending of PNG images. 
    if($imagesize[2] == IMAGETYPE_PNG) { 
        imagealphablending($annwyn_resize_image_dest, false); 
        imagesavealpha($annwyn_resize_image_dest, true); 
    } 

    // Save the transparency of GIF images. 
    if($imagesize[2] == IMAGETYPE_GIF) { 
        $colorTransparent = imagecolortransparent($annwyn_resize_image_source); 
        imagepalettecopy($annwyn_resize_image_dest,$annwyn_resize_image_source); 
        imagefill($annwyn_resize_image_dest,0,0,$colorTransparent); 
        imagecolortransparent($annwyn_resize_image_dest, $colorTransparent); 

    } 

    imagecopyresampled( 
        $annwyn_resize_image_dest, 
        $annwyn_resize_image_source, 
        0, 0, 0, 0, $new_width, $new_height, $imagesize[0], $imagesize[1]); 

    // Output the same image type as the original. 
    switch($imagesize[2]) { 
        case IMAGETYPE_JPEG: 
            imagejpeg($annwyn_resize_image_dest, $new_image); 
            break; 
        case IMAGETYPE_PNG: 
            imagepng($annwyn_resize_image_dest, $new_image); 
            break; 
        case IMAGETYPE_GIF: 
            imagegif($annwyn_resize_image_dest, $new_image); 
            break; 
        default: 
            return false; 
            break; 
    } 
     
    imagedestroy($annwyn_resize_image_source); 
    $annwyn_resize_image_source = false; 
    imagedestroy($annwyn_resize_image_dest); 
    $annwyn_resize_image_dest = false; 
    return true; 

}
New script:

Code: Select all

// $image - path to original image. 
// $new_image - path to save new image, 
// $new_height - should probably raname this max height.  
function resize_image_by_height($image, $new_image, $new_height) { 

    global $annwyn_resize_image_source, $annwyn_resize_image_dest; 

    $imagesize = getimagesize($image); 

    if($imagesize === false) { 
        return false; 
    } 
     
    register_shutdown_function("shutdown_func"); 
     
    switch($imagesize[2]) { 
        case IMAGETYPE_JPEG: 
            if(($annwyn_resize_image_source = 
                @imagecreatefromjpeg($image)) === false) { 

                return false; 

            } 
            break; 
        case IMAGETYPE_PNG: 
            if(($annwyn_resize_image_source = @imagecreatefrompng($image)) === false) { 

                return false; 

            } 
            break; 
        case IMAGETYPE_GIF: 
            if(($annwyn_resize_image_source = @imagecreatefromgif($image)) === false) { 

                return false; 

            } 
            break; 
        default: 
            return false; 
            break; 
    } 
     
    // Don't enlarge but reduce proportionally. 
    if($imagesize[1] < $new_height) { 
         
        $new_width = $imagesize[0]; 
        $new_height = $imagesize[1]; 
         
    } else { 
        //$new_height = $new_width * ($imagesize[1] / $imagesize[0]); //old calculation
		$new_width = $new_height * ($imagesize[1] / $imagesize[0]); //new calculation
         
    } 
     
    // Use imagecreat() for GIF images. 
    if($imagesize[2] == IMAGETYPE_GIF) { 
        $annwyn_resize_image_dest = imagecreate($new_width, $new_height); 
    } else { 
        $annwyn_resize_image_dest = imagecreatetruecolor($new_width, $new_height); 
    } 

    // Save the alphablending of PNG images. 
    if($imagesize[2] == IMAGETYPE_PNG) { 
        imagealphablending($annwyn_resize_image_dest, false); 
        imagesavealpha($annwyn_resize_image_dest, true); 
    } 

    // Save the transparency of GIF images. 
    if($imagesize[2] == IMAGETYPE_GIF) { 
        $colorTransparent = imagecolortransparent($annwyn_resize_image_source); 
        imagepalettecopy($annwyn_resize_image_dest,$annwyn_resize_image_source); 
        imagefill($annwyn_resize_image_dest,0,0,$colorTransparent); 
        imagecolortransparent($annwyn_resize_image_dest, $colorTransparent); 

    } 

    imagecopyresampled( 
        $annwyn_resize_image_dest, 
        $annwyn_resize_image_source, 
        0, 0, 0, 0, $new_width, $new_height, $imagesize[0], $imagesize[1]); 

    // Output the same image type as the original. 
    switch($imagesize[2]) { 
        case IMAGETYPE_JPEG: 
            imagejpeg($annwyn_resize_image_dest, $new_image); 
            break; 
        case IMAGETYPE_PNG: 
            imagepng($annwyn_resize_image_dest, $new_image); 
            break; 
        case IMAGETYPE_GIF: 
            imagegif($annwyn_resize_image_dest, $new_image); 
            break; 
        default: 
            return false; 
            break; 
    } 
     
    imagedestroy($annwyn_resize_image_source); 
    $annwyn_resize_image_source = false; 
    imagedestroy($annwyn_resize_image_dest); 
    $annwyn_resize_image_dest = false; 
    return true; 

}
The new script does exactly the same as the old one...?

Just point me in the right direction please! :)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Sorry, it was me being stupid :)

I had set the heights/width's bigger than those of the images I was testing with...

In the new (height) script all I had to do was change

Code: Select all

$new_width = $new_height * ($imagesize[1] / $imagesize[0]); //new calculation
to

Code: Select all

$new_width = $new_height * ($imagesize[0] / $imagesize[1]); //new calculation
Thanks anyway.
Post Reply