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 :P )

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

  1. Type the following script,
    <?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>
    
  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 :D 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 :D

:D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di itx.web.id boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.


Related Articles:

Artikel terkait:

: PHP

About the author

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 lo

Tinggalkan Balasan

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

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: