Create thumbnail when uploaded

Assuming the first image isn’t huge PHP can resize it using an image library which is Usually compiled in. Here’s a class for working with the library I wrote a year or two ago.


<?php
class PAMWF_ImageHandler 
{
	protected $sourcePath = '';
	protected $image = null;
	protected $destination = null;
	protected $type = '';
	protected $currentHeight = 0;
	protected $currentWidth = 0;
	
	public function load( $file ) {
		$this->sourcePath = $file;
	
		if(empty($type)) {
			$type = strtolower(pathinfo($file, PATHINFO_EXTENSION));
		}
		
		$imageAttributes = getimagesize($this->sourcePath);
		$this->currentHeight = $imageAttributes[1];
		$this->currentWidth = $imageAttributes[0];
		
		switch ($imageAttributes['mime']) {
			case 'jpg':
			case 'jpeg':
			case 'image/jpeg':
				$this->type = 'jpeg';
				$this->image = @imagecreatefromjpeg($this->sourcePath);
			break;
			case 'png':
			case 'image/png':
				$this->type = 'png';
				$this->image = @imagecreatefrompng($this->sourcePath);
			break;
			case 'gif':
			case 'image/gif':
				$this->type = 'gif';
				$this->image = @imagecreatefromgif($this->sourcePath);
			break;
		}
		
		if (!$this->image) {
			throw new Exception('PHP was unable to load Image');
		}		
	}
	
	public function clear() {
		if (is_resource($this->image)) {
			imagedestroy($this->image);
		}
		
		if (is_resource($this->destination)) {
			imagedestroy($this->destination);
		}

		$this->sourcePath = '';
		$this->image = null;
		$this->destination = null;
		$this->type = '';
		$this->currentHeight = 0;
		$this->currentWidth = 0;	
	}
	
	public function resize( $targetHeight, $targetWidth, $trim = false, $secondPass = false ) {
		if ( ($targetHeight && $targetWidth ) && 
			($targetHeight <= $this->currentHeight || $targetWidth <= $this->currentWidth) 
		) {
			$heightRatio = $targetHeight / $this->currentHeight;
			$widthRatio = $targetWidth / $this->currentWidth;

			## If we are trimming we want the short side to fit to match it's target.
			## If we aren't trimming we want the long side to fit to it's target.
			$ratio = ($this->currentHeight >= $this->currentWidth && !$trim ) ? $heightRatio : $widthRatio;
			
			$newHeight = floor( $this->currentHeight * $ratio );
			$newWidth = floor( $this->currentWidth * $ratio );
	
			if ($secondPass) {
				$this->destination = imagecreatetruecolor( $targetWidth, $targetHeight );			
			} else {
				$this->destination = imagecreatetruecolor( $newWidth, $newHeight );		
			}
			
			imagecopyresampled(
				$this->destination, 
				$this->image,
				0, 0, 0, 0, // Coordinate settings are unused by this class. 
				$newWidth,
				$newHeight, 
				$this->currentWidth, 
				$this->currentHeight
			);
			
			$this->image = $this->destination;
			$this->destination = null;
			$this->currentHeight = $newHeight;
			$this->currentWidth = $newWidth;
			
			if (!$secondPass && $trim && ( $newHeight > $targetHeight || $newWidth > $targetWidth ) ) {
				$this->resize($targetHeight, $targetWidth, true, true );
			}
		}
	}
	
	public function save ( $path, $type = null, $compression = 100 ) {
		if (empty($type)) {
			$type = $this->type;
		}
		
		if (empty($compression)) {
			$compression = 100;
		}
		
		switch ($type) {
			case 'jpg':
			case 'jpeg':
			case 'image/jpeg':
				imagejpeg($this->image, $path, $compression);
			break;
			case 'png':
			case 'image/png':
				imagepng($this->image, $path);
			break;
			case 'gif':
			case 'image/gif':
				imagegif($this->image, $path);
			break;
		}
	}
}
?>

Basic usage of the class

<?php
$imageHandler = new PAMWF_ImageHandler();
$imageHandler->load($pathToFullSizeImage);
$imageHandler->resize( $height, $width, $type, $compression ); // compression only matters to jpg images
$imageHandler->save($outputPathForThumbnail);
?>

Tinker with it and have fun.