ImageResizePipeline.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Jobs\GroupsPipeline;
  3. use App\Models\GroupMedia;
  4. use Exception;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Intervention\Image\Encoders\JpegEncoder;
  11. use Intervention\Image\Encoders\PngEncoder;
  12. use Intervention\Image\ImageManager;
  13. use Log;
  14. use Storage;
  15. class ImageResizePipeline implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $media;
  19. /**
  20. * Delete the job if its models no longer exist.
  21. *
  22. * @var bool
  23. */
  24. public $deleteWhenMissingModels = true;
  25. /**
  26. * Create a new job instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct(GroupMedia $media)
  31. {
  32. $this->media = $media;
  33. }
  34. /**
  35. * Execute the job.
  36. *
  37. * @return void
  38. */
  39. public function handle()
  40. {
  41. $media = $this->media;
  42. if (! $media) {
  43. return;
  44. }
  45. if (! Storage::exists($media->media_path) || $media->skip_optimize) {
  46. return;
  47. }
  48. $path = $media->media_path;
  49. $file = storage_path('app/'.$path);
  50. $quality = config_cache('pixelfed.image_quality');
  51. $orientations = [
  52. 'square' => [
  53. 'width' => 1080,
  54. 'height' => 1080,
  55. ],
  56. 'landscape' => [
  57. 'width' => 1920,
  58. 'height' => 1080,
  59. ],
  60. 'portrait' => [
  61. 'width' => 1080,
  62. 'height' => 1350,
  63. ],
  64. ];
  65. try {
  66. $driver = match (config('image.driver')) {
  67. 'imagick' => \Intervention\Image\Drivers\Imagick\Driver::class,
  68. 'vips' => \Intervention\Image\Drivers\Vips\Driver::class,
  69. default => \Intervention\Image\Drivers\Gd\Driver::class
  70. };
  71. $imageManager = new ImageManager(
  72. $driver,
  73. autoOrientation: true,
  74. decodeAnimation: true,
  75. blendingColor: 'ffffff',
  76. strip: true
  77. );
  78. $img = $imageManager->read($file);
  79. $width = $img->width();
  80. $height = $img->height();
  81. $aspect = $width / $height;
  82. $orientation = $aspect === 1 ? 'square' : ($aspect > 1 ? 'landscape' : 'portrait');
  83. $ratio = $orientations[$orientation];
  84. $img = $img->resize($ratio['width'], $ratio['height'], function ($constraint) {
  85. $constraint->aspectRatio();
  86. $constraint->upsize();
  87. });
  88. $extension = pathinfo($file, PATHINFO_EXTENSION);
  89. if (in_array(strtolower($extension), ['jpg', 'jpeg'])) {
  90. $encoder = new JpegEncoder($quality);
  91. } else {
  92. $encoder = new PngEncoder;
  93. }
  94. $encoded = $img->encode($encoder);
  95. file_put_contents($file, $encoded);
  96. } catch (Exception $e) {
  97. Log::error($e);
  98. }
  99. }
  100. }