phpThumb Helper for CakePHP


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * phpThumb Helper
  4.  *
  5.  * @author Stefan Zollinger
  6.  * @license GPL
  7.  *
  8.  * options:
  9.  * w = max width of output thumbnail in pixels
  10.  * h = max height of output thumbnail in pixels
  11.  * wp = max width for portrait images
  12.  * hp = max height for portrait images
  13.  * wl = max width for landscape images
  14.  * hl = max height for landscape images
  15.  * ws = max width for square images
  16.  * hs = max height for square images
  17.  * f = output image format ("jpeg", "png", or "gif")
  18.  * q = JPEG compression (1=worst, 95=best, 75=default)
  19.  * sx = left side of source rectangle (default = 0)
  20.  * (values 0 < sx < 1 represent percentage)
  21.  * sy = top side of source rectangle (default = 0)
  22.  * (values 0 < sy < 1 represent percentage)
  23.  * sw = width of source rectangle (default = fullwidth)
  24.  * (values 0 < sw < 1 represent percentage)
  25.  * sh = height of source rectangle (default = fullheight)
  26.  * (values 0 < sh < 1 represent percentage)
  27.  * zc = zoom-crop. Will auto-crop off the larger dimension
  28.  * so that the image will fill the smaller dimension
  29.  * (requires both "w" and "h", overrides "iar", "far")
  30.  * Set to "1" or "C" to zoom-crop towards the center,
  31.  * or set to "T", "B", "L", "R", "TL", "TR", "BL", "BR"
  32.  * to gravitate towards top/left/bottom/right directions
  33.  * (requies ImageMagick for values other than "C" or "1")
  34.  * bg = background hex color (default = FFFFFF)
  35.  * bc = border hex color (default = 000000)*
  36.  *
  37.  *
  38.  */
  39.  
  40. class ThumbHelper extends Helper {
  41.  
  42. var $defaults = array('q'=>80, 'zc'=>'C' );
  43.  
  44. function __construct(){
  45. $this->thumbPath = parent::url('/phpthumb/phpThumb.php');
  46.  
  47.  
  48. }
  49.  
  50. function image($img, $options=array(), $attributes=array()){
  51.  
  52. if(!empty($options['w'])){
  53. $width = "width=\"{$options['w']}\"";
  54. }else{
  55. $width = '';
  56. }
  57. if(!empty($options['h'])){
  58. $height = "height=\"{$options['h']}\"";
  59. }else{
  60. $height = '';
  61. }
  62. $attrs = '';
  63. foreach($attributes as $key=>$value){
  64. $attrs .= ' '.$key.'="'.$value.'"';
  65. }
  66.  
  67. $src = $this->url($img, $options);
  68. $output = "<img {$attrs} src=\"{$src}\" {$width} {$height} />";
  69.  
  70. return $output;
  71. }
  72.  
  73. function url($img, $options=array()){
  74.  
  75. $options = array_merge($this->defaults, $options);
  76. $urlOpt = htmlentities(http_build_query($options));
  77.  
  78. App::import('Helper', 'Html');
  79. $html = new HtmlHelper();
  80. if(substr($img, 0, 1) != '/'){
  81. $img = '/img/'.$img;
  82. }
  83. $src = parent::url($img);
  84. $output = $this->thumbPath . '?src='.$src .'&amp;'.$urlOpt;
  85. return $output;
  86. }
  87.  
  88. }
  89. ?>

URL: phpthumbhelper

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.