Creating Thumbnail

download Creating Thumbnail

of 3

Transcript of Creating Thumbnail

  • 8/3/2019 Creating Thumbnail

    1/3

    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 inyour own image gallery implementation or in any other cases when you need to create athumbnail 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 theimage name and the with and height values we want to resize to. However there are 2 cases. Thefirst case if the original image size is bigger than the requested one and the second is if we wantto enlarge the picture. To make an image smaller is quite normal and we will make theconversion. However to enlarge an image can result a quite bad and dirty result so be carefulwith 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 defaultso you have to enable it before using the script.

    You can check and set the GD extension in the following way:

    1. Open your php.ini fileIf you don't know where it is than create a small Php info script which will tell you.You can find the location in the header area.

    2. Find the "extension_dir" Php variable and set it to a correct valueEg.: extension_dir = "d:\Program Files\Php\extensions\"

    3. Now enable the GD2 library. Find "extension=php_gd2.dll" and remove the semicolon(;)from the beginning of the line.4. 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:

  • 8/3/2019 Creating Thumbnail

    2/3

    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:

    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:

    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.

  • 8/3/2019 Creating Thumbnail

    3/3

    The code for these steps are:

    As final step we returns with the new image. The complete resize function looks like this: