ImageMagickPlugin.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. const PLUGIN_VERSION = '2.0.0';
  47. public $preview_imageformat = 'PNG'; // Image format strings: http://www.imagemagick.org/script/formats.php#supported
  48. public $rasterize_vectors = false; // Whether we want to turn SVG into PNG etc.
  49. /**
  50. * @param ImageFile $file An ImageFile object we're getting metadata for
  51. * @param array $info The response from getimagesize()
  52. */
  53. public function onFillImageFileMetadata(ImageFile $imagefile) {
  54. if (is_null($imagefile->animated) && $imagefile->mimetype === 'image/gif') {
  55. $magick = new Imagick($imagefile->filepath);
  56. $magick = $magick->coalesceImages();
  57. $imagefile->animated = $magick->getNumberImages()>1;
  58. }
  59. return true;
  60. }
  61. public function onStartResizeImageFile(ImageFile $imagefile, $outpath, array $box)
  62. {
  63. switch ($imagefile->mimetype) {
  64. case 'image/gif':
  65. // If GIF, then only for animated gifs! (and only if we really want to resize the animation!)
  66. if ($imagefile->animated && common_config('thumbnail', 'animated')) {
  67. return $this->resizeImageFileAnimatedGif($imagefile, $outpath, $box);
  68. }
  69. break;
  70. }
  71. return true;
  72. }
  73. protected function resizeImageFileAnimatedGif(ImageFile $imagefile, $outpath, array $box)
  74. {
  75. $magick = new Imagick($imagefile->filepath);
  76. $magick = $magick->coalesceImages();
  77. $magick->setIteratorIndex(0);
  78. do {
  79. $magick->cropImage($box['w'], $box['h'], $box['x'], $box['y']);
  80. $magick->thumbnailImage($box['width'], $box['height']);
  81. $magick->setImagePage($box['width'], $box['height'], 0, 0);
  82. } while ($magick->nextImage());
  83. $magick = $magick->deconstructImages();
  84. // $magick->writeImages($outpath, true); did not work, had to use filehandle
  85. // There's been bugs for writeImages in php5-imagick before, probably now too
  86. $fh = fopen($outpath, 'w+');
  87. $success = $magick->writeImagesFile($fh);
  88. fclose($fh);
  89. $magick->destroy();
  90. return !$success;
  91. }
  92. public function onCreateFileImageThumbnailSource(File $file, &$imgPath, $media=null)
  93. {
  94. switch ($file->mimetype) {
  95. case 'image/svg+xml':
  96. if (!$this->rasterize_vectors) {
  97. // ImageMagick seems to be hard to trick into scaling vector graphics...
  98. return true;
  99. }
  100. break;
  101. default:
  102. // If we don't know the format, let's try not to mess with anything.
  103. return true;
  104. }
  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);
  108. @unlink($imgPath);
  109. $imgPath = null;
  110. return true;
  111. }
  112. return false;
  113. }
  114. protected function createImagePreview(File $file, $outpath)
  115. {
  116. $magick = new Imagick($file->getPath());
  117. $magick->setImageFormat($this->preview_imageformat);
  118. $magick->writeImage($outpath);
  119. $magick->destroy();
  120. return getimagesize($outpath); // Verify that we wrote an understandable image.
  121. }
  122. public function onPluginVersion(array &$versions): bool
  123. {
  124. $versions[] = array('name' => 'ImageMagick',
  125. 'version' => self::PLUGIN_VERSION,
  126. 'author' => 'Mikael Nordfeldth',
  127. 'homepage' => 'http://gnu.io/social',
  128. 'rawdescription' =>
  129. // TRANS: Plugin description.
  130. _m('Use ImageMagick for some more image support.'));
  131. return true;
  132. }
  133. }