ImageMagickPlugin.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /*
  31. * Dependencies:
  32. * php5-imagick
  33. *
  34. * Provides:
  35. * Animated GIF resize support
  36. *
  37. * Comments:
  38. * Animated GIF resize requires setting $config['thumbnail']['animated'] = true;
  39. *
  40. * Bugs:
  41. * Not even ImageMagick is very good at resizing animated GIFs.
  42. * We are not infinitely fast, so resizing animated GIFs is _not_ recommended.
  43. */
  44. class ImageMagickPlugin extends Plugin
  45. {
  46. /**
  47. * @param ImageFile $file An ImageFile object we're getting metadata for
  48. * @param array $info The response from getimagesize()
  49. */
  50. public function onFillImageFileMetadata(ImageFile $imagefile) {
  51. if (is_null($imagefile->animated) && $imagefile->type === IMAGETYPE_GIF) {
  52. $magick = new Imagick($imagefile->filepath);
  53. $magick = $magick->coalesceImages();
  54. $imagefile->animated = $magick->getNumberImages()>1;
  55. }
  56. return true;
  57. }
  58. public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box) {
  59. // So far we only take over the resize for IMAGETYPE_GIF
  60. // (and only animated for gifs! (and only if we really want to resize the animation!))
  61. if ($imagefile->type == IMAGETYPE_GIF && $imagefile->animated && common_config('thumbnail', 'animated')) {
  62. $magick = new Imagick($imagefile->filepath);
  63. $magick = $magick->coalesceImages();
  64. $magick->setIteratorIndex(0);
  65. do {
  66. $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
  67. $magick->thumbnailImage($box['width'], $box['height']);
  68. $magick->setImagePage($box['width'], $box['height'], 0, 0);
  69. } while ($magick->nextImage());
  70. $magick = $magick->deconstructImages();
  71. // $magick->writeImages($outpath, true); did not work, had to use filehandle
  72. // There's been bugs for writeImages in php5-imagick before, probably now too
  73. $fh = fopen($outpath, 'w+');
  74. $success = $magick->writeImagesFile($fh);
  75. fclose($fh);
  76. return !$success;
  77. }
  78. return true;
  79. }
  80. public function onPluginVersion(&$versions)
  81. {
  82. $versions[] = array('name' => 'ImageMagick',
  83. 'version' => GNUSOCIAL_VERSION,
  84. 'author' => 'Mikael Nordfeldth',
  85. 'homepage' => 'http://gnu.io/social',
  86. 'rawdescription' =>
  87. // TRANS: Plugin description.
  88. _m('Use ImageMagick for some more image support.'));
  89. return true;
  90. }
  91. }