PHP: Thumbnail erstellen

sebbl

Grünschnabel
Hallo,

in einer Bildergalerie oder ähnlichem ist es oft notwendig, eine Vorschau für die vorhandenen Bilder zu erstellen, um geringe Ladezeiten garantieren zu können.
Ich habe einen kleinen Code-Schnipsel, der diese Vorschaubilder erstellen soll.

Meine Bitte ist, dass Ihr ihn Euch mal zu Gemüte führt und mir ein kleines Feedback gebt, was noch angepasst und erweitert werden könnte / sollte.

Danke :)

Grüße

Der Schnipsel
PHP:
<?php
	/*
		What does this to any image?
		This file creates a thumbnail of any via GET['src'] specified JPEG-Image
		Width and height of the new thumbnail has to be specified via GET['width'] and GET['height']
		You can also specify a background color via GET['bgcolor']
	*/
	$dflt_width = 100; // default width, only needed if GET['width'] is not specified
	$dflt_height = 100; // default height, only needed if GET['height'] is not specified
	$dflt_bgcolor = "#000000"; // default bgcolor, if GET['bgcolor'] is not specified
	
	/*
		transforms any specified hex-color into rgb and returns an array of r, g, b
		parameter:
			- color: hex-code
	*/
	function html2rgb($color)
	{
		if ($color[0] == '#')
			$color = substr($color, 1);
	
		if (strlen($color) == 6)
			list($r, $g, $b) = array($color[0].$color[1],
									 $color[2].$color[3],
									 $color[4].$color[5]);
		elseif (strlen($color) == 3)
			list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
		else
			return false;
	
		$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
	
		return array($r, $g, $b);
	}

	header("Content-Type: image/jpeg"); // send header
	
	$thmbnl_width = isset($_GET["width"]) ? $_GET["width"] : $dflt_width; // get thumbnail width
	$thmbnl_height = isset($_GET["height"]) ? $_GET["height"] : $dftl_height; // get thumbnail height
	$thmbnl_bgcolor = isset($_GET["bgcolor"]) ? html2rgb($_GET["bgcolor"]) : $dflt_bgcolor; // get thumbnail background-color
	
	$src_filename = isset($_GET["src"]) ? $_GET["src"] : null; // get filename of specified image
	$src_size = @getimagesize($src_filename); // get size of specified image
	$src_width = is_array($src_size) ? $src_size[0] : $dflt_width; // width
	$src_height = is_array($src_size) ? $src_size[1] : $dfl_height; // height
	$src = @imagecreatefromjpeg($src_filename); // load image
	
	$thumbnail = imagecreatetruecolor($thmbnl_width, $thmbnl_height); // create new true color image 
	$bgcolor = imagecolorallocate($thumbnail, $thmbnl_bgcolor[0], $thmbnl_bgcolor[1], $thmbnl_bgcolor[2]); // create image background-color
	
	imagefill($thumbnail, 0, 0, $bgcolor); // fill image with specified color
	
	$src_props = $src_width / $src_height; // get image proportions
	$thmbnl_props = $thmbnl_width / $thmbnl_height; // get thumbnail proportions
	
	if($src_width > $thmbnl_width or $src_height > $thmbnl_height) // image dimensions greater than thumbnail dimensions?
	{
		if($src_width >= $src_height and $thmbnl_props <= $src_props) // width >= height, thumbnail proportions <= image proportions?
		{
			$width = $thmbnl_width; // use thumbnail width
			$height = ($thmbnl_width / $src_width) * $src_height;	// calc height (percentage)
		}
		else
		{
			$height = $thmbnl_height; // use thumbnail height
			$width = ($thmbnl_height / $src_height) * $src_width;	// calc width (percentage)			
		}
	}
	else // use image dimensions
	{
		$width = $src_width;
		$height = $src_height;
	}
	
	imagecopyresized($thumbnail, $src, ($thmbnl_width - $width) / 2, ($thmbnl_height - $height) / 2, 0, 0, $width, $height, $src_width, $src_height); // resize image and copy to thumbnail
	imagejpeg($thumbnail); // ouput thumbnail
?>
 
Wenn du das Vorschaubild bei jeder Anfrage erzeugst, ist die effektive Ladezeit nicht unbedingt geringer. Es wäre also sinnvoll, die Vorschaubilder zu speichern, um schneller darauf zugreifen zu können.
 
Das stimmt natürlich. Deswegen habe ich auch eine Klasse erstellt, welche das Ausgeben, das Speichern oder das Ausgeben & Speichern des Bildes ermöglicht. Könnte ungefähr so aussehen.

