From efe0b265a65404270d906d10ff6ad968c1db7329 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 29 Oct 2020 21:56:35 +0100 Subject: [PATCH] Add isCroppable and abort resizer when image is not croppable --- classes/CompressJob.php | 11 ++++++++++- models/SourceFile.php | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/classes/CompressJob.php b/classes/CompressJob.php index 2c7e4a2..a406b85 100644 --- a/classes/CompressJob.php +++ b/classes/CompressJob.php @@ -18,13 +18,18 @@ class CompressJob { public function fire($job, $data) { $this->attachment_id = $data['attachment_id']; + $this->attachment = Attachment::find($this->attachment_id); + + if (!$this->shouldResize()) { + return; + } + $this->crop = $data['crop']; $this->sizes = Setting::get('srcx'); Storage::disk($this->disk)->makeDirectory('cropped'); - $this->attachment = Attachment::find($this->attachment_id); $this->fullPath = Storage::disk($this->disk)->path($this->attachment->source->path); $this->crop(); @@ -73,4 +78,8 @@ class CompressJob { return app($this->strategy); } + private function shouldResize() { + return $this->attachment->source->isCroppable(); + } + } diff --git a/models/SourceFile.php b/models/SourceFile.php index 7d40eda..4660791 100644 --- a/models/SourceFile.php +++ b/models/SourceFile.php @@ -12,6 +12,8 @@ class SourceFile extends Model protected $slugs = ['slug' => 'basename']; + protected $croppableTypes = [ 'image/jpeg', 'image/gif', 'image/png', 'image/webp' ]; + /** * @var string The database table used by the model. */ @@ -59,4 +61,8 @@ class SourceFile extends Model public function getSizeAttribute() { return Storage::disk('uploads')->size($this->path); } + + public function isCroppable(): bool { + return in_array($this->type, $this->croppableTypes); + } }