]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ImageMagick/ImageMagickPlugin.php
resize_animated declared public in ImageMagickPlugin
[quix0rs-gnu-social.git] / plugins / ImageMagick / ImageMagickPlugin.php
1 <?php
2 /**
3  * GNU social - a federating social network
4  *
5  * Plugin to handle more kinds of image formats thanks to ImageMagick
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   GNUsocial
24  * @author    Mikael Nordfeldth <mmn@hethane.se>
25  * @copyright 2014 Free Software Foundation http://fsf.org
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      https://www.gnu.org/software/social/
28  */
29
30 if (!defined('GNUSOCIAL')) { exit(1); }
31
32 /*
33  * Dependencies:
34  *  php5-imagick
35  *
36  * Provides:
37  *  Animated image support (for image/gif at least)
38  *
39  * Bugs:
40  *  Not even ImageMagick is very good at resizing animated GIFs.
41  */
42
43 class ImageMagickPlugin extends Plugin
44 {
45     // configurable plugin options must be declared public
46     public $resize_animated = false;
47
48     /**
49      * @param ImageFile $file An ImageFile object we're getting metadata for
50      * @param array $info The response from getimagesize()
51      */
52     public function onFillImageFileMetadata(ImageFile $imagefile) {
53         switch ($imagefile->type) {
54         case IMAGETYPE_GIF:
55             $magick = new Imagick($imagefile->filepath);
56             $magick = $magick->coalesceImages();
57             $imagefile->animated = $magick->getNumberImages()>1;
58             return false;
59         }
60
61         return true;
62     }
63
64     public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box) {
65         // So far we only take over the resize for IMAGETYPE_GIF
66         // (and only animated for gifs!)
67         if ($imagefile->type == IMAGETYPE_GIF && $imagefile->animated) {
68             if (!$this->resize_animated) {
69                 // Don't resize if not desired, due to CPU/time limitations
70                 return false;
71             }
72             $magick = new Imagick($imagefile->filepath);
73             $magick = $magick->coalesceImages();
74             $magick->setIteratorIndex(0);
75             do {
76                 $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
77                 $magick->thumbnailImage($box['width'], $box['height']);
78                 $magick->setImagePage($box['width'], $box['height'], 0, 0);
79             } while ($magick->nextImage());
80             $magick = $magick->deconstructImages();
81
82             // $magick->writeImages($outpath, true); did not work, had to use filehandle
83             // There's been bugs for writeImages in php5-imagick before, probably now too
84             $fh = fopen($outpath, 'w+');
85             $success = $magick->writeImagesFile($fh);
86             fclose($fh);
87
88             return !$success;
89         }
90         return true;
91     }
92
93     public function onPluginVersion(&$versions)
94     {
95         $versions[] = array('name' => 'ImageMagick',
96                             'version' => GNUSOCIAL_VERSION,
97                             'author' => 'Mikael Nordfeldth',
98                             'homepage' => 'http://gnu.io/social',
99                             'rawdescription' =>
100                             // TRANS: Plugin description.
101                             _m('Use ImageMagick for better image support.'));
102         return true;
103     }
104 }
105