images/src/Templates/Custom.php

51 lines
1.4 KiB
PHP
Executable File

<?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;
}
}