Create thumbnail using php
VN:F [1.9.22_1171]
Rating: 7.5/10 (2 votes cast)

Create thumbnail using PHP GD library

The following PHP code will create thumbnail images on the fly and since it uses the PHP GD2 library, you will need an installation of PHP with at least GD 2.0.1 enabled. This is a very simple script and very easy to customize.

While creating an image gallery you can use this script to dynamically create Thumbnails.

Create thumbnail using php
Create thumbnail using php
<?php
header("Content-type: image/jpeg");
$image = $_GET['thumimg'];
$w = $_GET['w'];
$h = $_GET['h'];
if(!isset($w) && !isset($h)){
 $w = 110; //default width if $w is not set
 $h = 135; //default height if $h is not set
}
$x = @getimagesize($image);// get image size
$sw = $x[0];// width
$sh = $x[1];// height
$im = @ImageCreateFromJPEG ($image) or // Read JPEG Image
$im = false; // If image is not JPEG


if (!$im)
 readfile($image);// return the actual message if error occurs.
else {
// Create the resized image destination
   $thumb = @ImageCreateTrueColor ($w, $h);
// Copy from image source, resize it, and paste to image destination
   @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
// Output resized image
   @ImageJPEG ($thumb);
}
?>

Save the above code as create_thumb.php.

How to use:
Without specifying the width and height, Let us say your image name is example.jpg then you can access this image by going to the URL below

http://www.<YOURWEBSITE>.com/create_thumb.php?thumimg=example.jpg

You can also incorporate this to an image source HTML tag

<img src="http://www.<YOURWEBSITE>.com/create_thumb.php?thumimg=example.jpg">

With height and width specified

http://www.<YOURWEBSITE>.com/create_thumb.php?thumimg=example.jpg&w=100&h=100

You can also incorporate this to an image source HTML tag

<img src="http://www.<YOURWEBSITE>.com/create_thumb.php?thumimg=example.jpg&w=100&h=100">

DEMO

NOTE: This tutorial was published on Jul 13, 2009. Bug fixed and Demo added on May 23, 2016

VN:F [1.9.22_1171]
Rating: 7.5/10 (2 votes cast)
One thought on “Create thumbnail using PHP GD library”
  1. Hello use this

    its so simple,if u have any queries mail at [email protected]

    $ffmpeg = “ffmpeg Installed path”

    $flvfile = “source video file with root path”

    $png_path ” “Destination video file with root path and file type”

    exec(“$ffmpeg -y -i $flvfile -vframes 1 -ss 00:01:60 -an -vcodec png -f rawvideo -s 110×90 $png_path”);

    all the best….

    VA:F [1.9.22_1171]
    Rating: 3.0/5 (1 vote cast)

Leave a Reply

Your email address will not be published. Required fields are marked *