oc-resizer-plugin/classes/CompressJob.php

86 lines
2.2 KiB
PHP

<?php
namespace Aweos\Resizer\Classes;
use Storage;
use Aweos\Resizer\Models\Setting;
use October\Rain\Database\Attach\Resizer;
use Aweos\Resizer\Models\Attachment;
class CompressJob {
public $attachment_id;
public $maxFilesize = 1920;
public $disk = 'uploads';
public $fullPath;
public $crop;
public $sizes;
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->fullPath = Storage::disk($this->disk)->path($this->attachment->source->path);
$this->crop();
$this->createVersions();
}
public function crop() {
if ($this->crop === null) {
return;
}
$r = Resizer::open($this->fullPath);
$r->crop(floor($this->crop['x']), floor($this->crop['y']), floor($this->crop['w']), floor($this->crop['h']));
$r->save($this->fullPath);
}
public function createVersions()
{
[ $width, $height ] = getimagesize($this->fullPath);
Storage::disk($this->disk)->makeDirectory($this->attachment->path);
if ($width > $this->maxFilesize) {
$r = Resizer::open($this->fullPath);
$r->resize($this->maxFilesize, 0);
$r->save($this->fullPath);
}
$r = Resizer::open($this->fullPath);
$r->save(Storage::disk($this->disk)->path($this->attachment->getVersionPath('full')));
[ $width, $height ] = getimagesize($this->fullPath);
foreach ($this->sizes as $w) {
if ($width < $w) {
continue;
}
$r = Resizer::open($this->fullPath);
$r->resize($w, 0);
$r->save(Storage::disk($this->disk)->path($this->attachment->getVersionPath($w)));
}
}
private function getStrategy() {
return app($this->strategy);
}
private function shouldResize() {
return $this->attachment->source->isCroppable();
}
}