Refactor
This commit is contained in:
parent
04f31bbfcf
commit
0d055c1edf
|
@ -6,7 +6,6 @@ use Aweos\Resizer\Lib\MediaPath;
|
|||
use Aweos\Resizer\Models\Setting;
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Support\Collection;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Media\Classes\MediaLibrary;
|
||||
use October\Rain\Resize\Resizer;
|
||||
use Storage;
|
||||
|
@ -86,36 +85,7 @@ class ImageResizer
|
|||
private function generateVersions(): void
|
||||
{
|
||||
foreach ($this->possibleSizes() as $size) {
|
||||
$temp = microtime().'.'.$this->file->extension();
|
||||
|
||||
$r = app(ImageManager::class)->make($this->file->root());
|
||||
if ($this->file->type() === 'image/jpeg') {
|
||||
$r->fit($size->get('width'), $size->get('height'), fn ($constraint) => $constraint->upsize())
|
||||
->save($this->disk->path($temp));
|
||||
}
|
||||
if ($this->file->type() === 'image/png') {
|
||||
app(ImageManager::class)
|
||||
->canvas($size->get('width'), $size->get('width'))->insert($r, 'center')
|
||||
->save($this->disk->path($temp));
|
||||
}
|
||||
|
||||
list($destWidth, $destHeight) = getimagesize($this->disk->path($temp));
|
||||
|
||||
$versionFilename = $this->file->versionsDirPath().
|
||||
'/'.
|
||||
$this->file->filename().
|
||||
'-'.
|
||||
$destWidth.
|
||||
'x'.
|
||||
$destHeight.
|
||||
'.'.
|
||||
$this->file->extension();
|
||||
|
||||
if ($this->disk->exists($versionFilename)) {
|
||||
$this->disk->delete($versionFilename);
|
||||
}
|
||||
|
||||
$this->disk->move($temp, $versionFilename);
|
||||
$this->file->compressor()->resize($this->file->root(), $this->file->versionsDirPath(), $size);
|
||||
}
|
||||
|
||||
foreach ($this->file->versions() as $version) {
|
||||
|
|
|
@ -7,9 +7,21 @@ abstract class Compressor
|
|||
|
||||
abstract function make(string $path): array;
|
||||
|
||||
abstract protected function getExtension();
|
||||
|
||||
public function tmpPath(): string
|
||||
{
|
||||
return "/tmp/".str_slug(microtime());
|
||||
}
|
||||
|
||||
protected function versionFilename(string $source, $width, $height): string
|
||||
{
|
||||
return pathinfo($source, PATHINFO_FILENAME).
|
||||
'-'.
|
||||
$width.
|
||||
'x'.
|
||||
$height.
|
||||
'.'.$this->getExtension();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,6 +4,11 @@ namespace Aweos\Resizer\Compressors;
|
|||
|
||||
class DefaultCompressor extends Compressor {
|
||||
|
||||
public function getExtension(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function make(string $path): array
|
||||
{
|
||||
return [];
|
||||
|
|
|
@ -2,11 +2,20 @@
|
|||
|
||||
namespace Aweos\Resizer\Compressors;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Storage;
|
||||
|
||||
class JpgCompressor extends Compressor {
|
||||
|
||||
protected function getExtension(): string
|
||||
{
|
||||
return 'jpg';
|
||||
}
|
||||
|
||||
public function make(string $path): array
|
||||
{
|
||||
$output = "/tmp/".str_slug(microtime());
|
||||
$output = $this->tmpPath();
|
||||
$mimetype = mime_content_type($path);
|
||||
|
||||
system('imagemin '.escapeshellarg($path).' --plugin=jpegtran --plugin=mozjpeg --plugin.mozjpeg.quality=70 > '.escapeshellarg($output));
|
||||
|
@ -22,4 +31,31 @@ class JpgCompressor extends Compressor {
|
|||
return true;
|
||||
}
|
||||
|
||||
public function resize(string $source, string $destination, Collection $size): void
|
||||
{
|
||||
$extension = pathinfo($source, PATHINFO_EXTENSION);
|
||||
$temp = microtime().'.'.$extension;
|
||||
|
||||
$r = app(ImageManager::class)->make($source)
|
||||
->fit($size->get('width'), $size->get('height'), fn ($constraint) => $constraint->upsize())
|
||||
->save(Storage::path($temp));
|
||||
|
||||
list($destWidth, $destHeight) = getimagesize(Storage::path($temp));
|
||||
|
||||
$versionFilename = $destination.
|
||||
'/'.
|
||||
pathinfo($source, PATHINFO_FILENAME).
|
||||
'-'.
|
||||
$destWidth.
|
||||
'x'.
|
||||
$destHeight.
|
||||
'.jpg';
|
||||
|
||||
if (Storage::exists($versionFilename)) {
|
||||
Storage::delete($versionFilename);
|
||||
}
|
||||
|
||||
Storage::move($temp, $versionFilename);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,8 +2,17 @@
|
|||
|
||||
namespace Aweos\Resizer\Compressors;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Intervention\Image\ImageManager;
|
||||
use Storage;
|
||||
|
||||
class PngCompressor extends Compressor {
|
||||
|
||||
protected function getExtension(): string
|
||||
{
|
||||
return 'png';
|
||||
}
|
||||
|
||||
public function make(string $path): array
|
||||
{
|
||||
$output = "/tmp/".str_slug(microtime());
|
||||
|
@ -22,4 +31,25 @@ class PngCompressor extends Compressor {
|
|||
return true;
|
||||
}
|
||||
|
||||
public function resize(string $source, string $destination, Collection $size): void
|
||||
{
|
||||
$extension = pathinfo($source, PATHINFO_EXTENSION);
|
||||
$temp = microtime().'.'.$extension;
|
||||
|
||||
$r = app(ImageManager::class)->make($source);
|
||||
app(ImageManager::class)
|
||||
->canvas($size->get('width'), $size->get('height'))->insert($r, 'center')
|
||||
->save(Storage::path($temp));
|
||||
|
||||
list($destWidth, $destHeight) = getimagesize(Storage::path($temp));
|
||||
|
||||
$versionFilename = $destination.'/'.$this->versionFilename($source, $destWidth, $destHeight);
|
||||
|
||||
if (Storage::exists($versionFilename)) {
|
||||
Storage::delete($versionFilename);
|
||||
}
|
||||
|
||||
Storage::move($temp, $versionFilename);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -205,8 +205,9 @@ class ResizerTest extends TestCase
|
|||
$media->put('/pages/test.jpg', UploadedFile::fake()->image('test.jpg', 500, 500)->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/test.jpg', null]);
|
||||
|
||||
$this->assertHasFile('pages/test-100x100.jpg');
|
||||
$this->assertFileCount(2, 'pages');
|
||||
$this->assertHasFile('pages/test-100x100.jpg');
|
||||
$this->assertHasFile('pages/test-500x500.jpg');
|
||||
}
|
||||
|
||||
public function testDontUpsizeAPngImage(): void
|
||||
|
@ -216,10 +217,10 @@ class ResizerTest extends TestCase
|
|||
Setting::set('breakpoints', ['100', '600']);
|
||||
|
||||
$media = MediaLibrary::instance();
|
||||
$media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 500)->get());
|
||||
$media->put('/pages/test.png', UploadedFile::fake()->image('test.png', 500, 1000)->get());
|
||||
Event::fire('media.file.upload', [null, '/pages/test.png', null]);
|
||||
|
||||
$this->assertHasFile('pages/test-100x100.png');
|
||||
$this->assertHasFile('pages/test-100x200.png');
|
||||
$this->assertFileCount(2, 'pages');
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue