Zend Framework Source Code Snippets

Filter that creates image thumbnail

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105
<?php
/**
 * Class for thumbnailing images
 *
 * @author Michał Bachowski (michal@bachowski.pl) * @package JPL
 * @subpackage   Jpl_Filter
 * @version 0.1
 * @uses Zend_Filter_Interface, IMagick
 * @license   http://framework.zend.com/license/new-bsd     New BSD License */
class Jpl_Filter_File_Image_Thumbnail {
    /**
     * Thumbnail width
     *     * @var integer
     */
    protected $_width   = null;
    /**
     * Thumbnail height     *
     * @var integer
     */
    protected $_height  = null;
    /**     * Information whether to keep ratio or not while making thumbnail
     *
     * @var bool
     */
    protected $_bestFit = true;    /**
     * Information whether crop image or just to resize it
     *
     * @var bool
     */    protected $_crop    = false;
    /**
     * Method sets destination thumbnail width
     *
     * @param  integer  $width     * @return Jpl_Filter_File_Image_Thumbnail
     */
    public function setWidth( $width = null ) {
        $this->_width = (int) $width;
        return $this;    }
    /**
     * Method sets destination thumbnail height
     *
     * @param  integer  $height     * @return Jpl_Filter_File_Image_Thumbnail
     */
    public function setHeight( $height = null ) {
        $this->_height = (int) $height;
        return $this;    }
    /**
     * Method changes behaviour of filter.
     * Filter will resize image exactly to given dimensions (false)
     * or resize image to fit given dimensions but keep original dimension ratio (true).     * Setting bestFit to true both dimensions are become mandatory!
     *
     * @param  bool     $bestFit
     * @return Jpl_Filter_File_Image_Thumbnail
     */    public function setBestFit( $bestFit = false ) {
        $this->_bestFit = (bool) $bestFit;
        return $this;
    }
    /**     * Method changes behaviour of filter.
     * Filter either just resizes image (false)
     * or resizes with keeping ratio and crop to best fit given width and height (true)
     * If true ommits self::$_bestFit attribute!
     *     * @param  bool $crop
     * @return Jpl_Filter_File_Image_Thumbnail
     */
    public function setCrop( $crop = false ) {
        $this->_crop = (bool) $crop;        return $this;
    }
    /**
     * Method filters given file - makes thumb
     *     * @param  string   $file   path to file
     * @return string   name of file
     */
    public function filter( $file ) {
        $im = new IMagick( $file );        if ( $this->_crop ) {
            $im->cropThumbnailImage(
                $this->_width,
                $this->_height
            );        } else {
            $im->thumbnailImage(
                $this->_width,
                $this->_height,
                $this->_bestFit            );
        }
        $im->writeImage( $file );
    }
}

Comments

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Class for thumbnailing images

Snippet details

Created:
MiB MiB
3 months ago
Edited:
MiB MiB
3 months ago
Revision Id:
153
Edit Message:
Initial Release
ZF Version
1.9.0
Tags:
filter image thumbnail
Comments:
0
Views:
1390
Points:
0 (0 votes)

History

r153

Initial Release

MiB MiB
3 months ago