3 * GNU social - a federating social network
5 * Plugin to handle more kinds of image formats thanks to ImageMagick
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.
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.
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/>.
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/
30 if (!defined('GNUSOCIAL')) { exit(1); }
37 * Animated image support (for image/gif at least)
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
46 * Not even ImageMagick is very good at resizing animated GIFs.
49 class ImageMagickPlugin extends Plugin
51 // configurable plugin options must be declared public
52 public $resize_animated = null;
54 public function initialize()
58 if (is_null($this->resize_animated)) {
59 $this->resize_animated = common_config('image', 'resize_animated');
64 * @param ImageFile $file An ImageFile object we're getting metadata for
65 * @param array $info The response from getimagesize()
67 public function onFillImageFileMetadata(ImageFile $imagefile) {
68 switch ($imagefile->type) {
70 $magick = new Imagick($imagefile->filepath);
71 $magick = $magick->coalesceImages();
72 $imagefile->animated = $magick->getNumberImages()>1;
79 public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box) {
80 // So far we only take over the resize for IMAGETYPE_GIF
81 // (and only animated for gifs!)
82 if ($imagefile->type == IMAGETYPE_GIF && $imagefile->animated) {
83 if (!$this->resize_animated) {
84 // Don't resize if not desired, due to CPU/time limitations
87 $magick = new Imagick($imagefile->filepath);
88 $magick = $magick->coalesceImages();
89 $magick->setIteratorIndex(0);
91 $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
92 $magick->thumbnailImage($box['width'], $box['height']);
93 $magick->setImagePage($box['width'], $box['height'], 0, 0);
94 } while ($magick->nextImage());
95 $magick = $magick->deconstructImages();
97 // $magick->writeImages($outpath, true); did not work, had to use filehandle
98 // There's been bugs for writeImages in php5-imagick before, probably now too
99 $fh = fopen($outpath, 'w+');
100 $success = $magick->writeImagesFile($fh);
108 public function onPluginVersion(&$versions)
110 $versions[] = array('name' => 'ImageMagick',
111 'version' => GNUSOCIAL_VERSION,
112 'author' => 'Mikael Nordfeldth',
113 'homepage' => 'http://gnu.io/social',
115 // TRANS: Plugin description.
116 _m('Use ImageMagick for better image support.'));