Image.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. 'width_original' => $width,
  67. 'height_original' => $height,
  68. ];
  69. }
  70. public function resizeImage(Media $media)
  71. {
  72. $basePath = storage_path('app/'.$media->media_path);
  73. $this->handleResizeImage($media);
  74. }
  75. public function resizeThumbnail(Media $media)
  76. {
  77. $basePath = storage_path('app/'.$media->media_path);
  78. $this->handleThumbnailImage($media);
  79. }
  80. public function handleResizeImage(Media $media)
  81. {
  82. $this->handleImageTransform($media, false);
  83. }
  84. public function handleThumbnailImage(Media $media)
  85. {
  86. $this->handleImageTransform($media, true);
  87. }
  88. public function handleImageTransform(Media $media, $thumbnail = false)
  89. {
  90. $path = $media->media_path;
  91. $file = storage_path('app/'.$path);
  92. if (!in_array($media->mime, $this->acceptedMimes)) {
  93. return;
  94. }
  95. $ratio = $this->getAspectRatio($file, $thumbnail);
  96. $aspect = $ratio['dimensions'];
  97. $orientation = $ratio['orientation'];
  98. try {
  99. $img = Intervention::make($file);
  100. $metadata = $img->exif();
  101. $img->orientate();
  102. if($thumbnail) {
  103. $img->resize($aspect['width'], $aspect['height'], function ($constraint) {
  104. $constraint->aspectRatio();
  105. });
  106. } else {
  107. if(config('media.exif.database', false) == true && $metadata) {
  108. $meta = [];
  109. $keys = [
  110. "COMPUTED",
  111. "FileName",
  112. "FileSize",
  113. "FileType",
  114. "Make",
  115. "Model",
  116. "MimeType",
  117. "ColorSpace",
  118. "ExifVersion",
  119. "Orientation",
  120. "UserComment",
  121. "XResolution",
  122. "YResolution",
  123. "FileDateTime",
  124. "SectionsFound",
  125. "ExifImageWidth",
  126. "ResolutionUnit",
  127. "ExifImageLength",
  128. "FlashPixVersion",
  129. "Exif_IFD_Pointer",
  130. "YCbCrPositioning",
  131. "ComponentsConfiguration",
  132. "ExposureTime",
  133. "FNumber",
  134. "ISOSpeedRatings",
  135. "ShutterSpeedValue"
  136. ];
  137. foreach ($metadata as $k => $v) {
  138. if(in_array($k, $keys)) {
  139. $meta[$k] = $v;
  140. }
  141. }
  142. $media->metadata = json_encode($meta);
  143. }
  144. if (
  145. ($ratio['width_original'] > $aspect['width'])
  146. || ($ratio['height_original'] > $aspect['height'])
  147. ) {
  148. $img->resize($aspect['width'], $aspect['height'], function ($constraint) {
  149. $constraint->aspectRatio();
  150. });
  151. }
  152. }
  153. $converted = $this->setBaseName($path, $thumbnail, $img->extension);
  154. $newPath = storage_path('app/'.$converted['path']);
  155. $quality = config_cache('pixelfed.image_quality');
  156. $img->save($newPath, $quality);
  157. if ($thumbnail == true) {
  158. $media->thumbnail_path = $converted['path'];
  159. $media->thumbnail_url = url(Storage::url($converted['path']));
  160. } else {
  161. $media->width = $img->width();
  162. $media->height = $img->height();
  163. $media->orientation = $orientation;
  164. $media->media_path = $converted['path'];
  165. $media->mime = $img->mime;
  166. }
  167. $img->destroy();
  168. $media->save();
  169. if($thumbnail) {
  170. $this->generateBlurhash($media);
  171. }
  172. Cache::forget('status:transformer:media:attachments:'.$media->status_id);
  173. Cache::forget('status:thumb:'.$media->status_id);
  174. } catch (Exception $e) {
  175. $media->processed_at = now();
  176. $media->save();
  177. Log::info('MediaResizeException: Could not process media id: ' . $media->id);
  178. }
  179. }
  180. public function setBaseName($basePath, $thumbnail, $extension)
  181. {
  182. $png = false;
  183. $path = explode('.', $basePath);
  184. $name = ($thumbnail == true) ? $path[0].'_thumb' : $path[0];
  185. $ext = last($path);
  186. $basePath = "{$name}.{$ext}";
  187. return ['path' => $basePath, 'png' => $png];
  188. }
  189. protected function generateBlurhash($media)
  190. {
  191. $blurhash = Blurhash::generate($media);
  192. if($blurhash) {
  193. $media->blurhash = $blurhash;
  194. $media->save();
  195. }
  196. }
  197. }