Jump to content

thumbnail


smiles

Recommended Posts

I make thumbnails for image gallery simply by this code

...while($row = mysql_fetch_array($result))  {    if ($i%5 == 0)  echo "</tr><tr>";    echo "<td>";  echo "<center><a href='view_image.php?id=".$row['id']."'>";  echo "<img src='upload/".$row['imagelink']."' width='100' height='70' align='middle' border='1'></img></a></center></td>";	  $i++;  }...

Now I try to save as an image in thumbnail form and get the big size as it presented, how can I get small size of thumbnail ?thanks !!!

Link to comment
Share on other sites

You need to use the image functions to create a new, smaller image and then copy the larger one into the smaller one so that the larger one shrinks down to the small size. Once the images are open, the function to use to copy is copyimageresampled, it will resize when it copies. Look at the reference on php.net for that function for some examples, and check the other image functions to see how to open the images and save them.

Link to comment
Share on other sites

Here's some code that will help you out:It's an object oriented thumbnail class.

<?php//requires GD 2.0.1 or higherclass ThumbnailImage{	private $image;	//not applicable to gif	private $quality = 100;	private $mimetype;	private $imageproperties;	private $initialfilesize;		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);	}	public function __destruct(){		if(isset($this->image)){			imagedestroy($this->image);					}	}	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,"",$this->quality);				break;			default:				die("Couldn't create image.");		}	}	public function getMimeType(){  		return $this->mimetype;	}		public function getQuality(){		$quality = null;		if($this->imageproperties[2] == IMAGETYPE_JPEG	|| $this->imageproperties[2] == IMAGETYPE_PNG){			$quality = $this->quality;		}		return $quality;	}	public function setQuality($quality){		if($quality > 100 || $quality  <  1){			$quality = 75;	}		if($this->imageproperties[2] == IMAGETYPE_JPEG || $this->imageproperties[2] == IMAGETYPE_PNG){			$this->quality = $quality;		}	}	public function getInitialFileSize(){			return $this->initialfilesize;	}	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;	}}?>

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

You create these as ThumbnailImage.php and image.php. You can resize the image simply by refering with path and size as your $_GET variables.

Link to comment
Share on other sites

Well, I found some in php.net, I see that I can resized image to smaller one

<?php// File and new size$filename = 'file.jpg';$percent = 0.2;// Content typeheader('Content-type: image/jpeg');// Get new sizeslist($width, $height) = getimagesize($filename);$newwidth = $width * $percent;$newheight = $height * $percent;// Load$thumb = imagecreatetruecolor($newwidth, $newheight);$source = imagecreatefromjpeg($filename);// Resizeimagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);// Outputimagejpeg($thumb); ?>

everything is all right but when I try to put something at the bottom of the code

.....// Outputimagejpeg($thumb); echo "Hello World";?>

resized image appear but the text doesn't appear :)

Link to comment
Share on other sites

The text should not appear.You first output the image-data to the browser and then some text-data, there's a difference between those two.The browser get the image-data (and I guess it's ended with EOF or something like that) and the "ignores" the text...To write a string onto the image you can use imagestring().

Link to comment
Share on other sites

You just need to think about how PHP and the web browser interact. All PHP does is sent output to the browser. But the output could be anything. Most of the time we have the output being sent as text, most often formatted as some version of HTML. But PHP output is not limited to text. You can output a PDF file, a Flash movie, an image, or several other things. You are outputting an image in this case. So you are using PHP to send image data to the browser, so that the browser can display it. That's what the imagejpeg function does. The echo statement right after that function call is sending text to the browser, but this script does not have text as output, it is outputting an image. You just need to think about what output you are using PHP to send. If you are sending an image, send an image, if you are sending text, send text. But a given script can only send one type of output, you can't use a script to output an image, and then start outputting text. It's one or the other.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...