]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ImageMagick/ImageMagickPlugin.php
No need for ImageMagick to detected animated GIF
[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  * Suggestions:
40  *  After enabling the ImageMagick plugin, you might want to regenerate
41  *  thumbnails for the newly support formats. This is easiest done with
42  *  a script in the GNU social-bundled scripts directory:
43  *  $ php scripts/clean_thumbnails.php
44  *
45  * Bugs:
46  *  Not even ImageMagick is very good at resizing animated GIFs.
47  */
48
49 class ImageMagickPlugin extends Plugin
50 {
51     // configurable plugin options must be declared public
52     public $resize_animated = null;
53
54     public function initialize()
55     {
56         parent::initialize();
57
58         if (is_null($this->resize_animated)) {
59             $this->resize_animated = common_config('image', 'resize_animated');
60         }
61     }
62
63     /**
64      * @param ImageFile $file An ImageFile object we're getting metadata for
65      * @param array $info The response from getimagesize()
66      */
67     public function onFillImageFileMetadata(ImageFile $imagefile) {
68         if (is_null($imagefile->animated) && $imagefile->type === IMAGETYPE_GIF) {
69             $magick = new Imagick($imagefile->filepath);
70             $magick = $magick->coalesceImages();
71             $imagefile->animated = $magick->getNumberImages()>1;
72         }
73
74         return true;
75     }
76
77     public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box) {
78         // So far we only take over the resize for IMAGETYPE_GIF
79         // (and only animated for gifs!)
80         if ($imagefile->type == IMAGETYPE_GIF && $imagefile->animated) {
81             if (!$this->resize_animated) {
82                 // Don't resize if not desired, due to CPU/time limitations
83                 return false;
84             }
85             $magick = new Imagick($imagefile->filepath);
86             $magick = $magick->coalesceImages();
87             $magick->setIteratorIndex(0);
88             do {
89                 $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
90                 $magick->thumbnailImage($box['width'], $box['height']);
91                 $magick->setImagePage($box['width'], $box['height'], 0, 0);
92             } while ($magick->nextImage());
93             $magick = $magick->deconstructImages();
94
95             // $magick->writeImages($outpath, true); did not work, had to use filehandle
96             // There's been bugs for writeImages in php5-imagick before, probably now too
97             $fh = fopen($outpath, 'w+');
98             $success = $magick->writeImagesFile($fh);
99             fclose($fh);
100
101             return !$success;
102         }
103         return true;
104     }
105
106     public function onPluginVersion(&$versions)
107     {
108         $versions[] = array('name' => 'ImageMagick',
109                             'version' => GNUSOCIAL_VERSION,
110                             'author' => 'Mikael Nordfeldth',
111                             'homepage' => 'http://gnu.io/social',
112                             'rawdescription' =>
113                             // TRANS: Plugin description.
114                             _m('Use ImageMagick for better image support.'));
115         return true;
116     }
117 }
118