Click here to Skip to main content
11,631,896 members (69,289 online)
Click here to Skip to main content
Add your own
alternative version

Thumbnail images in PHP

, 18 Aug 2008 CPOL 19K 425 6
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

You may also be interested in...

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