ImageMagickPlugin.php 5.3 KB

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