PHP 用于CakePHP的phpThumb助手

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 用于CakePHP的phpThumb助手相关的知识,希望对你有一定的参考价值。

<?php
/**
 * phpThumb Helper
 * 
 * @author Stefan Zollinger
 * @license GPL
 * 
 * options:
 *  w = max width of output thumbnail in pixels
 *  h = max height of output thumbnail in pixels
 * wp = max width for portrait images
 * hp = max height for portrait images
 * wl = max width for landscape images
 * hl = max height for landscape images
 * ws = max width for square images
 * hs = max height for square images
 *  f = output image format ("jpeg", "png", or "gif")
 *  q = JPEG compression (1=worst, 95=best, 75=default)
 * sx = left side of source rectangle (default = 0)
 *      (values 0 < sx < 1 represent percentage)
 * sy = top side of source rectangle (default = 0)
 *      (values 0 < sy < 1 represent percentage)
 * sw = width of source rectangle (default = fullwidth)
 *      (values 0 < sw < 1 represent percentage)
 * sh = height of source rectangle (default = fullheight)
 *      (values 0 < sh < 1 represent percentage)
 * zc = zoom-crop. Will auto-crop off the larger dimension
 *      so that the image will fill the smaller dimension
 *      (requires both "w" and "h", overrides "iar", "far")
 *      Set to "1" or "C" to zoom-crop towards the center,
 *      or set to "T", "B", "L", "R", "TL", "TR", "BL", "BR"
 *      to gravitate towards top/left/bottom/right directions
 *      (requies ImageMagick for values other than "C" or "1")
 * bg = background hex color (default = FFFFFF)
 * bc = border hex color (default = 000000)*
 *
 *
 */

class ThumbHelper extends Helper  {
	
	var $defaults = array('q'=>80, 'zc'=>'C' );
	 
	function __construct(){
		$this->thumbPath = parent::url('/phpthumb/phpThumb.php');
		
		
	}

	function image($img, $options=array(), $attributes=array()){
		
		if(!empty($options['w'])){
			$width = "width=\"{$options['w']}\"";
		}else{
			$width = '';
		}
		if(!empty($options['h'])){
			$height = "height=\"{$options['h']}\"";
		}else{
			$height = '';
		}
		$attrs = '';
		foreach($attributes as $key=>$value){
			$attrs .= ' '.$key.'="'.$value.'"';
		}
		
		$src = $this->url($img, $options);
		$output = "<img {$attrs} src=\"{$src}\" {$width} {$height}  />";
		
		return $output;
	}
	
	function url($img, $options=array()){
		
		$options = array_merge($this->defaults, $options);
		$urlOpt = htmlentities(http_build_query($options));
		
		App::import('Helper', 'Html');
		$html = new HtmlHelper();
		if(substr($img, 0, 1) != '/'){
			$img = '/img/'.$img;
		}
		$src = parent::url($img);
		$output = $this->thumbPath . '?src='.$src .'&'.$urlOpt;
		return $output;
	}
	
}
?>

以上是关于PHP 用于CakePHP的phpThumb助手的主要内容,如果未能解决你的问题,请参考以下文章

PHP CakePHP导入助手

PHP CakePHP - 通过小部件助手进行邮件查看

Cakephp 在帮助器中调用组件方法

phpthumb 不适用于 bmp 文件

PHPthumb不起作用-不显示图像

如何让phpThumb识别并使用ImageMagick安装?