Make Thumb Image PHP


This script creates a 100px wide and high square from any jpg, gif or png image. Copy the script and save it as thumb.php.

if( isset($_GET[img]) ){

// image source and extension
$imgSrc = $_GET[img];
$imgExt = substr($imgSrc,-3);

// thumb image size in pixels
$thumbnail_width = 100;
$thumbnail_height = 100;

// getting the image dimensions  
list($width_orig, $height_orig) = getimagesize($imgSrc);   
// image type
if($imgExt == "jpg"){ $myImage = imagecreatefromjpeg($imgSrc); }
if($imgExt == "gif"){ $myImage = imagecreatefromgif($imgSrc); }
if($imgExt == "png"){ $myImage = imagecreatefrompng($imgSrc); }

$ratio_orig = $width_orig/$height_orig;

if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
   $new_height = $thumbnail_width/$ratio_orig;
   $new_width = $thumbnail_width;
} else {
   $new_width = $thumbnail_height*$ratio_orig;
   $new_height = $thumbnail_height;

$x_mid = $new_width/2;  //horizontal middle
$y_mid = $new_height/2; //vertical middle

$process = imagecreatetruecolor(round($new_width), round($new_height)); 

imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height); 
imagecopyresampled($thumb, $process, 0, 0, ($x_mid-($thumbnail_width/2)), ($y_mid-($thumbnail_height/2)), $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);


if($imgExt == "jpg"){ imagejpeg($thumb, null, 100); }
if($imgExt == "gif"){ imagegif($thumb); }
if($imgExt == "png"){ imagepng($thumb, null, 9); }



This is how you use the PHP script in your website. Replace picture.jpg with your image source.

<img src="thumb.php?img=picture.jpg" alt="" />


Related articles

Vecteezy - Free Vector Art

Vecteezy - Free Vector Art


If you need Free Vector Art do not hesitate: visit

Free vector icons

Free vector icons


Want to have some vector icons, free? Try this nice website.

Beautiful free stock photos from StockSnap

Beautiful free stock photos from StockSnap


Would you like some cool stock photos for free? Check out this article, and try this website.


BolduanYG2013-04-15 11:25

just dropping by to say hello

Gergely2012-12-28 17:41

Unfortunately, you can not do it with this script. I wrote a new tbumbnail generator script with more options. Check it out: Generate Thumbnail on the Fly with PHP

Wbmstr2012-12-21 10:18

Great script! How can I keep the original aspect ratio?

Leave a comment

We welcome any comment from you! Please keep in mind that comments are moderated and rel='nofollow' is in use. So, please do not use a domain as your name or a spammy keyword, or your comment will be deleted.

Name: *

E-mail: *

Message: *

Click on the car *

Find us on Facebook



Back to Top