Add TagPresenter
This commit is contained in:
parent
ff2ac7d88c
commit
ffc17d5a34
|
@ -10,6 +10,7 @@ use Aweos\Resizer\Console\ResizeMake;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
use Aweos\Resizer\Console\ResizePurge;
|
use Aweos\Resizer\Console\ResizePurge;
|
||||||
use Aweos\Resizer\FormWidgets\Responsiveimage;
|
use Aweos\Resizer\FormWidgets\Responsiveimage;
|
||||||
|
use Aweos\Resizer\Classes\TagPresenter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* resizer Plugin Information File
|
* resizer Plugin Information File
|
||||||
|
@ -187,6 +188,9 @@ class Plugin extends PluginBase
|
||||||
|
|
||||||
return 'src="'.$l->getPathUrl($media).'" '.$srcset.' '.$s.' '.$normalSize;
|
return 'src="'.$l->getPathUrl($media).'" '.$srcset.' '.$s.' '.$normalSize;
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
'responsiveimage' => function($id, $data = []) {
|
||||||
|
return (new TagPresenter($id, $data))->cacheOutput();
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Aweos\Resizer\Classes;
|
||||||
|
|
||||||
|
use Storage;
|
||||||
|
use Aweos\Resizer\Models\Attachment;
|
||||||
|
use Aweos\Resizer\Models\Setting;
|
||||||
|
|
||||||
|
class TagPresenter {
|
||||||
|
|
||||||
|
public $attachment;
|
||||||
|
public $disk = 'uploads';
|
||||||
|
public $attrs;
|
||||||
|
public $breakpoints;
|
||||||
|
public $sizes;
|
||||||
|
|
||||||
|
public function __construct($id, $data = []) {
|
||||||
|
$this->attachment = Attachment::find($id);
|
||||||
|
$this->attrs = data_get($data, 'attrs');
|
||||||
|
$this->breakpoints = data_get($data, 'breakpoints', []);
|
||||||
|
$this->sizes = Setting::get('srcx');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function cacheOutput() {
|
||||||
|
return $this->output();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function output() {
|
||||||
|
ksort($this->breakpoints);
|
||||||
|
|
||||||
|
$files = collect(Storage::disk($this->disk)->files($this->attachment->path))->filter(function($file) {
|
||||||
|
return preg_match($this->attachment->versionRegex, $file);
|
||||||
|
})->map(function($file) {
|
||||||
|
$sizes = getimagesize(Storage::disk($this->disk)->path($file));
|
||||||
|
|
||||||
|
return collect([
|
||||||
|
'url' => Storage::disk($this->disk)->url($file),
|
||||||
|
'width' => $sizes[0],
|
||||||
|
'height' => $sizes[1]
|
||||||
|
]);
|
||||||
|
})->sortBy('width');
|
||||||
|
|
||||||
|
if ($files->isEmpty()) {
|
||||||
|
return 'src="'.$this->attachment->url.'"';
|
||||||
|
}
|
||||||
|
|
||||||
|
$sizes = $files->map(function($file) {
|
||||||
|
return "(max-width: {$file->get('width')}px) {$file->get('width')}px";
|
||||||
|
});
|
||||||
|
|
||||||
|
$srcset = $files->map(function($file) {
|
||||||
|
return "{$file->get('url')} {$file->get('width')}w";
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this->htmlAttributes(collect([
|
||||||
|
'width' => $files->last()->get('width'),
|
||||||
|
'height' => $files->last()->get('height'),
|
||||||
|
'sizes' => $sizes->implode(', '),
|
||||||
|
'srcset' => $srcset->implode(', '),
|
||||||
|
'alt' => $this->attachment->title
|
||||||
|
])).' '.$this->attrs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function htmlAttributes($attr) {
|
||||||
|
return $attr->map(function($value, $key) {
|
||||||
|
return "{$key}=\"{$value}\"";
|
||||||
|
})->implode(' ');
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue