kuvia/app/Jobs/ResizeImage.php

154 lines
5.3 KiB
PHP
Raw Normal View History

2021-01-17 02:09:02 +00:00
<?php
namespace App\Jobs;
2021-01-20 18:51:12 +00:00
use Ajaxray\PHPWatermark\Watermark;
2021-01-17 02:09:02 +00:00
use App\Models\Access;
use App\Models\Gallery;
use App\Models\Image;
use App\Models\Tenant;
use Gumlet\ImageResize;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
2021-01-20 18:51:12 +00:00
use Intervention\Image\ImageManager;
2021-01-17 02:09:02 +00:00
class ResizeImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $image_id;
private $size;
/**
* Create a new job instance.
*
* @return void
*/
2021-01-17 02:13:59 +00:00
public function __construct(int $image_id, int $size)
2021-01-17 02:09:02 +00:00
{
$this->image_id = $image_id;
$this->size = $size;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2021-01-20 18:51:12 +00:00
//Get Database Entitiys
2021-01-17 02:09:02 +00:00
$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
2021-01-20 20:25:29 +00:00
$file = $this->getImage($tenant, $gallery, $image, $cacheName);
2021-01-20 18:51:12 +00:00
$manager = new ImageManager(array('driver' => 'imagick'));
$img = $manager->make($file);
$newHeight = $img->getHeight();
$newWidth = $img->getWidth();
if($img->getWidth() > $img->getHeight()) {
$newWidth = $this->size;
$newHeight = $img->getHeight() * ($newWidth / $img->getWidth() );
} else {
$newHeight = $this->size;
$newWidth = $img->getWidth() * ($newHeight / $img->getHeight());
}
$tmpfname = tempnam("/tmp", "FOO").".jpg";
$img->resize($newWidth, $newHeight);
//$img->blur(50);
2021-01-20 20:43:58 +00:00
if($this->size > 500 && !is_null($tenant->watermark)) {
Log::info("Add Watermark");
2021-01-20 20:25:29 +00:00
2021-01-20 20:59:42 +00:00
2021-01-20 20:43:58 +00:00
$waterMarkImage = Image::query()->where("id", "=", $tenant->watermark)->firstOrFail();
2021-01-20 20:59:42 +00:00
$cacheNameWatermark = "watermark/".$waterMarkImage->id."_".$waterMarkImage->filename;
2021-01-20 20:25:29 +00:00
$watermarkSource = $this->getImage($tenant, $gallery, $waterMarkImage, $cacheNameWatermark);
$watermark = $manager->make($watermarkSource);
2021-01-20 18:51:12 +00:00
if($watermark->getWidth() > $img->getWidth()) {
$newWidth = $img->getWidth() / 1.2;
$newHeight = $watermark->getHeight() * ($newWidth / $watermark->getWidth());
$watermark->resize($newWidth, $newHeight);
}
if($watermark->getHeight() > $img->getHeight()) {
$newHeight = $img->getHeight() / 1.1;
$newWidth = $watermark->getWidth() * ($newHeight / $watermark->getHeight());
$watermark->resize($newWidth, $newHeight);
}
$watermark->opacity(40);
$img->insert($watermark, 'center');
2021-01-20 20:25:29 +00:00
/*
* Reddit like balken unten
2021-01-20 18:51:12 +00:00
$img->resizeCanvas(0, 28, 'top', true, "#000000");
$img->text('www.kuvia.cloud/'.$tenant->url, $img->getWidth() - 10, $img->getHeight() - 25, function($font) {
$font->file(storage_path("OpenSans-Light.ttf"));
$font->size(18);
$font->color('#ffffff');
$font->align('right');
$font->valign('top');
//$font->angle(45);
});
2021-01-20 20:25:29 +00:00
*/
2021-01-20 20:43:58 +00:00
} else {
Log::info("No Watermark");
2021-01-20 18:51:12 +00:00
}
$img->save($tmpfname);
2021-01-20 21:35:41 +00:00
Log::info("Update cache for Image", ["id" => $image->id, "size" => $this->size]);
2021-01-20 18:51:12 +00:00
Storage::disk("cache")->put($cacheName."_".$this->size, file_get_contents($tmpfname));
2021-01-20 21:35:41 +00:00
$image->refreshCache = false;
$image->saveOrFail();
2021-01-20 18:51:12 +00:00
unlink($tmpfname);
//Watermark
//Storage::disk("cache")->put($cacheName."_".$this->size, $image->getImageAsString(IMAGETYPE_JPEG, 100));
2021-01-17 02:09:02 +00:00
}
2021-01-20 20:43:58 +00:00
private function getImage(Tenant $tenant, Gallery $gallery, Image $image, $cacheName) {
2021-01-20 21:35:41 +00:00
if (Storage::disk('cache')->exists($cacheName."_orginal") && $image->refreshCache == false) {
2021-01-20 20:25:29 +00:00
$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);
}
}
return $file;
}
2021-01-17 02:09:02 +00:00
2021-01-20 18:51:12 +00:00
2021-01-17 02:09:02 +00:00
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();
}
}