ImageThumbnail.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Jobs\ImageOptimizePipeline;
  3. use App\Media;
  4. use App\Util\Media\Image;
  5. use Carbon\Carbon;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Log;
  12. use Storage;
  13. class ImageThumbnail implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $media;
  17. /**
  18. * Delete the job if its models no longer exist.
  19. *
  20. * @var bool
  21. */
  22. public $deleteWhenMissingModels = true;
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct(Media $media)
  29. {
  30. $this->media = $media;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. $media = $this->media;
  40. if (! $media) {
  41. return;
  42. }
  43. $localFs = config('filesystems.default') === 'local';
  44. if ($localFs) {
  45. $path = storage_path('app/'.$media->media_path);
  46. if (! is_file($path)) {
  47. return;
  48. }
  49. } else {
  50. $disk = Storage::disk(config('filesystems.default'));
  51. if (! $disk->exists($media->media_path)) {
  52. return;
  53. }
  54. }
  55. try {
  56. $img = new Image;
  57. $img->resizeThumbnail($media);
  58. } catch (\Exception $e) {
  59. if (config('app.dev_log')) {
  60. Log::error('Thumbnail generation failed: '.$e->getMessage());
  61. }
  62. }
  63. $media->processed_at = Carbon::now();
  64. $media->save();
  65. ImageUpdate::dispatch($media)->onQueue('mmo');
  66. }
  67. }