Ben Griffiths - Freelance PHP Web Developer
Ben Griffiths - Freelance PHP Developer

GD Thumbnail Class

05
Apr

Hi all, this little set of functions will create thumbnails of images using PHP’s built in GD functionality.

The first function will create thumbnails that save the original aspect ratio. The second will create a sqaure cropped thumbnail.

To learn more about GD, and to make sure you have it installed on your webserver (Most hosts do this as standard), click here.

You can download the original as a zip here: [gd_resize.zip 0.6kb]

Here is the source code:

<?php

function resize_normal($image_type,$file_old,$file_new,$max_size)
{
    
//
    // $image_type should be either:
    //
    // png, jpg or gif
    //
    
list($width$height) = getimagesize($file_old);
    if(
$width <= $max_size && $height <= $max_size){
        
copy($file_old$file_new);
    } else {
        if (
$width $height){
            
$resize_percent $max_size $width;
            
$new_height $height $resize_percent;
            
$new_width $max_size;
        } else {
            
$resize_percent $max_size $height;
            
$new_height $max_size;
            
$new_width $width $resize_percent;
        }
        
$temp_image imagecreatetruecolor($new_width$new_height);
        switch(
$image_type){
            case 
'png':
                
$image imagecreatefrompng($file_old);
                
imagecopyresampled($temp_image$image0000$new_width$new_height$width$height);
                
imagepng($temp_image$file_new);
                break;
            case 
'jpg':
                
$image imagecreatefromjpeg($file_old);
                
imagecopyresampled($temp_image$image0000$new_width$new_height$width$height);
                
imagejpeg($temp_image$file_new);
                break;
            case 
'gif':
                
$image imagecreatefromgif($file_old);
                
imagecopyresampled($temp_image$image0000$new_width$new_height$width$height);
                
imagegif($temp_image$file_new);
                break;
        }
    }
}

function 
resize_square($image_type,$file_old,$file_new,$max_size)
{
    
//
    // $image_type should be either:
    //
    // png, jpg or gif
    //
    
list($width$height) = getimagesize($file_old);
    if(
$width <= $max_size && $height <= $max_size){
        
copy($file_old$file_new);
    } else {
        if (
$width $height) {
            
$src_w $src_h $height;
            
$src_y 0;
            
$src_x round(($width $height) / 2);
        } else {
            
$src_w $src_h $width;
            
$src_x 0;
            
$src_y round(($height $width) / 2);
        }
        
$temp_image imagecreatetruecolor($max_size$max_size);
        switch(
$image_type){
            case 
'png':
                
$image imagecreatefrompng($file_old);
                
imagecopyresampled($temp_image$image00$src_x$src_y$max_size$max_size$src_w$src_h);
                
imagepng($temp_image$file_new);
                break;
            case 
'jpg':
                
$image imagecreatefromjpeg($file_old);
                
imagecopyresampled($temp_image$image00$src_x$src_y$max_size$max_size$src_w$src_h);
                
imagejpeg($temp_image$file_new);
                break;
            case 
'gif':
                
$image imagecreatefromgif($file_old);
                
imagecopyresampled($temp_image$image00$src_x$src_y$max_size$max_size$src_w$src_h);
                
imagegif($temp_image$file_new);
                break;
        }
    }
}

?>

Client Tools

Blog Entries & PHP Tutorials

Connect with Me

Most Popular Pages

My Daily Reads

Sweet Flickry Goodness

100_0415100_0410Rainbow in rotherhamDanny at the parkDanny AsleepThe DashThe Back SeatMy New Car!
Flex PHP Framework