Changement des filtersO

This commit is contained in:
Cassandre Cantet 2017-10-10 01:07:03 +02:00
parent a34031f914
commit 9d190dbede
4 changed files with 77 additions and 8 deletions

View File

@ -8,6 +8,8 @@ return [
'small' => \Meoran\Images\Templates\Small::class,
'medium' => \Meoran\Images\Templates\Medium::class,
'large' => \Meoran\Images\Templates\Large::class,
'custom' => \Meoran\Images\Templates\Custom::class,
),
'lifetime' => 10,
'cache' => [

View File

@ -4,14 +4,21 @@ namespace Meoran\Images\Http\Controllers;
use Closure;
use Illuminate\Http\Request;
use Intervention\Image\Constraint;
use Intervention\Image\Exception\ImageException;
use Intervention\Image\Exception\NotSupportedException;
use Laravel\Lumen\Routing\Controller as BaseController;
use Meoran\Images\Model\Image;
use Meoran\Images\Templates\Custom;
use ReflectionFunction;
use ReflectionMethod;
class ImagesController extends BaseController
{
public function get($filename, $template = null)
public function get($filename)
{
$template = app('request')->input('template');
if ($template === null) {
$template = 'original';
}
@ -91,7 +98,10 @@ class ImagesController extends BaseController
return $template($image->make($path));
} else {
// build from filter template
return $image->make($path)->filter($template);
$res = $image->make($path)->filter($template);
if ($res === null) {
abort(404);
}
}
}
@ -101,9 +111,9 @@ class ImagesController extends BaseController
* @param string $template
* @return mixed
*/
private function getTemplate($template)
private function getTemplate($templateName)
{
$template = config("image.templates.{$template}");
$template = config("image.templates.{$templateName}");
switch (true) {
// closure template found
case is_callable($template):
@ -114,9 +124,8 @@ class ImagesController extends BaseController
return new $template;
default:
// template not found
abort(404);
break;
return $this->getCustomTemplate($templateName);
}
}
@ -132,4 +141,11 @@ class ImagesController extends BaseController
'Etag' => md5($content)
));
}
private function getCustomTemplate($templateName)
{
$custom = new Custom();
$custom->actions = [$templateName];
return $custom;
}
}

View File

@ -35,7 +35,7 @@ class ImagesServiceProvider extends ServiceProvider
'as' => 'uploadImage', 'uses' => '\Meoran\Images\Http\Controllers\ImagesController@upload'
]);
$this->app->get('images/{filename}[/{template}]', [
$this->app->get('images/{filename}', [
'as' => 'getPicture', 'uses' => '\Meoran\Images\Http\Controllers\ImagesController@get'
]);
}

51
src/Templates/Custom.php Executable file
View File

@ -0,0 +1,51 @@
<?php
namespace Meoran\Images\Templates;
use Intervention\Image\Constraint;
use Intervention\Image\Exception\NotSupportedException;
use Intervention\Image\Filters\FilterInterface;
use Intervention\Image\Image;
class Custom implements FilterInterface
{
public $actions = null;
public function applyFilter(Image $image)
{
$actions = $this->actions;
if ($actions === null) {
$actions = app('request')->input('actions');
}
if (empty($actions)) {
return null;
}
foreach ($actions as $action) {
$exploded = explode(':',$action,2);
if (isset($exploded[0])) {
$methodName = $exploded[0];
}
$params= [];
if (isset($exploded[1])) {
$params = explode(',', $exploded[1]);
}
$params[] = function (Constraint $constraint) {
$constraint->upsize();
$constraint->aspectRatio();
};
$params = array_map(function($el) {
if (is_string($el) && $el === 'null') return null;
return $el;
},$params);
try {
call_user_func_array([$image,$methodName], $params);
} catch (NotSupportedException $exception) {
return null;
}
}
return $image;
}
}