PHP:
<?php
	class image_handler
	{
		public $src;
		public $thmbnl_dflt_width = 100;
		public $thmbnl_dflt_height = 100;
		public $thmbnl_dflt_bgcolor = "#000000";
		public $thmbnl_dflt_filename = "thumb.jpg";
		public $thmbnl_dflt_directory = "thumbs/";
	
		public function __construct($src = null)
		{
			if(!is_null($src)) $this->src = $src;
		}
		
		/*
			transforms any specified hex-color into rgb and returns an array of r, g, b
			parameter:
				- color: hex-code
		*/
		private function html2rgb($color)
		{
			if ($color[0] == '#')
				$color = substr($color, 1);
			
			if (strlen($color) == 6)
				list($r, $g, $b) = array($color[0].$color[1],
										 $color[2].$color[3],
										 $color[4].$color[5]);
			elseif (strlen($color) == 3)
				list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
			else
				return false;
		
			$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
		
			return array($r, $g, $b);
		}
		
		public function create_thumbnail($mode = 0, $output_filename = null, $output_width = null, $ouput_height = null, $output_bgcolor = null, $output_directory = null)
		{			
			$dflt_width = $this->thmbnl_dflt_width; // default width
			$dflt_height = $this->thmbnl_dflt_height; // default height
			$dflt_bgcolor = $this->thmbnl_dflt_bgcolor; // default bgcolor
			$dflt_filename = $this->thmbnl_dflt_filename; // default filename
			$dflt_directory = $this->thmbnl_dflt_directory; // default directory
			
			$thmbnl_width = !is_null($output_width) ? $output_width : $dflt_width; // get thumbnail width
			$thmbnl_height = !is_null($output_height) ? $output_height : $dflt_height; // get thumbnail height
			$thmbnl_bgcolor = $this->html2rgb((!is_null($output_bgcolor) ? $output_bgcolor : $dflt_bgcolor)); // get thumbnail background-color
			$thmbnl_filename = !is_null($output_filename) ? $output_filename : $dflt_filename; // get thumbnail filename
			$thmbnl_directory = !is_null($output_directory) ? $output_directory : $dflt_directory; // get thumbnail directory
			
			$src_filename = $this->src; // get filename of specified image
			$src_size = @getimagesize($src_filename); // get size of specified image
			$src_width = is_array($src_size) ? $src_size[0] : $dflt_width; // width
			$src_height = is_array($src_size) ? $src_size[1] : $dfl_height; // height
			$src = @imagecreatefromjpeg($src_filename); // load image
			
			$thumbnail = imagecreatetruecolor($thmbnl_width, $thmbnl_height); // create new true color image 
			$bgcolor = imagecolorallocate($thumbnail, $thmbnl_bgcolor[0], $thmbnl_bgcolor[1], $thmbnl_bgcolor[2]); // create image background-color
			
			imagefill($thumbnail, 0, 0, $bgcolor); // fill image with specified color
			
			$src_props = $src_width / $src_height; // get image proportions
			$thmbnl_props = $thmbnl_width / $thmbnl_height; // get thumbnail proportions
			
			if($src_width > $thmbnl_width or $src_height > $thmbnl_height) // image dimensions greater than thumbnail dimensions?
			{
				if($src_width >= $src_height and $thmbnl_props <= $src_props) // width >= height, thumbnail proportions <= image proportions?
				{
					$width = $thmbnl_width; // use thumbnail width
					$height = ($thmbnl_width / $src_width) * $src_height;	// calc height (percentage)
				}
				else
				{
					$height = $thmbnl_height; // use thumbnail height
					$width = ($thmbnl_height / $src_height) * $src_width;	// calc width (percentage)			
				}
			}
			else // use image dimensions
			{
				$width = $src_width;
				$height = $src_height;
			}
			
			imagecopyresized($thumbnail, $src, ($thmbnl_width - $width) / 2, ($thmbnl_height - $height) / 2, 0, 0, $width, $height, $src_width, $src_height); // resize image and copy to thumbnail
			
			
			if($mode == 0) // save thumbnail width specified output filename
			{
				imagejpeg($thumbnail, $thmbnl_directory . $thmbnl_filename);
			}
			elseif($mode == 1) // display thumbnail
			{
				header("Content-Type: image/jpeg"); // send header
				imagejpeg($thumbnail); // ouput thumbnail
			}
			else // save & display thumbnail
			{
			
			}
		}
	}
?>

Aufruf wie folgt:

PHP:
	include("class_image_handler.php");
	
	$image_handler = new image_handler($_GET["src"]);
	$image_handler->thmbnl_dflt_width = 100;
	$image_handler->thmbnl_dflt_height = 100;
	$image_handler->thmbnl_dflt_bgcolor = "#000000";
	$image_handler->thmbnl_dflt_filename = "thumb.jpg";
	$image_handler->thmbnl_dflt_directory = "";
	$image_handler->create_thumbnail(1);

Was man auf jeden Fall noch ergänzen könnte wäre PNG-Unterstützung. Außerdem werden momentan mögliche Fehlermeldungen zum Großteil einfach unterdrückt.
 
Zurück