Генерация превью изображения

Материал из Umicms

Перейти к: навигация, поиск

Пример использования:

<img src="%custom makeThumbnailFull('./images/cms/data/big1.jpg', '165',
'200', 'info')%" />

Код (см. ниже) вставляется в custom.php


Пример генерации превью:

public function makeThumbnailFull($path, $width, $height, $template = "default", $returnArrayOnly = false, $crop = true, $cropside = 5, $isLogo = false)

Пример наложения логотипа (необходим полупрозрачный png):

private function makeThumbnailFullPlaceLogo($image)


Пример добавления резкости после ресайза:

private function makeThumbnailFullUnsharpMask($img, $amount, $radius, $threshold)


	private function makeThumbnailFullUnsharpMask($img, $amount, $radius, $threshold)
		{
 
		if (function_exists('UnsharpMask')){return UnsharpMask($img, $amount, $radius, $threshold);}
			else{
 
			// Attempt to calibrate the parameters to Photoshop:
			if ($amount > 500) $amount = 500;
			$amount = $amount * 0.016;
			if ($radius > 50) $radius = 50;
			$radius = $radius * 2;
			if ($threshold > 255) $threshold = 255;
 
			$radius = abs(round($radius)); 	// Only integers make sense.
			if ($radius == 0) {	return $img; imagedestroy($img); break;	}
			$w = imagesx($img); $h = imagesy($img);
			$imgCanvas = $img;
			$imgCanvas2 = $img;
			$imgBlur = imagecreatetruecolor($w, $h);
 
			// Gaussian blur matrix:
			//	1	2	1
			//	2	4	2
			//	1	2	1
 
			// Move copies of the image around one pixel at the time and merge them with weight
			// according to the matrix. The same matrix is simply repeated for higher radii.
			for ($i = 0; $i < $radius; $i++)
				{
				imagecopy	  ($imgBlur, $imgCanvas, 0, 0, 1, 1, $w - 1, $h - 1); // up left
				imagecopymerge ($imgBlur, $imgCanvas, 1, 1, 0, 0, $w, $h, 50); // down right
				imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 1, 0, $w - 1, $h, 33.33333); // down left
				imagecopymerge ($imgBlur, $imgCanvas, 1, 0, 0, 1, $w, $h - 1, 25); // up right
				imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 1, 0, $w - 1, $h, 33.33333); // left
				imagecopymerge ($imgBlur, $imgCanvas, 1, 0, 0, 0, $w, $h, 25); // right
				imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 20 ); // up
				imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 16.666667); // down
				imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 0, $w, $h, 50); // center
				}
			$imgCanvas = $imgBlur;
 
			// Calculate the difference between the blurred pixels and the original
			// and set the pixels
			for ($x = 0; $x < $w; $x++)
				{ // each row
				for ($y = 0; $y < $h; $y++)
					{ // each pixel
					$rgbOrig = ImageColorAt($imgCanvas2, $x, $y);
					$rOrig = (($rgbOrig >> 16) & 0xFF);
					$gOrig = (($rgbOrig >> 8) & 0xFF);
					$bOrig = ($rgbOrig & 0xFF);
					$rgbBlur = ImageColorAt($imgCanvas, $x, $y);
					$rBlur = (($rgbBlur >> 16) & 0xFF);
					$gBlur = (($rgbBlur >> 8) & 0xFF);
					$bBlur = ($rgbBlur & 0xFF);
 
					// When the masked pixels differ less from the original
					// than the threshold specifies, they are set to their original value.
					$rNew = (abs($rOrig - $rBlur) >= $threshold) ? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig)) : $rOrig;
					$gNew = (abs($gOrig - $gBlur) >= $threshold) ? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig)) : $gOrig;
					$bNew = (abs($bOrig - $bBlur) >= $threshold) ? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig)) : $bOrig;
 
					if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew))
						{
						$pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);
						ImageSetPixel($img, $x, $y, $pixCol);
						}
					}
				}
			return $img;
		}
 
	}
 
 
 
 
 
 
 
 
 
 
 
 
		//place logo
		private function makeThumbnailFullPlaceLogo($image) {
		$logo_full_file_name="./images/cms/thumbs_logo.png";  // файл логотип
		$type_placment=3; // тип размещения (1 - левый верхни, 2 - правый верхнй, 3 - правый нижний, 4 - левый нижний, 5 - центр)
		$x=5; // координата размещения - Х
		$y=5; // координата размещения - Y
 
		$image_width=imagesx($image);
		$image_height=imagesy($image);
 
 
 
		if (file_exists($logo_full_file_name)) {
 
			$logoFile = new umiImageFile($logo_full_file_name);
			if(($image_width < ($logoFile->getWidth()+$x)) || ($image_height < ($logoFile->getHeight()+$y)) ){return $image;}
 
			$logo=@imagecreatefrompng($logo_full_file_name);
			if ($logo) {
				$logo_width=imagesx($logo);
				$logo_height=imagesy($logo);
				switch ($type_placment) {
					case 2:
						$x=$image_width-$x-$logo_width;
						break;
					case 3:
						$x=$image_width-$x-$logo_width;
						$y=$image_height-$y-$logo_height;
						break;
					case 4:
						$y=$image_height-$y-$logo_height;
						break;
					case 5:
						$x=floor($image_width/2-$logo_width/2);
						$y=floor($image_height/2-$logo_height/2);
						break;
					default:
				}
				imagecopy($image,$logo,$x,$y,0,0,$logo_width,$logo_height);
			}
		}
 
		return $image;
		}
 
 
 
		public $thumbs_path = "./images/cms/thumbs/";
		public function makeThumbnailFull($path, $width, $height, $template = "default", $returnArrayOnly = false, $crop = true, $cropside = 5, $isLogo = false) {
		if(!$template) $template = "default";
 
		$quality = 80;
		$isSharpen=true;
 
		$image = new umiImageFile($path);
		$file_name = $image->getFileName();
		$file_ext = $image->getExt();
 
		$file_ext = strtolower($file_ext);
		$allowedExts = Array('gif', 'jpeg', 'jpg', 'png', 'bmp');
		if(!in_array($file_ext, $allowedExts)) return "";
 
		$file_modified 	= filemtime($path);
		$file_name_new = md5($path.$width.$height.$crop.$cropside.$isLogo)."." . $file_ext;
		$path_new = $this->thumbs_path . $file_name_new;
 
 
		if(!is_file($path_new)) {
			$width_src = $image->getWidth();
			$height_src = $image->getHeight();
 
				if($height == "auto") {
					$real_height = (int) round($height_src * ($width / $width_src));
					$real_width = (int) $width;
				} else {
					if($width == "auto") {
							$real_width = (int) round($width_src * ($height / $height_src));
					} else {
						$real_width = (int) $width;
					}
 
					$real_height = (int) $height;
				}
 
				$offset_h=0;
				$offset_w=0;
 
				if($crop)
					{
					$width_ratio = $width_src/$width;
					$height_ratio = $height_src/$height;
 
					if ($width_ratio > $height_ratio)
						{
						$offset_w = round(($width_src-$width*$height_ratio)/2);
						$width_src = round($width*$height_ratio);
						}
					elseif ($width_ratio < $height_ratio)
						{
						$offset_h = round(($height_src-$height*$width_ratio)/2);
						$height_src = round($height*$width_ratio);
						}
 
 
				if($cropside)
					{
						//defore all it was cropside work like as - 5
						//123
						//456
						//789
						switch ($cropside):
						    case 1:
								$offset_w = 0;
								$offset_h = 0;
						        break;
						    case 2:
								$offset_h = 0;
						        break;
						    case 3:
								$offset_w += $offset_w;
								$offset_h = 0;
						        break;
						    case 4:
								$offset_w = 0;
						        break;
						    case 5:
						        break;
						    case 6:
								$offset_w += $offset_w;
						        break;
						    case 7:
								$offset_w = 0;
								$offset_h += $offset_h;
						        break;
						    case 8:
								$offset_h += $offset_h;
						        break;
						    case 9:
								$offset_w += $offset_w;
								$offset_h += $offset_h;
						        break;
						endswitch;
 
					}
 
 
 
					}
 
 
 
				$thumb = imagecreatetruecolor($real_width, $real_height);
 
				if ($image->getExt() == "gif")	{ $source = imagecreatefromgif($path);}
				else if ($image->getExt() == "png")	{ $source = imagecreatefrompng($path);}
				else { 	$source = imagecreatefromjpeg($path); }
 
				if ($width*4 < $width_src AND $height*4 < $height_src)
					{
					$_TMP=array();
					$_TMP['width'] = round($width*4);
					$_TMP['height'] = round($height*4);
 
					$_TMP['image'] = imagecreatetruecolor($_TMP['width'], $_TMP['height']);
					imagecopyresized($_TMP['image'], $source, 0, 0, $offset_w, $offset_h, $_TMP['width'], $_TMP['height'], $width_src, $height_src);
					$source = $_TMP['image'];
					$width_src = $_TMP['width'];
					$height_src = $_TMP['height'];
 
					$offset_w = 0;
					$offset_h = 0;
					unset($_TMP);
					}
 
				imagecopyresampled($thumb, $source, 0, 0, $offset_w, $offset_h, $width, $height, $width_src, $height_src);
 
				if($isLogo) {$thumb=$this->makeThumbnailFullPlaceLogo($thumb);}
				if($isSharpen) $thumb = $this->makeThumbnailFullUnsharpMask($thumb,80,.5,3);
 
 
				if($image->getExt() == "png") {
					imagepng($thumb, $path_new);
				} else if($image->getExt() == "gif") {
					imagegif($thumb, $path_new);
				} else {
					imagejpeg($thumb, $path_new, $quality);
				}
			}
 
 
 
 
		//Parsing
		$value = new umiImageFile($path_new);
 
		$arr = Array();
		$arr['size'] = $value->getSize();
		$arr['filename'] = $value->getFileName();
		$arr['filepath'] = $value->getFilePath();
		$arr['src'] = $value->getFilePath(true);
		$arr['ext'] = $value->getExt();
 
		$arr['width'] = $value->getWidth();
		$arr['height'] = $value->getHeight();
 
		$arr['template'] = $template;
 
		if(cmsController::getInstance()->getCurrentMode() == "admin") {
			$arr['src'] = str_replace("&", "&amp;", $arr['src']);
		}
// с
		if($returnArrayOnly) {
			return $arr;
		} else {
			list($tpl) = def_module::loadTemplates("tpls/thumbs/{$template}.tpl", "image");
			return def_module::parseTemplate($tpl, $arr);
		}
	}


Copyright artcifra@gmail.com

Личные инструменты