Jump to content

Recommended Posts

This is the thumbnail generator class (ThumbnailImage.php) that I use:

 

<?php
//requires GD 2.0.1 or higher
//note about gif
class ThumbnailImage{
private $image;
//not applicable to gif or png
private $quality = 100;
private $mimetype;
private $imageproperties;
private $initialfilesize;
////////////////////////////////////////////////////////
//constructor
////////////////////////////////////////////////////////
public function __construct($file, $thumbnailsize = 100){
	//check path
	is_file($file) or die ("File: $file doesn't exist.");
	$this->initialfilesize = filesize($file);
	$this->imageproperties = getimagesize($file) or die ("Incorrect file type.");
	// new function image_type_to_mime_type
	$this->mimetype = image_type_to_mime_type($this->imageproperties[2]);	
	//create image
	switch($this->imageproperties[2]){
		case IMAGETYPE_JPEG:
			$this->image = imagecreatefromjpeg($file);	
			break;
		case IMAGETYPE_GIF:	
			$this->image = imagecreatefromgif($file);
			break;
		case IMAGETYPE_PNG:
			$this->image = imagecreatefrompng($file);
			break;
		default:
			die("Couldn't create image.");
	}
	$this->createThumb($thumbnailsize);
}
////////////////////////////////////////////////////////
//destructor
////////////////////////////////////////////////////////
public function __destruct(){
	if(isset($this->image)){
		imagedestroy($this->image);			
	}
}
////////////////////////////////////////////////////////
//public methods
////////////////////////////////////////////////////////
public function getImage(){
	header("Content-type: $this->mimetype");
	switch($this->imageproperties[2]){
		case IMAGETYPE_JPEG:
			imagejpeg($this->image,"",$this->quality);
			break;
		case IMAGETYPE_GIF:
			imagegif($this->image);
			break;
		case IMAGETYPE_PNG:
			imagepng($this->image);
			break;
		default:
			die("Couldn't create image.");
	}
}
////////////////////////////////////////////////////////
public function getMimeType(){
  
	return $this->mimetype;
}
////////////////////////////////////////////////////////
public function getQuality(){
	$quality = null;
	if($this->imageproperties[2] == IMAGETYPE_JPEG){
		$quality = $this->quality;
	}
	return $quality;
}
////////////////////////////////////////////////////////
public function setQuality($quality){
	if($quality > 100 || $quality  <  1){
		$quality = 75;
    }
	if($this->imageproperties[2] == IMAGETYPE_JPEG){
		$this->quality = $quality;
	}
}
////////////////////////////////////////////////////////
public function getInitialFileSize(){	
	return $this->initialfilesize;
}
////////////////////////////////////////////////////////
//private methods
////////////////////////////////////////////////////////
private function createThumb($thumbnailsize){
	//array elements
	$srcW = $this->imageproperties[0];
	$srcH = $this->imageproperties[1];
	//only adjust if larger than reduction size
	if($srcW >$thumbnailsize || $srcH > $thumbnailsize){
		$reduction = $this->calculateReduction($thumbnailsize);
		//get proportions
  		$desW = $srcW/$reduction;
  		$desH = $srcH/$reduction;								
		$copy = imagecreatetruecolor($desW, $desH);			
		imagecopyresampled($copy,$this->image,0,0,0,0,$desW, $desH, $srcW, $srcH)
			 or die ("Image copy failed.");			
		//destroy original
		imagedestroy($this->image);
		$this->image = $copy;			
	}
}
////////////////////////////////////////////////////////
private function calculateReduction($thumbnailsize){
	//adjust
	$srcW = $this->imageproperties[0];
	$srcH = $this->imageproperties[1];
  	if($srcW < $srcH){
  		$reduction = round($srcH/$thumbnailsize);
  	}else{  			
  		$reduction = round($srcW/$thumbnailsize);
  	}
	return $reduction;
}
}//end class
////////////////////////////////////////////////////////
?>

 

This is the file (getthumb.php) called by the source of the img tag:

 

<?php
//this file will be the src for an img tag
require 'ThumbnailImage.php';
$path = @$_GET["path"];
$maxsize = @$_GET["size"];
if(!isset($maxsize)){
$maxsize=100;
}
if(isset($path)){
  $thumb = new ThumbNailImage($path, $maxsize);	
  $thumb->getImage();
}
?>

 

Usage:

<img src="getthumb.php?path=../big_pics/Access_road_n_mountains.jpg&size=256" alt= "Access road n mountains" />

 

Give it a try.

Link to comment
https://forums.phpfreaks.com/topic/143892-thumbnail-generator/#findComment-755037
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.