diff --git a/app/Jobs/ResizeImage.php b/app/Jobs/ResizeImage.php new file mode 100644 index 0000000..408a754 --- /dev/null +++ b/app/Jobs/ResizeImage.php @@ -0,0 +1,81 @@ +image_id = $image_id; + $this->size = $size; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $image = Image::query()->where("id", "=", $this->image_id)->firstOrFail(); + $gallery = Gallery::query()->where("id", "=", $image->gallery)->firstOrFail(); + $tenant = Tenant::query()->where("id", "=", $gallery->tenant)->firstOrFail(); + $cacheName = "cache/".$tenant->url."_".$gallery->url."_".$image->id; + + //Check if orginal size is cached on the current system + if (Storage::disk('cache')->exists($cacheName."_orginal")) { + $file = Storage::disk("cache")->get($cacheName."_orginal"); + } else { + $this->addAccessLog($tenant->id, $gallery->id, $image->id, "Access", $image->size); + $file = Storage::disk($image->driver)->get($image->path); + if(env("CACHE_ORGINAL")) + { + Storage::disk("cache")->put($cacheName."_orginal", $file); + } + } + + $image = ImageResize::createFromString($file); + $image->resizeToLongSide($this->size); + Storage::disk("cache")->put($cacheName."_".$this->size, $image->getImageAsString()); + } + + + private function addAccessLog(int $tenant, int $gallery, int $image, string $typ, int $size) { + $access = new Access(); + $access->year = date("Y"); + $access->month = date("m"); + $access->day = date("d"); + $access->hour = date("H"); + $access->image = $image; + $access->gallery = $gallery; + $access->tenant = $tenant; + $access->typ = $typ; + $access->size = $size; + $access->saveOrFail(); + } +}