Image.php 4.8 KB

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