oc-resizer-plugin/Plugin.php

213 lines
6.8 KiB
PHP

<?php namespace Aweos\Resizer;
use Backend;
use System\Classes\PluginBase;
use System\Classes\MediaLibrary;
use Aweos\Resizer\Models\Setting;
use Aweos\Resizer\Console\ClearOld;
use Aweos\Resizer\Console\ResizeMake;
use Illuminate\Support\Facades\Cache;
use Aweos\Resizer\Console\ResizePurge;
/**
* 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()
{
}
/**
* Registers any front-end components implemented in this plugin.
*
* @return array
*/
public function registerComponents()
{
return []; // Remove this line to activate
return [
'Aweos\Resizer\Components\MyComponent' => 'myComponent',
];
}
/**
* Registers any back-end permissions used by this plugin.
*
* @return array
*/
public function registerPermissions()
{
return []; // Remove this line to activate
return [
'aweos.resizer.some_permission' => [
'tab' => 'resizer',
'label' => 'Some permission'
],
];
}
/**
* Registers back-end navigation items for this plugin.
*
* @return array
*/
public function registerNavigation()
{
return []; // Remove this line to activate
return [
'resizer' => [
'label' => 'resizer',
'url' => Backend::url('aweos/resizer/mycontroller'),
'icon' => 'icon-leaf',
'permissions' => ['aweos.resizer.*'],
'order' => 500,
],
];
}
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, $breakpoints = [], $name = '') {
return Cache::remember('resized_image.'.$name.$media, 3600, function() use ($media, $breakpoints) {
ksort($breakpoints);
$l = MediaLibrary::instance();
$folders = Setting::get('folders');
$sizes = Setting::get('srcx');
$media = '/'.ltrim($media, '/');
$filename = basename($media);
$dirname = dirname($media);
$extension = pathinfo($media, PATHINFO_EXTENSION);
if (! $l->folderExists($dirname.'/c')) {
return 'src="'.$l->getPathUrl($media).'"';
}
$sizes = collect($l->listFolderContents($dirname.'/c'))
->filter(function($f) use ($sizes, $filename) {
foreach ($sizes as $size) {
if (pathinfo($f->path, PATHINFO_FILENAME).'.'.pathinfo($filename, PATHINFO_EXTENSION)
== pathinfo($filename, PATHINFO_FILENAME).'-'.$size.'t.'.pathinfo($filename, PATHINFO_EXTENSION)) {
return true;
}
if (pathinfo($f->path, PATHINFO_FILENAME).'.'.pathinfo($filename, PATHINFO_EXTENSION)
== pathinfo($filename, PATHINFO_FILENAME).'-'.$size.'.'.pathinfo($filename, PATHINFO_EXTENSION)) {
return true;
}
}
return false;
})
->map(function($fileVersion) use ($l) {
$sizes = getimagesize($l->getPathUrl($fileVersion->path));
return [$l->getPathUrl($fileVersion->path), $sizes[0], $sizes[1]];
})->sortBy(function($size) {
return $size[1];
})
;
if ($sizes->isEmpty()) {
return 'src="'.$l->getPathUrl($media).'"';
}
$srcset = 'srcset="';
$s = '';
foreach($sizes as $size) {
$srcset .= $size[0].' '.$size[1].'w, ';
$s .= '(max-width: '.$size[1].'px) '.$size[1].'px, ';
}
$s = 'sizes="'.substr($s, 0, -2).', 1920px"';
if (!empty($breakpoints)) {
$s = $this->breakpointsToSizes($breakpoints);
}
$srcset = substr($srcset, 0, -2).'"';
$normalSize = 'width="'.$sizes->last()[1].'" height="'.$sizes->last()[2].'"';
return 'src="'.$l->getPathUrl($media).'" '.$srcset.' '.$s.' '.$normalSize;
});
}
]
];
}
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');
}
}