86 lines
2.4 KiB
PHP
86 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Aweos\Resizer\Classes;
|
|
|
|
use Storage;
|
|
use Aweos\Resizer\Models\Setting;
|
|
use October\Rain\Database\Attach\Resizer;
|
|
|
|
class CompressJob {
|
|
|
|
public $path;
|
|
public $filename;
|
|
public $disk;
|
|
public $strategy;
|
|
public $sizes;
|
|
public $maxFilesize = 1920;
|
|
|
|
public function fire($job, $data) {
|
|
$this->path = $data['path'];
|
|
$this->filename = $data['filename'];
|
|
$this->disk = $data['disk'];
|
|
$this->strategy = $data['strategy'];
|
|
$this->crop = $data['crop'];
|
|
|
|
$this->sizes = Setting::get('srcx');
|
|
|
|
$this->crop();
|
|
$this->createVersions();
|
|
}
|
|
|
|
public function crop() {
|
|
if ($this->crop === null) {
|
|
return;
|
|
}
|
|
|
|
$fullPath = Storage::disk($this->disk)->path($this->path.'/'.$this->filename);
|
|
|
|
Storage::disk($this->disk)->makeDirectory($this->getStrategy()->croppedPath());
|
|
|
|
$r = Resizer::open($fullPath);
|
|
|
|
$r->crop(floor($this->crop['x']), floor($this->crop['y']), floor($this->crop['w']), floor($this->crop['h']));
|
|
|
|
$this->path = $this->getStrategy()->croppedPath($this->filename);
|
|
$this->filename = $this->getStrategy()->croppedFilename($this->filename, $this->crop);
|
|
|
|
$r->save(Storage::disk($this->disk)->path($this->path.'/'.$this->filename));
|
|
}
|
|
|
|
public function createVersions()
|
|
{
|
|
$fullPath = Storage::disk($this->disk)->path($this->path.'/'.$this->filename);
|
|
[ $width, $height ] = getimagesize($fullPath);
|
|
|
|
if ($width > $this->maxFilesize) {
|
|
$r = Resizer::open($fullPath);
|
|
$r->resize($this->maxFilesize, 0);
|
|
$r->save($fullPath);
|
|
}
|
|
|
|
[ $width, $height ] = getimagesize($fullPath);
|
|
|
|
Storage::disk($this->disk)->makeDirectory($this->getStrategy()->publicPath($this->filename));
|
|
|
|
foreach ($this->sizes as $w) {
|
|
|
|
$filename = $this->getStrategy()->smallFilename($this->filename, $w);
|
|
|
|
if ($width < $w) {
|
|
continue;
|
|
}
|
|
|
|
$destination = Storage::disk($this->disk)->path($this->getStrategy()->publicPath($this->filename).'/'.$this->getStrategy()->smallFilename($this->filename, $w));
|
|
|
|
$r = Resizer::open($fullPath);
|
|
$r->resize($w, 0);
|
|
$r->save($destination);
|
|
}
|
|
}
|
|
|
|
private function getStrategy() {
|
|
return app($this->strategy);
|
|
}
|
|
|
|
}
|