Create thumbnail with PHP (2)

In this post you will find how to create thumbnail images with PHP. Function uses GD library so it doesn’t depend on installed utilities like ImageMagick. On the other hand, you will have to install php-gd module – “yum install php-gd”. Function will resize JPEG, PNG and GIF images, while PNG and GIF without losing their transparency. Version with GD library is a little bit longer than a version with ImageMagick utilities.

If you are looking for ImageMagick version of PHP thumbnail function, please see Create thumbnail with PHP (1). Both versions will do the same “magic”.

 * function creates a thumbnail image in the same directory with the prefix 'tn'
 * thumb should fit to the defined box (second parameter)
 * only bigger images are processed, while smaller images are just copied
 * function will resize PNG and GIF images, without losing their transparency
 * @param string  $image1_path - full path to the image
 * @param integer $box         - box dimension
function create_thumb($image1_path, $box=200){
	// get image size and type
	list($width1, $height1, $image1_type) = getimagesize($image1_path);

	// prepare thumb name in the same directory with prefix 'tn'
	$image2_path = dirname($image1_path) . '/tn_' .basename($image1_path);
	// make image smaller if doesn't fit to the box 
	if ($width1 > $box || $height1 > $box){
		// set the largest dimension
		$width2 = $height2 = $box;
		// calculate smaller thumb dimension (proportional)
		if ($width1 < $height1) $width2  = round(($box / $height1) * $width1);
		else                    $height2 = round(($box / $width1) * $height1);
		// set image type, blending and set functions for gif, jpeg and png
			case IMAGETYPE_PNG:  $img = 'png';  $blending = false; break;
			case IMAGETYPE_GIF:  $img = 'gif';  $blending = true;  break;
			case IMAGETYPE_JPEG: $img = 'jpeg'; break;
		$imagecreate = "imagecreatefrom$img";
		$imagesave   = "image$img";
		// initialize image from the file
		$image1 = $imagecreate($image1_path);

		// create a new true color image with dimensions $width2 and $height2
		$image2 = imagecreatetruecolor($width2, $height2);

		// preserve transparency for PNG and GIF images
		if ($img == 'png' || $img == 'gif'){
		  // allocate a color for thumbnail
			$background = imagecolorallocate($image2, 0, 0, 0);
			// define a color as transparent 
			imagecolortransparent($image2, $background);
			// set the blending mode for thumbnail
			imagealphablending($image2, $blending);
			// set the flag to save alpha channel  
			imagesavealpha($image2, true); 
		// save thumbnail image to the file
		imagecopyresampled($image2, $image1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);
		$imagesave($image2, $image2_path);
	// else just copy the image
	else copy($image1_path, $image2_path);

I also wrote Resize images with PHP where you can read about resizing all JPG images inside a current directory. After saving images from my camera to the computer, I needed a tool to prepare images for the Web upload. With small command line PHP script, images are converted to the lower resolution and saved to the separate directory.

Categories PHP

6 thoughts on “Create thumbnail with PHP (2)”

  1. Hello use this

    its so simple,if u have any queries mail at

    $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 110x90 $png_path");

    all the best….

  2. @k00dez – create_thumb() function can be called from PHP code to resize image in the filesystem. For example after image is upladed to the Web server. Or you can create a small PHP code to process all images in the current directory. Imagine you have a list of big images saved from camera and you want to reduce image resolution at once. Yes it can be done with PhotoShop, GIMP, IrfanView – and some of them have scripting option to automate resizing process … By all means, it was easier for me to write a PHP code and run it from command line. Here are few PHP lines to resize all images in current directory:

    // read files inside current directory
    $files = scandir('.');
    // open loop for all fetched files in current directory 
    foreach ($files as $file){
    	// process only jpg files
    	if (substr($file, -3) == 'jpg'){
    		// proportionally resize images to fit in 800x800 box
    		create_thumb($file, 800);

    @karthi – This post is about image resizing with PHP. As I can read from your comment, input for ffmpeg is a “video file” not an image. So this is not closely related to the shown PHP solution. Anyway, thank you for posting a comment, it will be surely useful. Cheers!

  3. Thank you. Great utility. Used it for a Blog where users can post Comments with an image. Needed a way to resize the humongous photos people submit to a reasonable size.

Leave a Comment