From 97496896a60735f27de0f078824893638eedc0ca Mon Sep 17 00:00:00 2001 From: Matthieu Sadouni Date: Mon, 26 Sep 2011 16:07:08 +0200 Subject: [PATCH] adding an url() method and HTML options to thumbnail() --- readme.md | 26 ++++++++++++++++++++++++++ views/helpers/php_thumb.php | 22 ++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 1f8c364..efc87b8 100644 --- a/readme.md +++ b/readme.md @@ -67,6 +67,14 @@ In the view, generate the thumbnail : In the `$options` array you can use any valid phpThumb() option. For detailed information on the available options, check [phpThumb()'s readme](http://phpthumb.sourceforge.net/demo/docs/phpthumb.readme.txt) +You can also pass HTML options : + + echo $this->PhpThumb->thumbnail( + 'img/image.jpg', + array('w' => 100, 'h' => 100, 'zc' => 1), + array('alt' => "Alternative text") + ); + ### MeioUpload'ed image // for a Post with an image field containing my-post.jpg @@ -80,3 +88,21 @@ This will also work : echo $this->PhpThumb->thumbnail('uploads/post/image/.$post['Post']['image'], array( 'w' => 100, 'h' => 100, 'zc' => 1 )); + +### Image url + +To get only the image url, use the `url` method : + + echo $this->PhpThumb->url('img/image.jpg', array( + 'w' => 100, 'h' => 100, 'zc' => 1 + )); + +It's useful for the various Javascript gallery script, like Lightbox / Thickbox / Colorbox etc. : + + echo $this->Html->link( + $this->PhpThumb->thumbnail( + 'img/image.jpeg', array('w' => 100, 'h' => 100, 'zc' => 1) + ), + $this->PhpThumb->url('img/image.jpeg', array('w' => 640, 'h' => 640)), + array('escape' => false) + ); diff --git a/views/helpers/php_thumb.php b/views/helpers/php_thumb.php index 109a165..69cf20b 100644 --- a/views/helpers/php_thumb.php +++ b/views/helpers/php_thumb.php @@ -122,8 +122,8 @@ public function generate($options = array()) { return $this->get_thumb_data(); } - - function thumbnail($image, $options) { + + function generateThumbnail($image, $options) { $thumbs_path = Configure::read('PhpThumb.thumbs_path'); if (empty($thumbs_path)) { return false; @@ -155,10 +155,20 @@ function thumbnail($image, $options) { $options['src'] = WWW_ROOT . $image; } $finalOptions = array_merge($options, $pathOptions); - $thumbnail = $this->generate($finalOptions); - return $this->image($thumbnail['src'], array( - 'width' => $thumbnail['w'], 'height' => $thumbnail['h']) - ); + return $this->generate($finalOptions); + } + + function url($image, $options) { + $thumbnail = $this->generateThumbnail($image, $options); + return $thumbnail['src']; + } + + function thumbnail($image, $options, $htmlOptions = array()) { + $thumbnail = $this->generateThumbnail($image, $options); + return $this->image($thumbnail['src'], array_merge( + array('width' => $thumbnail['w'], 'height' => $thumbnail['h']), + $htmlOptions + )); } } ?>