1
0

AvatarOptimize.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Jobs\AvatarPipeline;
  3. use App\Avatar;
  4. use App\Profile;
  5. use Cache;
  6. use Carbon\Carbon;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Str;
  13. use Storage;
  14. use Intervention\Image\ImageManager;
  15. use Intervention\Image\Encoders\JpegEncoder;
  16. use Intervention\Image\Encoders\WebpEncoder;
  17. use Intervention\Image\Encoders\AvifEncoder;
  18. use Intervention\Image\Encoders\PngEncoder;
  19. class AvatarOptimize implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $profile;
  23. protected $current;
  24. /**
  25. * Delete the job if its models no longer exist.
  26. *
  27. * @var bool
  28. */
  29. public $deleteWhenMissingModels = true;
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct(Profile $profile, $current)
  36. {
  37. $this->profile = $profile;
  38. $this->current = $current;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $avatar = $this->profile->avatar;
  48. $file = storage_path("app/$avatar->media_path");
  49. $fileInfo = pathinfo($file);
  50. $extension = strtolower($fileInfo['extension'] ?? 'jpg');
  51. $driver = match(config('image.driver')) {
  52. 'imagick' => \Intervention\Image\Drivers\Imagick\Driver::class,
  53. 'vips' => \Intervention\Image\Drivers\Vips\Driver::class,
  54. default => \Intervention\Image\Drivers\Gd\Driver::class
  55. };
  56. $imageManager = new ImageManager(
  57. $driver,
  58. autoOrientation: true,
  59. decodeAnimation: true,
  60. blendingColor: 'ffffff',
  61. strip: true
  62. );
  63. $quality = config_cache('pixelfed.image_quality');
  64. $encoder = null;
  65. switch ($extension) {
  66. case 'jpeg':
  67. case 'jpg':
  68. $encoder = new JpegEncoder($quality);
  69. break;
  70. case 'png':
  71. $encoder = new PngEncoder();
  72. break;
  73. case 'webp':
  74. $encoder = new WebpEncoder($quality);
  75. break;
  76. case 'avif':
  77. $encoder = new AvifEncoder($quality);
  78. break;
  79. case 'heic':
  80. $encoder = new JpegEncoder($quality);
  81. $extension = 'jpg';
  82. break;
  83. default:
  84. $encoder = new JpegEncoder($quality);
  85. $extension = 'jpg';
  86. }
  87. try {
  88. $img = $imageManager->read($file);
  89. $img = $img->coverDown(200, 200);
  90. $encoded = $encoder->encode($img);
  91. file_put_contents($file, $encoded->toString());
  92. $avatar = Avatar::whereProfileId($this->profile->id)->firstOrFail();
  93. $avatar->change_count = ++$avatar->change_count;
  94. $avatar->last_processed_at = Carbon::now();
  95. $avatar->save();
  96. Cache::forget('avatar:'.$avatar->profile_id);
  97. $this->deleteOldAvatar($avatar->media_path, $this->current);
  98. if ((bool) config_cache('pixelfed.cloud_storage') && (bool) config_cache('instance.avatar.local_to_cloud')) {
  99. $this->uploadToCloud($avatar);
  100. } else {
  101. $avatar->cdn_url = null;
  102. $avatar->save();
  103. }
  104. } catch (Exception $e) {
  105. }
  106. }
  107. protected function deleteOldAvatar($new, $current)
  108. {
  109. if (storage_path('app/'.$new) == $current ||
  110. Str::endsWith($current, 'avatars/default.png') ||
  111. Str::endsWith($current, 'avatars/default.jpg')) {
  112. return;
  113. }
  114. if (is_file($current)) {
  115. @unlink($current);
  116. }
  117. }
  118. protected function uploadToCloud($avatar)
  119. {
  120. $base = 'cache/avatars/'.$avatar->profile_id;
  121. $disk = Storage::disk(config('filesystems.cloud'));
  122. $disk->deleteDirectory($base);
  123. $path = $base.'/'.'avatar_'.strtolower(Str::random(random_int(3, 6))).$avatar->change_count.'.'.pathinfo($avatar->media_path, PATHINFO_EXTENSION);
  124. $url = $disk->put($path, Storage::get($avatar->media_path));
  125. $avatar->media_path = $path;
  126. $avatar->cdn_url = $disk->url($path);
  127. $avatar->save();
  128. Storage::delete($avatar->media_path);
  129. Cache::forget('avatar:'.$avatar->profile_id);
  130. }
  131. }