Kategori
PHP

Create Thumbnail Version from Image with PHP

Case Study : Create Thumbnail Version from Image with PHP GD Library
Requirements : Webserver Packages, already installed. And make sure your PHP support Image GD Library (version 1-8 above)
Follow this step..

Step 1 : Prepare the Work Folder

  1. Create folder named tutorphp in your document root
  2. Prepare sample JPG image in this folder. For this tutorial, i use this sample image (p.s. : you can use any jpeg image). If you want to use this image, just right click and save as
  3. Rename the image to myPic.jpg (p.s. : this “rename” things just to make easy for follow the tutorial 😛 )

Step 2 : Create main script to resize image in to thumbnail

  1. Type the following script,
    [sourcecode language=”php”]
    <?php
    $image = ‘myPic.jpg’; //default image, change here if you want to use another image

    //get the image size and image info
    list($width_orig, $height_orig, $image_type) = getimagesize($image);

    if($image_type !== 2) { // 2 is for JPEG Image
    echo ‘Image is not JPEG Image!’;
    }
    else {
    $thumbname = ‘thumbnail_’.$image; //default name for thumbnail version of image

    // setting the height for the width 75px (75px is default width for the thumbnail). this is for maintain the ratio
    $height_tb = (int) ((75 / $width_orig) * $height_orig);

    //Create a new true color image
    $image_p = imagecreatetruecolor(75, $height_tb);

    //Create a new JPEG image from file or URL
    $image = imageCreateFromJpeg($image); //

    //Copy and resize part of an image with resampling
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, 75, $height_tb, $width_orig, $height_orig);

    //upload image to folder
    if(!is_writeable(dirname($thumbname)))
    {
    echo ‘Unable to Upload image to ‘ . dirname($thumbname);
    }
    else
    {
    //output image to browser or file, the parameter is imageJPEG(resource image, name of the image, quality)
    imageJpeg($image_p, $thumbname, 100);
    }
    }
    ?>
    <center><h1>Create Thumbnail Image</h1>
    <table border=’1′ cellpadding=’5′ cellspacing=’5′>
    <tr align=’center’> <td>Original</td> <td>Thumbnail</td> </tr>
    <tr> <td><img src="myPic.jpg" alt="image" /></td> <td><img src="<?php echo $thumbname;?>" alt="thumbnail" /></td></tr>
    </table>
    <center>
    [/sourcecode]

  2. Save with the name imagetb.php, save in the tutorphp folder
  3. Explanation : see on the script (script with green) color, Okay

Step 3 : Testing Code

  1. Go to http://localhost/tutorphp/imagetb.php
  2. You can see your image (original) with its thumbnail 😀 just like the picture below..
  3. Check tutor.php folder. You will see the thumbnail_myPic.jpg file, that is thumbnail version of your image 😀

😀

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.