Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Thumbnail images in PHP

, 18 Aug 2008 CPOL
Creating thumbnail images using PHP.
thumbnail_image.zip
source.jpg
<?php

/*
Created by : Mohammad Dayyan
Mds_soft@yahoo.com
1387/5/15
*/

function thumbnail_image($original_file_path, $new_width, $new_height, $save_path="")
{
	$imgInfo = getimagesize($original_file_path);
	$imgExtension = "";

    switch ($imgInfo[2])
    {
	    case 1:
	    	$imgExtension = '.gif';
	   		break;

	    case 2:
	    	$imgExtension = '.jpg';
	    	break;

	    case 3:
	    	$imgExtension = '.png';
	    	break;
    }
	
	if ($save_path=="") $save_path = "thumbnail".$imgExtension ;

	// Get new dimensions
	list($width, $height) = getimagesize($original_file_path);

	// Resample
	$imageResample = imagecreatetruecolor($new_width, $new_height);

	if ( $imgExtension == ".jpg" )
	{
		$image = imagecreatefromjpeg($original_file_path);
	}
	else if ( $imgExtension == ".gif" )
	{
		$image = imagecreatefromgif($original_file_path);
	}
	else if ( $imgExtension == ".png" )
	{
		$image = imagecreatefrompng($original_file_path);
	}

	imagecopyresampled($imageResample, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

	if ( $imgExtension == ".jpg" )
	{
		imagejpeg($imageResample, $save_path);
	}
	else if ( $imgExtension == ".gif" )
	{
		imagegif($imageResample, $save_path);
	}
	else if ( $imgExtension == ".png" )
	{
		imagepng($imageResample, $save_path);
	}
	
}

?>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mohammad Dayyan

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 18 Aug 2008
Article Copyright 2008 by Mohammad Dayyan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid