Image.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. namespace App\Util\Media;
  3. use App\Media;
  4. use Intervention\Image\ImageManager;
  5. use Intervention\Image\Encoders\JpegEncoder;
  6. use Intervention\Image\Encoders\WebpEncoder;
  7. use Intervention\Image\Encoders\AvifEncoder;
  8. use Intervention\Image\Encoders\PngEncoder;
  9. use Cache, Log, Storage;
  10. use App\Util\Media\Blurhash;
  11. use App\Services\StatusService;
  12. class Image
  13. {
  14. public $square;
  15. public $landscape;
  16. public $portrait;
  17. public $thumbnail;
  18. public $orientation;
  19. public $acceptedMimes = [
  20. 'image/png',
  21. 'image/jpeg',
  22. 'image/jpg',
  23. 'image/webp',
  24. 'image/avif',
  25. 'image/heic',
  26. ];
  27. protected $imageManager;
  28. public function __construct()
  29. {
  30. ini_set('memory_limit', config('pixelfed.memory_limit', '1024M'));
  31. $this->square = $this->orientations()['square'];
  32. $this->landscape = $this->orientations()['landscape'];
  33. $this->portrait = $this->orientations()['portrait'];
  34. $this->thumbnail = [
  35. 'width' => 640,
  36. 'height' => 640,
  37. ];
  38. $this->orientation = null;
  39. $driver = match(config('image.driver')) {
  40. 'imagick' => \Intervention\Image\Drivers\Imagick\Driver::class,
  41. 'vips' => \Intervention\Image\Drivers\Vips\Driver::class,
  42. default => \Intervention\Image\Drivers\Gd\Driver::class
  43. };
  44. $this->imageManager = new ImageManager(
  45. $driver,
  46. autoOrientation: true,
  47. decodeAnimation: true,
  48. blendingColor: 'ffffff',
  49. strip: true
  50. );
  51. }
  52. public function orientations()
  53. {
  54. return [
  55. 'square' => [
  56. 'width' => 1080,
  57. 'height' => 1080,
  58. ],
  59. 'landscape' => [
  60. 'width' => 1920,
  61. 'height' => 1080,
  62. ],
  63. 'portrait' => [
  64. 'width' => 1080,
  65. 'height' => 1350,
  66. ],
  67. ];
  68. }
  69. public function getAspect($width, $height, $isThumbnail)
  70. {
  71. if ($isThumbnail) {
  72. return [
  73. 'dimensions' => $this->thumbnail,
  74. 'orientation' => 'thumbnail',
  75. ];
  76. }
  77. $aspect = $width / $height;
  78. $orientation = $aspect === 1 ? 'square' :
  79. ($aspect > 1 ? 'landscape' : 'portrait');
  80. $this->orientation = $orientation;
  81. return [
  82. 'dimensions' => $this->orientations()[$orientation],
  83. 'orientation' => $orientation,
  84. 'width_original' => $width,
  85. 'height_original' => $height,
  86. ];
  87. }
  88. public function resizeImage(Media $media)
  89. {
  90. $this->handleResizeImage($media);
  91. }
  92. public function resizeThumbnail(Media $media)
  93. {
  94. $this->handleThumbnailImage($media);
  95. }
  96. public function handleResizeImage(Media $media)
  97. {
  98. $this->handleImageTransform($media, false);
  99. }
  100. public function handleThumbnailImage(Media $media)
  101. {
  102. $this->handleImageTransform($media, true);
  103. }
  104. public function handleImageTransform(Media $media, $thumbnail = false)
  105. {
  106. $path = $media->media_path;
  107. $file = storage_path('app/'.$path);
  108. if (!in_array($media->mime, $this->acceptedMimes)) {
  109. return;
  110. }
  111. try {
  112. $fileInfo = pathinfo($file);
  113. $extension = strtolower($fileInfo['extension'] ?? 'jpg');
  114. $outputExtension = $extension;
  115. $metadata = null;
  116. if (!$thumbnail && config('media.exif.database', false) == true) {
  117. try {
  118. $exif = @exif_read_data($file);
  119. if ($exif) {
  120. $meta = [];
  121. $keys = [
  122. "FileName",
  123. "FileSize",
  124. "FileType",
  125. "Make",
  126. "Model",
  127. "MimeType",
  128. "ColorSpace",
  129. "ExifVersion",
  130. "Orientation",
  131. "UserComment",
  132. "XResolution",
  133. "YResolution",
  134. "FileDateTime",
  135. "SectionsFound",
  136. "ExifImageWidth",
  137. "ResolutionUnit",
  138. "ExifImageLength",
  139. "FlashPixVersion",
  140. "Exif_IFD_Pointer",
  141. "YCbCrPositioning",
  142. "ComponentsConfiguration",
  143. "ExposureTime",
  144. "FNumber",
  145. "ISOSpeedRatings",
  146. "ShutterSpeedValue"
  147. ];
  148. foreach ($exif as $k => $v) {
  149. if (in_array($k, $keys)) {
  150. $meta[$k] = $v;
  151. }
  152. }
  153. $media->metadata = json_encode($meta);
  154. }
  155. } catch (\Exception $e) {
  156. Log::info('EXIF extraction failed: ' . $e->getMessage());
  157. }
  158. }
  159. $img = $this->imageManager->read($file);
  160. $ratio = $this->getAspect($img->width(), $img->height(), $thumbnail);
  161. $aspect = $ratio['dimensions'];
  162. $orientation = $ratio['orientation'];
  163. if ($thumbnail) {
  164. $img = $img->coverDown(
  165. $aspect['width'],
  166. $aspect['height']
  167. );
  168. } else {
  169. if (
  170. ($ratio['width_original'] > $aspect['width'])
  171. || ($ratio['height_original'] > $aspect['height'])
  172. ) {
  173. $img = $img->scaleDown(
  174. $aspect['width'],
  175. $aspect['height']
  176. );
  177. }
  178. }
  179. $quality = config_cache('pixelfed.image_quality');
  180. $encoder = null;
  181. switch ($extension) {
  182. case 'jpeg':
  183. case 'jpg':
  184. $encoder = new JpegEncoder($quality);
  185. $outputExtension = 'jpg';
  186. break;
  187. case 'png':
  188. $encoder = new PngEncoder();
  189. $outputExtension = 'png';
  190. break;
  191. case 'webp':
  192. $encoder = new WebpEncoder($quality);
  193. $outputExtension = 'webp';
  194. break;
  195. case 'avif':
  196. $encoder = new JpegEncoder($quality);
  197. $outputExtension = 'jpg';
  198. break;
  199. case 'heic':
  200. $encoder = new JpegEncoder($quality);
  201. $outputExtension = 'jpg';
  202. break;
  203. default:
  204. $encoder = new JpegEncoder($quality);
  205. $outputExtension = 'jpg';
  206. }
  207. $converted = $this->setBaseName($path, $thumbnail, $outputExtension);
  208. $newPath = storage_path('app/'.$converted['path']);
  209. $encoded = $encoder->encode($img);
  210. file_put_contents($newPath, $encoded->toString());
  211. if ($thumbnail == true) {
  212. $media->thumbnail_path = $converted['path'];
  213. $media->thumbnail_url = url(Storage::url($converted['path']));
  214. } else {
  215. $media->width = $img->width();
  216. $media->height = $img->height();
  217. $media->orientation = $orientation;
  218. $media->media_path = $converted['path'];
  219. $media->mime = 'image/' . $outputExtension;
  220. }
  221. $media->save();
  222. if ($thumbnail) {
  223. $this->generateBlurhash($media);
  224. }
  225. if($media->status_id) {
  226. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  227. Cache::forget('status:thumb:'.$media->status_id);
  228. StatusService::del($media->status_id);
  229. }
  230. } catch (\Exception $e) {
  231. $media->processed_at = now();
  232. $media->save();
  233. Log::info('MediaResizeException: ' . $e->getMessage() . ' | Could not process media id: ' . $media->id);
  234. }
  235. }
  236. public function setBaseName($basePath, $thumbnail, $extension)
  237. {
  238. $path = explode('.', $basePath);
  239. $name = ($thumbnail == true) ? $path[0].'_thumb' : $path[0];
  240. $basePath = "{$name}.{$extension}";
  241. return ['path' => $basePath, 'png' => false];
  242. }
  243. protected function generateBlurhash($media)
  244. {
  245. $blurhash = Blurhash::generate($media);
  246. if ($blurhash) {
  247. $media->blurhash = $blurhash;
  248. $media->save();
  249. }
  250. }
  251. }