oc-resizer-plugin/classes/CompressJob.php

75 lines
2.0 KiB
PHP
Raw Normal View History

2020-10-25 20:53:10 +01:00
<?php
namespace Aweos\Resizer\Classes;
use Storage;
use Aweos\Resizer\Models\Setting;
use October\Rain\Database\Attach\Resizer;
2020-10-27 01:49:52 +01:00
use Aweos\Resizer\Models\Attachment;
2020-10-25 20:53:10 +01:00
class CompressJob {
2020-10-27 01:49:52 +01:00
public $attachment_id;
2020-10-25 20:53:10 +01:00
public $maxFilesize = 1920;
2020-10-27 01:49:52 +01:00
public $disk = 'uploads';
public $fullPath;
public $crop;
public $sizes;
2020-10-25 20:53:10 +01:00
public function fire($job, $data) {
2020-10-27 01:49:52 +01:00
$this->attachment_id = $data['attachment_id'];
2020-10-25 20:53:10 +01:00
$this->crop = $data['crop'];
$this->sizes = Setting::get('srcx');
2020-10-27 01:49:52 +01:00
Storage::disk($this->disk)->makeDirectory('cropped');
$this->attachment = Attachment::find($this->attachment_id);
$this->fullPath = Storage::disk($this->disk)->path($this->attachment->source->path);
2020-10-25 20:53:10 +01:00
$this->crop();
$this->createVersions();
}
public function crop() {
if ($this->crop === null) {
return;
}
2020-10-27 01:49:52 +01:00
$r = Resizer::open($this->fullPath);
2020-10-25 20:53:10 +01:00
$r->crop(floor($this->crop['x']), floor($this->crop['y']), floor($this->crop['w']), floor($this->crop['h']));
2020-10-27 01:49:52 +01:00
$r->save($this->fullPath);
2020-10-25 20:53:10 +01:00
}
public function createVersions()
{
2020-10-27 01:49:52 +01:00
[ $width, $height ] = getimagesize($this->fullPath);
2020-10-25 20:53:10 +01:00
if ($width > $this->maxFilesize) {
2020-10-27 01:49:52 +01:00
$r = Resizer::open($this->fullPath);
2020-10-25 20:53:10 +01:00
$r->resize($this->maxFilesize, 0);
2020-10-27 01:49:52 +01:00
$r->save($this->fullPath);
2020-10-25 20:53:10 +01:00
}
2020-10-27 01:49:52 +01:00
[ $width, $height ] = getimagesize($this->fullPath);
2020-10-25 20:53:10 +01:00
2020-10-27 01:49:52 +01:00
Storage::disk($this->disk)->makeDirectory($this->attachment->path);
2020-10-25 20:53:10 +01:00
foreach ($this->sizes as $w) {
if ($width < $w) {
continue;
}
2020-10-27 01:49:52 +01:00
Storage::disk($this->disk)->makeDirectory($this->attachment->path);
$r = Resizer::open($this->fullPath);
2020-10-25 20:53:10 +01:00
$r->resize($w, 0);
2020-10-27 01:49:52 +01:00
$r->save(Storage::disk($this->disk)->path($this->attachment->getVersionPath($w)));
2020-10-25 20:53:10 +01:00
}
}
private function getStrategy() {
return app($this->strategy);
}
}