images/src/Templates/Custom.php

62 lines
1.6 KiB
PHP
Raw Normal View History

2017-10-10 01:07:03 +02:00
<?php
namespace Meoran\Images\Templates;
use Intervention\Image\Constraint;
use Intervention\Image\Exception\NotSupportedException;
use Intervention\Image\Filters\FilterInterface;
use Intervention\Image\Image;
2020-11-27 23:35:34 +01:00
/**
* Class Custom
* @package Meoran\Images\Templates
*/
2017-10-10 01:07:03 +02:00
class Custom implements FilterInterface
{
2020-11-27 23:35:34 +01:00
public $actions;
2017-10-10 01:07:03 +02:00
2020-11-27 23:35:34 +01:00
/**
* @param Image $image
* @return Image|null
*/
public function applyFilter(Image $image): ?Image
2017-10-10 01:07:03 +02:00
{
$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]);
}
2020-11-27 23:35:34 +01:00
$params[] = static function (Constraint $constraint) {
2017-10-10 01:07:03 +02:00
$constraint->upsize();
$constraint->aspectRatio();
};
2020-11-27 23:35:34 +01:00
$params = array_map(static function($el) {
if (is_string($el) && $el === 'null') {
return null;
}
2017-10-10 01:07:03 +02:00
return $el;
},$params);
try {
2020-11-27 23:35:34 +01:00
call_user_func_array([$image, $methodName], $params);
2017-10-10 01:07:03 +02:00
} catch (NotSupportedException $exception) {
return null;
}
}
return $image;
}
2020-11-27 23:35:34 +01:00
}