136 lines
4.0 KiB
PHP
136 lines
4.0 KiB
PHP
<?php namespace Aweos\Resizer;
|
|
|
|
use Aweos\Resizer\Classes\FileObserver;
|
|
use Aweos\Resizer\Classes\ImageResizer;
|
|
use Aweos\Resizer\Classes\ResizeJob;
|
|
use Aweos\Resizer\Classes\TagGenerator;
|
|
use Aweos\Resizer\Console\ClearOld;
|
|
use Aweos\Resizer\Console\ResizeMake;
|
|
use Aweos\Resizer\Console\ResizePurge;
|
|
use Aweos\Resizer\FormWidgets\Responsiveimage;
|
|
use Aweos\Resizer\Models\Setting;
|
|
use Event;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Queue;
|
|
use Storage;
|
|
use System\Classes\MediaLibrary;
|
|
use System\Classes\PluginBase;
|
|
use System\Models\File;
|
|
|
|
/**
|
|
* resizer Plugin Information File
|
|
*/
|
|
class Plugin extends PluginBase
|
|
{
|
|
/**
|
|
* Returns information about this plugin.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function pluginDetails()
|
|
{
|
|
return [
|
|
'name' => 'resizer',
|
|
'description' => 'No description provided yet...',
|
|
'author' => 'aweos',
|
|
'icon' => 'icon-leaf'
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Register method, called when the plugin is first registered.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function register()
|
|
{
|
|
$this->registerConsoleCommand('resizer.resizemake', ResizeMake::class);
|
|
$this->registerConsoleCommand('resizer.resizepurge', ResizePurge::class);
|
|
$this->registerConsoleCommand('resizer.clearold', ClearOld::class);
|
|
}
|
|
|
|
/**
|
|
* Boot method, called right before the request route.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function boot()
|
|
{
|
|
app()->bind(ImageResizer::class, function() {
|
|
$disk = (new File())->getDisk();
|
|
$dir = (new File(['is_public' => true]))->getStorageDirectory().'c/';
|
|
$media = MediaLibrary::instance();
|
|
|
|
return new ImageResizer($disk, $dir, $media);
|
|
});
|
|
|
|
Event::listen('media.file.upload', function($widget, $filePath, $uploadedFile) {
|
|
if (app(FileObserver::class)->shouldProcessFile($filePath)) {
|
|
Queue::push(ResizeJob::class, [$filePath]);
|
|
}
|
|
});
|
|
Event::listen('media.file.delete', function($widget, $filePath) {
|
|
app(FileObserver::class)->delete($filePath);
|
|
});
|
|
|
|
Event::listen('media.file.move', function($widget, $old, $new) {
|
|
app(FileObserver::class)->rename($old, $new);
|
|
});
|
|
Event::listen('media.file.rename', function($widget, $old, $new) {
|
|
app(FileObserver::class)->rename($old, $new);
|
|
});
|
|
}
|
|
|
|
public function registerSettings() {
|
|
return [
|
|
'resizer' => [
|
|
'label' => 'Resizer Settings',
|
|
'description' => 'Change how images are resized and compressed',
|
|
'category' => 'Base',
|
|
'icon' => 'icon-cog',
|
|
'class' => '\Aweos\Resizer\Models\Setting',
|
|
'order' => 500,
|
|
'keywords' => 'setting',
|
|
'permissions' => ['aweos.resizer.*']
|
|
]
|
|
];
|
|
}
|
|
|
|
public function registerMarkupTags() {
|
|
return [
|
|
'filters' => [
|
|
'resize' => function($media, $size = 'original') {
|
|
$normalPath = app(FileObserver::class)->normalizePath($media);
|
|
return Cache::rememberForever("resize.{$size}.{$normalPath}", fn () => app(TagGenerator::class)->generate($media, $size));
|
|
},
|
|
]
|
|
];
|
|
}
|
|
|
|
public function breakpointsToSizes($breakpoints) {
|
|
$s = [];
|
|
|
|
foreach ($breakpoints as $size => $bp) {
|
|
if ($size === 'max') { continue; }
|
|
|
|
$s[] = '(max-width: '.$size.'px) '.$bp;
|
|
}
|
|
|
|
if (array_key_exists('max', $breakpoints)) {
|
|
$s[] = $breakpoints['max'];
|
|
}
|
|
|
|
return 'sizes="'.implode(', ', $s).'"';
|
|
}
|
|
|
|
public function registerSchedule($schedule) {
|
|
$schedule->command('resize:make')->dailyAt('01:00');
|
|
}
|
|
|
|
public function registerFormWidgets() {
|
|
return [
|
|
Responsiveimage::class => 'responsiveimage'
|
|
];
|
|
}
|
|
}
|