ImageEncoder.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Plugin\ImageEncoder;
  20. use App\Core\Event;
  21. use App\Core\GSFile;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Log;
  24. use App\Core\Modules\Plugin;
  25. use App\Util\Common;
  26. use App\Util\Exception\ClientException;
  27. use App\Util\Exception\ServerException;
  28. use App\Util\Exception\TemporaryFileException;
  29. use App\Util\Formatting;
  30. use App\Util\TemporaryFile;
  31. use EventResult;
  32. use Exception;
  33. use Jcupitt\Vips;
  34. use SplFileInfo;
  35. /**
  36. * Create thumbnails and validate image attachments
  37. *
  38. * @package GNUsocial
  39. * @category Attachment
  40. *
  41. * @author Diogo Peralta Cordeiro <mail@diogo.site>
  42. * @author Hugo Sales <hugo@hsal.es>
  43. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  44. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  45. */
  46. class ImageEncoder extends Plugin
  47. {
  48. public static function version(): string
  49. {
  50. return '3.0.0';
  51. }
  52. public static function shouldHandle(string $mimetype): bool
  53. {
  54. return GSFile::mimetypeMajor($mimetype) === 'image';
  55. }
  56. public function onFileMetaAvailable(array &$event_map, string $mimetype): EventResult
  57. {
  58. if (!self::shouldHandle($mimetype)) {
  59. return Event::next;
  60. }
  61. $event_map['image'][] = [$this, 'fileMeta'];
  62. return Event::next;
  63. }
  64. public function onFileSanitizerAvailable(array &$event_map, string $mimetype): EventResult
  65. {
  66. if (!self::shouldHandle($mimetype)) {
  67. return Event::next;
  68. }
  69. $event_map['image'][] = [$this, 'fileSanitize'];
  70. return Event::next;
  71. }
  72. public function onFileResizerAvailable(array &$event_map, string $mimetype): EventResult
  73. {
  74. if (!self::shouldHandle($mimetype)) {
  75. return Event::next;
  76. }
  77. $event_map['image'][] = [$this, 'resizeImagePath'];
  78. return Event::next;
  79. }
  80. public function fileMeta(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
  81. {
  82. $old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
  83. try {
  84. $original_mimetype = $mimetype;
  85. if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
  86. // Nothing concerning us
  87. return false;
  88. }
  89. try {
  90. $image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
  91. } catch (Vips\Exception $e) {
  92. Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e->getMessage()}.", [$e]);
  93. return false;
  94. }
  95. $width = $image->width;
  96. $height = $image->height;
  97. } finally {
  98. ini_set('memory_limit', $old_limit); // Restore the old memory limit
  99. }
  100. // Only one plugin can handle meta
  101. return true;
  102. }
  103. /**
  104. * Re-encodes the image ensuring it is valid.
  105. * Also ensures that the image is not greater than the max width and height configured.
  106. *
  107. * @param null|string $mimetype in/out
  108. * @param null|int $width out
  109. * @param null|int $height out
  110. *
  111. * @throws ClientException When vips doesn't understand the given mimetype
  112. * @throws ServerException
  113. * @throws TemporaryFileException
  114. * @throws Vips\Exception
  115. *
  116. * @return bool true if sanitized
  117. */
  118. public function fileSanitize(SplFileInfo &$file, ?string &$mimetype, ?int &$width, ?int &$height): bool
  119. {
  120. $old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
  121. try {
  122. $original_mimetype = $mimetype;
  123. if (GSFile::mimetypeMajor($original_mimetype) !== 'image') {
  124. // Nothing concerning us
  125. return false;
  126. }
  127. // Try to maintain original mimetype extension, otherwise default to preferred.
  128. $extension = '.' . Common::config('thumbnail', 'extension');
  129. $extension = GSFile::ensureFilenameWithProperExtension(
  130. title: $file->getFilename(),
  131. mimetype: $original_mimetype,
  132. ext: $extension,
  133. force: false,
  134. ) ?? $extension;
  135. // TemporaryFile handles deleting the file if some error occurs
  136. // IMPORTANT: We have to specify the extension for the temporary file
  137. // in order to have a format conversion
  138. $temp = new TemporaryFile(['prefix' => 'image', 'suffix' => $extension]);
  139. try {
  140. $image = Vips\Image::newFromFile($file->getRealPath(), ['access' => 'sequential']);
  141. } catch (Vips\Exception $e) {
  142. Log::debug("ImageEncoder's Vips couldn't handle the image file, failed with {$e->getMessage()}.", [$e]);
  143. return false;
  144. }
  145. $width = $image->width;
  146. $height = $image->height;
  147. $image = $image->crop(
  148. left: 0,
  149. top: 0,
  150. width: $width,
  151. height: $height,
  152. );
  153. $image->writeToFile($temp->getRealPath());
  154. // Replace original file with the sanitized one
  155. $temp->commit($file->getRealPath());
  156. } finally {
  157. ini_set('memory_limit', $old_limit); // Restore the old memory limit
  158. }
  159. // Only one plugin can handle sanitization
  160. return true;
  161. }
  162. /**
  163. * Generates the view for attachments of type Image
  164. */
  165. public function onViewAttachment(array $vars, array &$res): EventResult
  166. {
  167. if (!self::shouldHandle($vars['attachment']->getMimetype())) {
  168. return Event::next;
  169. }
  170. if (\is_null($thumbnail = $vars['attachment']->getThumbnail())) {
  171. return Event::next;
  172. }
  173. $res[] = Formatting::twigRenderFile(
  174. 'imageEncoder/imageEncoderView.html.twig',
  175. [
  176. 'attachment' => $vars['attachment'],
  177. 'note' => $vars['note'],
  178. 'title' => $vars['title'],
  179. 'thumbnail' => $thumbnail,
  180. ],
  181. );
  182. return Event::stop;
  183. }
  184. /**
  185. * Resizes an image. It will encode the image in the
  186. * preferred thumbnail extension. This only applies henceforward,
  187. * not retroactively
  188. *
  189. * Increases the 'memory_limit' to the one in the 'attachments' section in the config, to
  190. * enable the handling of bigger images, which can cause a peak of memory consumption, while
  191. * encoding
  192. *
  193. * @throws TemporaryFileException
  194. * @throws Vips\Exception
  195. */
  196. public function resizeImagePath(string $source, ?TemporaryFile &$destination, int &$width, int &$height, bool $smart_crop, ?string &$mimetype): bool
  197. {
  198. $old_limit = ini_set('memory_limit', Common::config('attachments', 'memory_limit'));
  199. try {
  200. try {
  201. if (!$smart_crop) {
  202. $image = Vips\Image::thumbnail($source, $width, ['height' => $height]);
  203. } else {
  204. $image = Vips\Image::newFromFile($source, ['access' => 'sequential']);
  205. $image = $image->smartcrop($width, $height, [Vips\Interesting::ATTENTION]);
  206. }
  207. } catch (Exception $e) {
  208. Log::error(__METHOD__ . ' encountered exception: ' . \get_class($e));
  209. // TRANS: Exception thrown when trying to resize an unknown file type.
  210. throw new Exception(_m('Unknown file type'));
  211. }
  212. if (\is_null($destination)) {
  213. // IMPORTANT: We have to specify the extension for the temporary file
  214. // in order to have a format conversion
  215. $ext = '.' . Common::config('thumbnail', 'extension');
  216. $destination = new TemporaryFile(['prefix' => 'gs-thumbnail', 'suffix' => $ext]);
  217. } elseif ($source === $destination->getRealPath()) {
  218. @unlink($destination->getRealPath());
  219. }
  220. $mimetype = Common::config('thumbnail', 'mimetype');
  221. $width = $image->width;
  222. $height = $image->height;
  223. $image->writeToFile($destination->getRealPath());
  224. unset($image);
  225. } finally {
  226. ini_set('memory_limit', $old_limit); // Restore the old memory limit
  227. }
  228. return true;
  229. }
  230. /**
  231. * Event raised when GNU social polls the plugin for information about it.
  232. * Adds this plugin's version information to $versions array
  233. *
  234. * @param array $versions inherited from parent
  235. */
  236. public function onPluginVersion(array &$versions): EventResult
  237. {
  238. $versions[] = [
  239. 'name' => 'ImageEncoder',
  240. 'version' => $this->version(),
  241. 'author' => 'Hugo Sales, Diogo Peralta Cordeiro',
  242. 'homepage' => GNUSOCIAL_PROJECT_URL,
  243. 'description', // TRANS: Plugin description. => _m('Use VIPS for some additional image support.'),
  244. ];
  245. return Event::next;
  246. }
  247. }