Image.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace App\Util\Media;
  3. use App\Media;
  4. use Image as Intervention;
  5. use Cache, Log, Storage;
  6. class Image
  7. {
  8. public $square;
  9. public $landscape;
  10. public $portrait;
  11. public $thumbnail;
  12. public $orientation;
  13. public $acceptedMimes = [
  14. 'image/png',
  15. 'image/jpeg',
  16. 'image/webp'
  17. ];
  18. public function __construct()
  19. {
  20. ini_set('memory_limit', config('pixelfed.memory_limit', '1024M'));
  21. $this->square = $this->orientations()['square'];
  22. $this->landscape = $this->orientations()['landscape'];
  23. $this->portrait = $this->orientations()['portrait'];
  24. $this->thumbnail = [
  25. 'width' => 640,
  26. 'height' => 640,
  27. ];
  28. $this->orientation = null;
  29. }
  30. public function orientations()
  31. {
  32. return [
  33. 'square' => [
  34. 'width' => 1080,
  35. 'height' => 1080,
  36. ],
  37. 'landscape' => [
  38. 'width' => 1920,
  39. 'height' => 1080,
  40. ],
  41. 'portrait' => [
  42. 'width' => 1080,
  43. 'height' => 1350,
  44. ],
  45. ];
  46. }
  47. public function getAspectRatio($mediaPath, $thumbnail = false)
  48. {
  49. if (!is_file($mediaPath)) {
  50. throw new \Exception('Invalid Media Path');
  51. }
  52. if ($thumbnail) {
  53. return [
  54. 'dimensions' => $this->thumbnail,
  55. 'orientation' => 'thumbnail',
  56. ];
  57. }
  58. list($width, $height) = getimagesize($mediaPath);
  59. $aspect = $width / $height;
  60. $orientation = $aspect === 1 ? 'square' :
  61. ($aspect > 1 ? 'landscape' : 'portrait');
  62. $this->orientation = $orientation;
  63. return [
  64. 'dimensions' => $this->orientations()[$orientation],
  65. 'orientation' => $orientation,
  66. ];
  67. }
  68. public function resizeImage(Media $media)
  69. {
  70. $basePath = storage_path('app/'.$media->media_path);
  71. $this->handleResizeImage($media);
  72. }
  73. public function resizeThumbnail(Media $media)
  74. {
  75. $basePath = storage_path('app/'.$media->media_path);
  76. $this->handleThumbnailImage($media);
  77. }
  78. public function handleResizeImage(Media $media)
  79. {
  80. $this->handleImageTransform($media, false);
  81. }
  82. public function handleThumbnailImage(Media $media)
  83. {
  84. $this->handleImageTransform($media, true);
  85. }
  86. public function handleImageTransform(Media $media, $thumbnail = false)
  87. {
  88. $path = $media->media_path;
  89. $file = storage_path('app/'.$path);
  90. if (!in_array($media->mime, $this->acceptedMimes)) {
  91. return;
  92. }
  93. $ratio = $this->getAspectRatio($file, $thumbnail);
  94. $aspect = $ratio['dimensions'];
  95. $orientation = $ratio['orientation'];
  96. try {
  97. $img = Intervention::make($file);
  98. $metadata = $img->exif();
  99. $img->orientate();
  100. if($thumbnail) {
  101. $img->resize($aspect['width'], $aspect['height'], function ($constraint) {
  102. $constraint->aspectRatio();
  103. });
  104. } else {
  105. if(config('media.exif.database', false) == true && $metadata) {
  106. $meta = [];
  107. $keys = [
  108. "COMPUTED",
  109. "FileName",
  110. "FileSize",
  111. "FileType",
  112. "Make",
  113. "Model",
  114. "MimeType",
  115. "ColorSpace",
  116. "ExifVersion",
  117. "Orientation",
  118. "UserComment",
  119. "XResolution",
  120. "YResolution",
  121. "FileDateTime",
  122. "SectionsFound",
  123. "ExifImageWidth",
  124. "ResolutionUnit",
  125. "ExifImageLength",
  126. "FlashPixVersion",
  127. "Exif_IFD_Pointer",
  128. "YCbCrPositioning",
  129. "ComponentsConfiguration",
  130. "ExposureTime",
  131. "FNumber",
  132. "ISOSpeedRatings",
  133. "ShutterSpeedValue"
  134. ];
  135. foreach ($metadata as $k => $v) {
  136. if(in_array($k, $keys)) {
  137. $meta[$k] = $v;
  138. }
  139. }
  140. $media->metadata = json_encode($meta);
  141. }
  142. $img->resize($aspect['width'], $aspect['height'], function ($constraint) {
  143. $constraint->aspectRatio();
  144. });
  145. }
  146. $converted = $this->setBaseName($path, $thumbnail, $img->extension);
  147. $newPath = storage_path('app/'.$converted['path']);
  148. $quality = config_cache('pixelfed.image_quality');
  149. $img->save($newPath, $quality);
  150. if ($thumbnail == true) {
  151. $media->thumbnail_path = $converted['path'];
  152. $media->thumbnail_url = url(Storage::url($converted['path']));
  153. } else {
  154. $media->width = $img->width();
  155. $media->height = $img->height();
  156. $media->orientation = $orientation;
  157. $media->media_path = $converted['path'];
  158. $media->mime = $img->mime;
  159. }
  160. $img->destroy();
  161. $media->save();
  162. if($thumbnail) {
  163. $this->generateBlurhash($media);
  164. }
  165. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  166. Cache::forget('status:thumb:'.$media->status_id);
  167. } catch (Exception $e) {
  168. $media->processed_at = now();
  169. $media->save();
  170. Log::info('MediaResizeException: Could not process media id: ' . $media->id);
  171. }
  172. }
  173. public function setBaseName($basePath, $thumbnail, $extension)
  174. {
  175. $png = false;
  176. $path = explode('.', $basePath);
  177. $name = ($thumbnail == true) ? $path[0].'_thumb' : $path[0];
  178. $ext = last($path);
  179. $basePath = "{$name}.{$ext}";
  180. return ['path' => $basePath, 'png' => $png];
  181. }
  182. protected function generateBlurhash($media)
  183. {
  184. $blurhash = Blurhash::generate($media);
  185. if($blurhash) {
  186. $media->blurhash = $blurhash;
  187. $media->save();
  188. }
  189. }
  190. }