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 GIF resize support
40 * Animated GIF resize requires setting $config['thumbnail']['animated'] = true;
43 * Not even ImageMagick is very good at resizing animated GIFs.
44 * We are not infinitely fast, so resizing animated GIFs is _not_ recommended.
47 class ImageMagickPlugin extends Plugin
49 public $preview_imageformat = 'PNG'; // Image format strings: http://www.imagemagick.org/script/formats.php#supported
52 * @param ImageFile $file An ImageFile object we're getting metadata for
53 * @param array $info The response from getimagesize()
55 public function onFillImageFileMetadata(ImageFile $imagefile) {
56 if (is_null($imagefile->animated) && $imagefile->mimetype === 'image/gif') {
57 $magick = new Imagick($imagefile->filepath);
58 $magick = $magick->coalesceImages();
59 $imagefile->animated = $magick->getNumberImages()>1;
65 public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box)
67 switch ($imagefile->mimetype) {
69 // If GIF, then only for animated gifs! (and only if we really want to resize the animation!)
70 if ($imagefile->animated && common_config('thumbnail', 'animated')) {
71 return $this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
78 protected function resizeImageFileAnimatedGif(ImageFile $imagefile, $outpath, array $box)
80 $magick = new Imagick($imagefile->filepath);
81 $magick = $magick->coalesceImages();
82 $magick->setIteratorIndex(0);
84 $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
85 $magick->thumbnailImage($box['width'], $box['height']);
86 $magick->setImagePage($box['width'], $box['height'], 0, 0);
87 } while ($magick->nextImage());
88 $magick = $magick->deconstructImages();
90 // $magick->writeImages($outpath, true); did not work, had to use filehandle
91 // There's been bugs for writeImages in php5-imagick before, probably now too
92 $fh = fopen($outpath, 'w+');
93 $success = $magick->writeImagesFile($fh);
100 public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
102 switch ($file->mimetype) {
103 case 'image/svg+xml':
104 // Let's save our frame to a temporary file. If we fail, remove it.
105 $imgPath = tempnam(sys_get_temp_dir(), 'socialthumb-');
106 if (!$this->createImagePreview($file, $imgPath)) {
107 common_debug('Could not create ImageMagick preview of File id=='.$file->id);
117 protected function createImagePreview(File $file, $outpath)
119 $magick = new Imagick($file->getPath());
120 $magick->setImageFormat($this->preview_imageformat);
121 $magick->writeImage($outpath);
124 return getimagesize($outpath); // Verify that we wrote an understandable image.
127 public function onPluginVersion(&$versions)
129 $versions[] = array('name' => 'ImageMagick',
130 'version' => GNUSOCIAL_VERSION,
131 'author' => 'Mikael Nordfeldth',
132 'homepage' => 'http://gnu.io/social',
134 // TRANS: Plugin description.
135 _m('Use ImageMagick for some more image support.'));