How to Create Image thumbnail using PHP

Last updated on April 22nd, 2022 at 10:03 am

The following PHP code will create image thumbnails on the fly . We are using PHP GD2 library. You will need to make sure that you have PHP with at least GD 2.0.1 enabled. Very simple script and very easy to customize.

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

Generating thumbnails

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

Add the above URL to IMG SRC tag

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

Add custom Width/Height

By specifying w and h in the query string along with the thumimg you can get the image size of your choice generated.

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

Demo

NOTE: This tutorial was first published on Jul 13, 2009. Bug fixed and Demo added on May 23, 2016, Modified again and sanitized on Apr 22, 2022

Click to rate this tutorial!
[Total: 2 Average: 4]

One reply on “How to Create Image thumbnail using PHP”

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….

Leave a Reply

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