deving.de

Hello World!
PHP

Coding, PHP, , , , ,

PHP: Thumbnail erstellen Funktion (Crop und Resize)

Vorschaubilder (Thumbnails) mit PHP erzeugen

Hier ein kleines Snippet bzw. eine PHP Funktion um Thumbnails für Bilder zu erstellen, wahlweise mit Crop oder Resize Funktion :)

<?php
/**
 * Create Thumbnails
 *
 * @param string $filename 
 * @param string $filenameThumb 
 * @param int $thumbSizeWidth 
 * @param int $thumbSizeHeight 
 * @param bool $crop 
 * @return bool 
 */
function mkthumb($filename, $filenameThumb, $thumbSizeWidth = 100, $thumbSizeHeight = 100, $crop = false) 
{
	//Set Filenames / Folders
	$srcFile = $filename;
	$thumbFile = $filenameThumb;
	
	//Determine the File Type
	$type = substr($srcFile, strrpos($srcFile, '.') + 1);
	
	//Create the Source Image
	switch ($type) 
	{
		case 'jpg':
			$src = imagecreatefromjpeg($srcFile);
			break;

		case 'jpeg':
			$src = imagecreatefromjpeg($srcFile);
			break;

		case 'png':
			$src = imagecreatefrompng($srcFile);
			break;

		case 'gif':
			$src = imagecreatefromgif($srcFile);
			break;
	}

	//Determine the Image Dimensions
	$oldWidth = imagesx($src);
	$oldHeight = imagesy($src);

	//Crop: Calculate the New Image Dimensions
	if ($crop)
	{
		$original_aspect = $oldWidth / $oldHeight;
		$thumb_aspect = $thumbSizeWidth / $thumbSizeHeight;

		 //If image is wider than thumbnail (in aspect ratio sense)
		if ($original_aspect >= $thumb_aspect)
		{
		   $newHeight = $thumbSizeHeight;
		   $newWidth = $oldWidth / ($oldHeight / $thumbSizeHeight);
		}
		//If the thumbnail is wider than the image
		else
		{
		   $newWidth = $thumbSizeWidth;
		   $newHeight = $oldHeight / ($oldWidth / $thumbSizeWidth);
		}

		//Create the New Image
		$new = imagecreatetruecolor($thumbSizeWidth, $thumbSizeHeight);

		//Transcribe the Source Image into the New (Square) Image
		imagecopyresampled($new, $src, 0 - ($newWidth - $thumbSizeWidth) / 2, 0 - ($newHeight - $thumbSizeHeight) / 2, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);
	}
	//Resize: Calculate the New Image Dimensions
	else
	{
		//If image is wider than thumbnail (in aspect ratio sense)
		if ($oldWidth >= $oldHeight)
		{
			$newWidth = $thumbSizeWidth;
			$newHeight = $oldHeight * $thumbSizeWidth / $oldWidth;
		}
		//If the thumbnail is wider than the image
		else
		{
			$newHeight = $thumbSizeHeight;
			$newWidth = $oldWidth * $thumbSizeHeight / $oldHeight;
		}

		//Create the New Image
		$new = imagecreatetruecolor($newWidth, $newHeight);

		//Transcribe the Source Image into the New Image
		imagecopyresampled($new, $src, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight);
	}

	switch ($type) 
	{
		case 'jpg':
			$src = imagejpeg($new, $thumbFile, 80);
			break;

		case 'jpeg':
			$src = imagejpeg($new, $thumbFile, 80);
			break;

		case 'png':
			$src = imagepng($new, $thumbFile, 80);
			break;

		case 'gif':
			$src = imagegif($new, $thumbFile, 80);
			break;
	}

	imagedestroy($new);

	return $src;
}

//Example Resize
mkthumb('test.jpg', 'test_thumb.jpg', 1024, 1024, false);

//Example Crop
mkthumb('test.jpg', 'test_thumb.jpg', 200, 150, true);
?>

The following two tabs change content below.
Theodoros
Hello World! Begeisterter Webentwickler & SEO :)

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*
*