Creating thumbnail - Resize an image with PHP

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Creating thumbnail - Resize an image with PHP

    In this tutorial I will show you how to resize an image with PHP. You can use this function in your own image gallery implementation or in any other cases when you need to create a thumbnail from your image. Using a this image resize script gives you the possibility to create various images which are different in their size.

    Step 1.
    First of all let's discuss what we need to resize an image. The answer is that we have to know the image name and the with and height values we want to resize to. However there are 2 cases. The first case if the original image size is bigger than the requested one and the second is if we want to enlarge the picture. To make an image smaller is quite normal and we will make the conversion. However to enlarge an image can result a quite bad and dirty result so be careful with it.

    Besides this we will use PHP function in this tutorial which are using the gd2 PHP extension. This extension is relevant for the basic image processing routines. GD2 is not enabled by default so you have to enable it before using the script.

    You can check and set the GD extension in the following way:
    Open your php.ini file
    If you don't know where it is than create a small Php info script which will tell you.
    <?php phpinfo(); ?>
    You can find the location in the header area.
    Find the "extension_dir" Php variable and set it to a correct value
    Eg.: extension_dir = "d:\Program Files\Php\extensions\"
    Now enable the GD2 library. Find "extension=php_gd2.dll" and remove the semicolon(;) from the beginning of the line.
    Check the settings. Call again the phpinfo file. You should find a section with caption "gd" and the parameter "GD Support" must be enabled.


    Step 2.
    Now let's make some coding. We will make a function called resizeImage(). The function has 3 parameters: the original image, the target width and the target height.

    The function header looks like this:
    Code:
    <?php
    function resizeImage($originalImage,$toWidth,$toHeight){
    }
    ?>
    Step 3.
    To calculate the resizing factor we need to know the original image dimension as well. We can get it by using the getimagesize() function which will determine the size of any GIF, JPG, PNG, SWF, SWC, PSD, TIFF, BMP, IFF, JP2, JPX, JB2, JPC, XBM, or WBMP image file and return the dimensions in an array.

    So to resize factor can be calculated as follows:
    Code:
    <?php
        // Get the original geometry and calculate scales
        list($width, $height) = getimagesize($originalImage);
        $xscale=$width/$toWidth;
        $yscale=$height/$toHeight;
        
    ?>
    After this step we can calculate the new size of the image. To keep the image dimension ratio the code will overwrite the input values to keep the ratio the same as in case of the original image.

    The new image size can be calculated as this:
    Code:
    <?php
        // Recalculate new size with default ratio
        if ($yscale>$xscale){
            $new_width = round($width * (1/$yscale));
            $new_height = round($height * (1/$yscale));
        }
        else {
            $new_width = round($width * (1/$xscale));
            $new_height = round($height * (1/$xscale));
        }
    ?>
    Step 4.
    Now we have all the necessary information so we can start with the real work. We will use 3 PHP functions. First we will create a base image with the new size using the imagecreatetruecolor() function which creates a new true color image. After it we create a temporary image from the image filename we got as a parameter. For this we use the imagecreatefromjpeg() function which creates a new image from file or URL.
    The last step is the most important part. Here we will use the function imagecopyresampled() to copy and resize part of an image with resampling.

    The code for these steps are:
    Code:
    <?php
        // Resize the original image
        $imageResized = imagecreatetruecolor($new_width, $new_height);
        $imageTmp     = imagecreatefromjpeg ($originalImage);
        imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    ?>
    As final step we returns with the new image. The complete resize function looks like this:
    Code:
    <?php
    function resizeImage($originalImage,$toWidth,$toHeight){
        
        // Get the original geometry and calculate scales
        list($width, $height) = getimagesize($originalImage);
        $xscale=$width/$toWidth;
        $yscale=$height/$toHeight;
        
        // Recalculate new size with default ratio
        if ($yscale>$xscale){
            $new_width = round($width * (1/$yscale));
            $new_height = round($height * (1/$yscale));
        }
        else {
            $new_width = round($width * (1/$xscale));
            $new_height = round($height * (1/$xscale));
        }
    
        // Resize the original image
        $imageResized = imagecreatetruecolor($new_width, $new_height);
        $imageTmp     = imagecreatefromjpeg ($originalImage);
        imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
        return $imageResized;
    }
    ?>
    ________________
    Jacques
    jacques@gw-designs.co.za
    http://coding.biz.tm
    Come join and lets make it a place to learn all the noobies how to code
    __________________

    NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH
Working...
X