ImageEncoder.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\ImageEncoder;
  19. use App\Core\Event;
  20. use App\Core\GSFile;
  21. use function App\Core\I18n\_m;
  22. use App\Core\Log;
  23. use App\Core\Modules\Plugin;
  24. use App\Entity\Attachment;
  25. use App\Entity\AttachmentThumbnail;
  26. use App\Util\Common;
  27. use App\Util\TemporaryFile;
  28. use Exception;
  29. use Jcupitt\Vips;
  30. /**
  31. * Create thumbnails and validate image attachments
  32. *
  33. * @package GNUsocial
  34. * @ccategory Attachment
  35. *
  36. * @author Diogo Peralta Cordeiro <mail@diogo.site>
  37. * @authir Hugo Sales <hugo@hsal.es>
  38. *
  39. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  40. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  41. */
  42. class ImageEncoder extends Plugin
  43. {
  44. /**
  45. * Several obscure file types should be normalized to WebP on resize.
  46. */
  47. public function preferredType(): int
  48. {
  49. return IMAGETYPE_WEBP;
  50. }
  51. /**
  52. * Encodes the image to self::preferredType() format ensuring it's valid.
  53. *
  54. * @param \SplFileInfo $file
  55. * @param null|string $mimetype in/out
  56. * @param null|string $title in/out
  57. * @param null|int $width out
  58. * @param null|int $height out
  59. *
  60. * @throws Vips\Exception
  61. * @throws \App\Util\Exception\TemporaryFileException
  62. *
  63. * @return bool
  64. */
  65. public function onAttachmentValidation(\SplFileInfo &$file, ?string &$mimetype, ?string &$title, ?int &$width, ?int &$height): bool
  66. {
  67. $original_mimetype = $mimetype;
  68. if (GSFile::mimetypeMajor($original_mimetype) != 'image') {
  69. // Nothing concerning us
  70. return Event::next;
  71. }
  72. $type = self::preferredType();
  73. $extension = image_type_to_extension($type, include_dot: true);
  74. $temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]); // This handles deleting the file if some error occurs
  75. $mimetype = image_type_to_mime_type($type);
  76. if ($mimetype != $original_mimetype) {
  77. // If title seems to be a filename with an extension
  78. if (preg_match('/\.[a-z0-9]/i', $title) === 1) {
  79. $title = substr($title, 0, strrpos($title, '.')) . $extension;
  80. }
  81. }
  82. $image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
  83. $width = Common::clamp($image->width, 0, Common::config('attachments', 'max_width'));
  84. $height = Common::clamp($image->height, 0, Common::config('attachments', 'max_height'));
  85. $image = $image->crop(0, 0, $width, $height);
  86. $image->writeToFile($temp->getRealPath());
  87. $filesize = $temp->getSize();
  88. $filepath = $file->getRealPath();
  89. @unlink($filepath);
  90. Event::handle('EnforceQuota', [$filesize]);
  91. $temp->commit($filepath);
  92. return Event::stop;
  93. }
  94. /**
  95. * Resizes an image. It will encode the image in the
  96. * `self::preferredType()` format. This only applies henceforward,
  97. * not retroactively
  98. *
  99. * Increases the 'memory_limit' to the one in the 'attachments' section in the config, to
  100. * enable the handling of bigger images, which can cause a peak of memory consumption, while
  101. * encoding
  102. *
  103. * @param Attachment $attachment
  104. * @param AttachmentThumbnail $thumbnail
  105. * @param int $width
  106. * @param int $height
  107. * @param bool $crop
  108. *
  109. * @throws Exception
  110. * @throws Vips\Exception
  111. *
  112. * @return bool
  113. *
  114. */
  115. public function onResizeImagePath(string $source, string $destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype)
  116. {
  117. $old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
  118. try {
  119. try {
  120. $image = Vips\Image::thumbnail($source, $width, ['height' => $height]);
  121. } catch (Exception $e) {
  122. Log::error(__METHOD__ . ' encountered exception: ' . print_r($e, true));
  123. // TRANS: Exception thrown when trying to resize an unknown file type.
  124. throw new Exception(_m('Unknown file type'));
  125. }
  126. if ($source === $destination) {
  127. @unlink($destination);
  128. }
  129. $type = self::preferredType();
  130. $mimetype = image_type_to_mime_type($type);
  131. if ($smart_crop) {
  132. $image = $image->smartcrop($width, $height);
  133. }
  134. $width = $image->width;
  135. $height = $image->height;
  136. $image->writeToFile($destination);
  137. unset($image);
  138. } finally {
  139. ini_set('memory_limit', $old_limit); // Restore the old memory limit
  140. }
  141. return Event::next;
  142. }
  143. }