diff --git a/config/image.php b/config/image.php index d5f02e8..d4c8fbe 100755 --- a/config/image.php +++ b/config/image.php @@ -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' => [ diff --git a/src/Http/Controllers/ImagesController.php b/src/Http/Controllers/ImagesController.php index 5f155c0..a0b9692 100755 --- a/src/Http/Controllers/ImagesController.php +++ b/src/Http/Controllers/ImagesController.php @@ -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,10 @@ class ImagesController extends BaseController * @param string $template * @return mixed */ - private function getTemplate($template) + private function getTemplate($templateName) { - $template = config("images.templates.{$template}"); + + $template = config("image.templates.{$templateName}"); switch (true) { // closure template found case is_callable($template): @@ -114,9 +125,8 @@ class ImagesController extends BaseController return new $template; default: - // template not found - abort(404); - break; + return $this->getCustomTemplate($templateName); + } } @@ -132,4 +142,11 @@ class ImagesController extends BaseController 'Etag' => md5($content) )); } + + private function getCustomTemplate($templateName) + { + $custom = new Custom(); + $custom->actions = [$templateName]; + return $custom; + } } diff --git a/src/Providers/ImagesServiceProvider.php b/src/Providers/ImagesServiceProvider.php index f65278b..add5ec0 100755 --- a/src/Providers/ImagesServiceProvider.php +++ b/src/Providers/ImagesServiceProvider.php @@ -42,7 +42,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' ]); } diff --git a/src/Templates/Custom.php b/src/Templates/Custom.php new file mode 100755 index 0000000..443538f --- /dev/null +++ b/src/Templates/Custom.php @@ -0,0 +1,51 @@ +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; + } +} \ No newline at end of file