]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/ImageMagick/ImageMagickPlugin.php
Merge branch 'nightly', beginning of 1.2.x
[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 GIF resize support
38  *
39  * Comments:
40  *  Animated GIF resize requires setting $config['thumbnail']['animated'] = true;
41  *
42  * Bugs:
43  *  Not even ImageMagick is very good at resizing animated GIFs.
44  *  We are not infinitely fast, so resizing animated GIFs is _not_ recommended.
45  */
46
47 class ImageMagickPlugin extends Plugin
48 {
49     public $preview_imageformat = 'PNG';    // Image format strings: http://www.imagemagick.org/script/formats.php#supported
50     public $rasterize_vectors = false;       // Whether we want to turn SVG into PNG etc.
51
52     /**
53      * @param ImageFile $file An ImageFile object we're getting metadata for
54      * @param array $info The response from getimagesize()
55      */
56     public function onFillImageFileMetadata(ImageFile $imagefile) {
57         if (is_null($imagefile->animated) && $imagefile->mimetype === 'image/gif') {
58             $magick = new Imagick($imagefile->filepath);
59             $magick = $magick->coalesceImages();
60             $imagefile->animated = $magick->getNumberImages()>1;
61         }
62
63         return true;
64     }
65
66     public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box)
67     {
68         switch ($imagefile->mimetype) {
69         case 'image/gif':
70             // If GIF, then only for animated gifs! (and only if we really want to resize the animation!)
71             if ($imagefile->animated && common_config('thumbnail', 'animated')) {
72                 return $this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
73             }
74             break;
75         }
76         return true;
77     }
78
79     protected function resizeImageFileAnimatedGif(ImageFile $imagefile, $outpath, array $box)
80     {
81         $magick = new Imagick($imagefile->filepath);
82         $magick = $magick->coalesceImages();
83         $magick->setIteratorIndex(0);
84         do {
85             $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
86             $magick->thumbnailImage($box['width'], $box['height']);
87             $magick->setImagePage($box['width'], $box['height'], 0, 0);
88         } while ($magick->nextImage());
89         $magick = $magick->deconstructImages();
90
91         // $magick->writeImages($outpath, true); did not work, had to use filehandle
92         // There's been bugs for writeImages in php5-imagick before, probably now too
93         $fh = fopen($outpath, 'w+');
94         $success = $magick->writeImagesFile($fh);
95         fclose($fh);
96         $magick->destroy();
97
98         return !$success;
99     }
100
101     public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
102     {
103         switch ($file->mimetype) {
104         case 'image/svg+xml':
105             if (!$this->rasterize_vectors) {
106                 // ImageMagick seems to be hard to trick into scaling vector graphics...
107                 return true;
108             }
109             break;
110         default:
111             // If we don't know the format, let's try not to mess with anything.
112             return true;
113         }
114
115         $imgPath = tempnam(sys_get_temp_dir(), 'socialthumb-');
116         if (!$this->createImagePreview($file, $imgPath)) {
117             common_debug('Could not create ImageMagick preview of File id=='.$file->id);
118             @unlink($imgPath);
119             $imgPath = null;
120             return true;
121         }
122         return false;
123     }
124
125     protected function createImagePreview(File $file, $outpath)
126     {
127         $magick = new Imagick($file->getPath());
128         $magick->setImageFormat($this->preview_imageformat);
129         $magick->writeImage($outpath);
130         $magick->destroy();
131
132         return getimagesize($outpath);  // Verify that we wrote an understandable image.
133     }
134
135     public function onPluginVersion(&$versions)
136     {
137         $versions[] = array('name' => 'ImageMagick',
138                             'version' => GNUSOCIAL_VERSION,
139                             'author' => 'Mikael Nordfeldth',
140                             'homepage' => 'http://gnu.io/social',
141                             'rawdescription' =>
142                             // TRANS: Plugin description.
143                             _m('Use ImageMagick for some more image support.'));
144         return true;
145     }
146 }