pass to laravel

This commit is contained in:
Cassandre Cantet 2020-11-27 23:35:34 +01:00
parent f36d2ad705
commit bbec348e60
7 changed files with 236 additions and 133 deletions

View File

@ -2,18 +2,29 @@
namespace Meoran\Images\Http\Controllers; namespace Meoran\Images\Http\Controllers;
use App\Http\Controllers\Controller;
use Closure; use Closure;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Intervention\Image\Constraint; use Illuminate\Http\Response;
use Intervention\Image\Exception\ImageException; use Illuminate\Validation\ValidationException;
use Intervention\Image\Exception\NotSupportedException;
use Laravel\Lumen\Routing\Controller as BaseController;
use Meoran\Images\Model\Image; use Meoran\Images\Model\Image;
use Meoran\Images\Templates\Custom; use Meoran\Images\Templates\Custom;
class ImagesController extends BaseController /**
* Class ImagesController
* @package Meoran\Images\Http\Controllers
*/
class ImagesController extends Controller
{ {
/**
* @param $filename
* @return Application|ResponseFactory|Response
*/
public function get($filename) public function get($filename)
{ {
$template = app('request')->input('template'); $template = app('request')->input('template');
@ -30,7 +41,12 @@ class ImagesController extends BaseController
} }
} }
public function upload(Request $request) /**
* @param Request $request
* @return JsonResponse
* @throws ValidationException
*/
public function upload(Request $request): JsonResponse
{ {
$this->validate($request, [ $this->validate($request, [
'image' => 'required|file|image' 'image' => 'required|file|image'
@ -79,6 +95,12 @@ class ImagesController extends BaseController
abort(404); abort(404);
} }
/**
* @param $template
* @param $filename
* @return Application|ResponseFactory|Response
* @throws BindingResolutionException
*/
public function getImage($template, $filename) public function getImage($template, $filename)
{ {
$template = $this->getTemplate($template); $template = $this->getTemplate($template);
@ -97,6 +119,13 @@ class ImagesController extends BaseController
return $this->buildResponse($content); return $this->buildResponse($content);
} }
/**
* @param $template
* @param $path
* @param null $image
* @return mixed
* @throws BindingResolutionException
*/
private function applyTemplate($template, $path, $image = null) private function applyTemplate($template, $path, $image = null)
{ {
if (empty($image)) { if (empty($image)) {
@ -106,12 +135,12 @@ class ImagesController extends BaseController
if ($template instanceof Closure) { if ($template instanceof Closure) {
// build from closure callback template // build from closure callback template
return $template($image->make($path)); return $template($image->make($path));
} else { }
// build from filter template
$res = $image->make($path)->filter($template); // build from filter template
if ($res === null) { $res = $image->make($path)->filter($template);
abort(404); if ($res === null) {
} abort(404);
} }
} }
@ -140,6 +169,10 @@ class ImagesController extends BaseController
} }
} }
/**
* @param $content
* @return Application|ResponseFactory|Response
*/
private function buildResponse($content) private function buildResponse($content)
{ {
// define mime type // define mime type
@ -155,7 +188,11 @@ class ImagesController extends BaseController
)); ));
} }
private function getCustomTemplate($templateName) /**
* @param $templateName
* @return Custom
*/
private function getCustomTemplate($templateName): Custom
{ {
$custom = new Custom(); $custom = new Custom();
$custom->actions = [$templateName]; $custom->actions = [$templateName];

View File

@ -2,48 +2,78 @@
namespace Meoran\Images\Model; namespace Meoran\Images\Model;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany; use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Str;
use Intervention\Image\Exception\NotReadableException; use Intervention\Image\Exception\NotReadableException;
use Intervention\Image\Image as InterventionImage; use Intervention\Image\Image as InterventionImage;
use Meoran\Images\Exception\InvalidContent; use Meoran\Images\Exception\InvalidContent;
use RuntimeException;
use Spatie\ImageOptimizer\OptimizerChain; use Spatie\ImageOptimizer\OptimizerChain;
/** /**
* Class Image * Class Image
* @property InterventionImage $content * @property InterventionImage $content
* @property string filename
* @property string hash
* @package App\Model * @package App\Model
*/ */
class Image extends Model class Image extends Model
{ {
/** /** @var string[] */
* @var InterventionImage $_content
*/
protected $_content;
protected $table = 'images';
public $fillable = [ public $fillable = [
'content', 'content',
'filename', 'filename',
'created_at', 'created_at',
'updated_at' 'updated_at',
]; ];
/** @var InterventionImage */
protected $_content;
/** @var string */
protected $table = 'images';
/** @var string[] */
protected $dates = ['created_at', 'updated_at']; protected $dates = ['created_at', 'updated_at'];
/** @var string[] */
protected $appends = ['url']; protected $appends = ['url'];
protected $hidden = [];
public function getUrlAttribute() /**
* @param string $filename
* @return string
*/
public static function sanitizeFilename(string $filename): string
{ {
$route = config('image.route'); return Str::slug($filename, '-');
if (empty($route)) {
return null;
}
return app('url')->to('/' . $route . '/' . $this->filename);
} }
protected static function boot() /**
* @param Model $class
* @param $relationName
* @return MorphToMany
*/
public static function createRelation(Model $class, $relationName): MorphToMany
{
$instance = $class->newRelatedInstance(static::class);
$foreignPivotKey = 'relation_id';
$relatedPivotKey = 'image_id';
$table = 'associate_images';
$name = 'relation';
$morph = new MorphToMany(
$instance->newQuery(), $class, $name, $table,
$foreignPivotKey, $relatedPivotKey, $class->getKeyName(),
$instance->getKeyName(), $relationName, false
);
$morph->withPivot('position');
return $morph;
}
/**
*
*/
protected static function boot(): void
{ {
parent::boot(); parent::boot();
@ -67,7 +97,56 @@ class Image extends Model
} }
public function getPath() protected function savePicture()
{
if (empty($this->content)) {
return true;
}
$this->generateFilename();
return $this->saveContent();
}
/**
* @param boolean $force
*/
public function generateFilename($force = false): void
{
if ($this->filename && !$force) {
return;
}
$this->filename = self::generateRandomFilename();
}
/**+
* @return string
*/
public static function generateRandomFilename(): string
{
return mb_strtolower(Str::random(60));
}
/**
* @return InterventionImage
*/
protected function saveContent(): InterventionImage
{
if (empty($this->content)) {
throw new \InvalidArgumentException("Content is Empty");
}
$path = $this->getPath();
$dir = dirname($path);
if (!is_dir($dir) && !mkdir($dir, 0775, true) && !is_dir($dir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
}
$res = $this->content->save($path);
app(OptimizerChain::class)->optimize($path);
return $res;
}
/**
* @return string|null
*/
public function getPath(): ?string
{ {
if (empty($this->filename)) { if (empty($this->filename)) {
return null; return null;
@ -76,29 +155,48 @@ class Image extends Model
return self::getAbsolutePath($this->filename); return self::getAbsolutePath($this->filename);
} }
static function getAbsolutePath($filename) /**
* @param $filename
* @return string
*/
public static function getAbsolutePath($filename): string
{ {
$basePath = config('image.path'); $basePath = config('image.path');
if (empty($basePath)) { if (empty($basePath)) {
throw new \Exception('You must defined config image.path'); throw new RuntimeException('You must defined config image.path');
} }
$parts = array_slice(str_split(md5($filename), 2), 0, 2); $parts = array_slice(str_split(md5($filename), 2), 0, 2);
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename; return $basePath . '/' . implode('/', $parts) . '/' . $filename;
return $path;
} }
static function generateRandomFilename() /**
* @return bool
*/
protected function deletePicture(): bool
{ {
return mb_strtolower(str_random(60)); $path = $this->getPath();
if (is_file($path)) {
return unlink($path);
}
return true;
} }
static function sanitizeFilename($filename) /**
* @return string
*/
public function getUrlAttribute(): ?string
{ {
return str_slug($filename, '-'); $route = config('image.route');
if (empty($route)) {
return null;
}
return app('url')->to('/' . $route . '/' . $this->filename);
} }
public function setFilenameAttribute($value) /**
* @param string $value
*/
public function setFilenameAttribute(string $value): void
{ {
$pattern = '/[^a-z_\-\.0-9]/i'; $pattern = '/[^a-z_\-\.0-9]/i';
$patternLetter = '/[a-z0-9]+/i'; $patternLetter = '/[a-z0-9]+/i';
@ -108,25 +206,27 @@ class Image extends Model
$this->attributes['filename'] = $value; $this->attributes['filename'] = $value;
} }
public function fileExist() /**
* @return bool
*/
public function fileExist(): bool
{ {
return is_file($this->getPath()); return is_file($this->getPath());
} }
public function generateFilename($force = false) /**
{ * @param $content
if ($this->filename && !$force) { * @throws BindingResolutionException
return; */
} public function setContentAttribute($content): void
$this->filename = self::generateRandomFilename();
}
public function setContentAttribute($content)
{ {
$this->_content = app('image')->make($content)->orientate(); $this->_content = app('image')->make($content)->orientate();
} }
public function getHashAttribute() /**
* @return string|null
*/
public function getHashAttribute(): ?string
{ {
if (empty($this->content)) { if (empty($this->content)) {
return null; return null;
@ -137,7 +237,10 @@ class Image extends Model
return sha1($this->content->getEncoded()); return sha1($this->content->getEncoded());
} }
public function getExtensionAttribute() /**
* @return string|null
*/
public function getExtensionAttribute(): ?string
{ {
$mime = $this->content->mime(); $mime = $this->content->mime();
if (empty($mime)) { if (empty($mime)) {
@ -146,21 +249,33 @@ class Image extends Model
return str_replace('image/', '', $mime); return str_replace('image/', '', $mime);
} }
public function getMimeAttribute() { /**
* @return string|null
*/
public function getMimeAttribute(): ?string
{
if (empty($this->content)) { if (empty($this->content)) {
return null; return null;
} }
return $this->content->mime(); return $this->content->mime();
} }
public function same(Image $image) /**
* @param Image $image
* @return bool
*/
public function same(Image $image): bool
{ {
$hash1 = $this->hash; $hash1 = $this->hash;
$hash2 = $image->hash; $hash2 = $image->hash;
return !empty($hash1) && !empty($hash2) && $hash1 === $hash2; return !empty($hash1) && !empty($hash2) && $hash1 === $hash2;
} }
public function getContentAttribute($value) /**
* @return InterventionImage|null
* @throws BindingResolutionException
*/
public function getContentAttribute(): ?InterventionImage
{ {
if (empty($this->_content)) { if (empty($this->_content)) {
try { try {
@ -172,67 +287,4 @@ class Image extends Model
return $this->_content; return $this->_content;
} }
protected function savePicture()
{
if (empty($this->content)) {
return true;
}
$this->generateFilename();
return $this->saveContent();
}
protected function saveContent()
{
if (empty($this->content)) {
throw new \InvalidArgumentException("Content is Empty");
}
$path = $this->getPath();
$dir = dirname($path);
if (!is_dir($dir)) {
mkdir($dir, 0775, true);
}
$res = $this->content->save($path);
app(OptimizerChain::class)->optimize($path);
return $res;
}
protected function deletePicture()
{
$path = $this->getPath();
if (is_file($path)) {
return unlink($path);
}
return true;
}
/**
* @param Model $class
* @param $relationName
* @return MorphToMany
*/
public static function createRelation(Model $class, $relationName)
{
$instance = $class->newRelatedInstance(static::class);
$foreignPivotKey = 'relation_id';
$relatedPivotKey = 'image_id';
$table = 'associate_images';
$name = 'relation';
$morph = new MorphToMany(
$instance->newQuery(), $class, $name, $table,
$foreignPivotKey, $relatedPivotKey, $class->getKeyName(),
$instance->getKeyName(), $relationName, false
);
$morph->withPivot('position');
return $morph;
}
public function toArray()
{
$attributes = parent::toArray();
return $attributes;
}
} }

View File

@ -7,11 +7,19 @@ use Intervention\Image\Exception\NotSupportedException;
use Intervention\Image\Filters\FilterInterface; use Intervention\Image\Filters\FilterInterface;
use Intervention\Image\Image; use Intervention\Image\Image;
/**
* Class Custom
* @package Meoran\Images\Templates
*/
class Custom implements FilterInterface class Custom implements FilterInterface
{ {
public $actions = null; public $actions;
public function applyFilter(Image $image) /**
* @param Image $image
* @return Image|null
*/
public function applyFilter(Image $image): ?Image
{ {
$actions = $this->actions; $actions = $this->actions;
if ($actions === null) { if ($actions === null) {
@ -31,16 +39,18 @@ class Custom implements FilterInterface
if (isset($exploded[1])) { if (isset($exploded[1])) {
$params = explode(',', $exploded[1]); $params = explode(',', $exploded[1]);
} }
$params[] = function (Constraint $constraint) { $params[] = static function (Constraint $constraint) {
$constraint->upsize(); $constraint->upsize();
$constraint->aspectRatio(); $constraint->aspectRatio();
}; };
$params = array_map(function($el) { $params = array_map(static function($el) {
if (is_string($el) && $el === 'null') return null; if (is_string($el) && $el === 'null') {
return null;
}
return $el; return $el;
},$params); },$params);
try { try {
call_user_func_array([$image,$methodName], $params); call_user_func_array([$image, $methodName], $params);
} catch (NotSupportedException $exception) { } catch (NotSupportedException $exception) {
return null; return null;
} }
@ -48,4 +58,4 @@ class Custom implements FilterInterface
return $image; return $image;
} }
} }

View File

@ -8,11 +8,15 @@ use Intervention\Image\Image;
class Large implements FilterInterface class Large implements FilterInterface
{ {
public function applyFilter(Image $image) /**
* @param Image $image
* @return Image
*/
public function applyFilter(Image $image): Image
{ {
return $image->resize(1920, null, function (Constraint $constraint) { return $image->resize(1920, null, function (Constraint $constraint) {
$constraint->upsize(); $constraint->upsize();
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
} }
} }

View File

@ -8,11 +8,11 @@ use Intervention\Image\Image;
class Medium implements FilterInterface class Medium implements FilterInterface
{ {
public function applyFilter(Image $image) public function applyFilter(Image $image): Image
{ {
return $image->resize(960, null, function (Constraint $constraint) { return $image->resize(960, null, function (Constraint $constraint) {
$constraint->upsize(); $constraint->upsize();
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
} }
} }

View File

@ -8,11 +8,11 @@ use Intervention\Image\Image;
class Small implements FilterInterface class Small implements FilterInterface
{ {
public function applyFilter(Image $image) public function applyFilter(Image $image): Image
{ {
return $image->resize(480, null, function (Constraint $constraint) { return $image->resize(480, null, function (Constraint $constraint) {
$constraint->upsize(); $constraint->upsize();
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
} }
} }

View File

@ -8,11 +8,11 @@ use Intervention\Image\Image;
class Thumb implements FilterInterface class Thumb implements FilterInterface
{ {
public function applyFilter(Image $image) public function applyFilter(Image $image): Image
{ {
return $image->resize(120, null, function (Constraint $constraint) { return $image->resize(120, null, function (Constraint $constraint) {
$constraint->upsize(); $constraint->upsize();
$constraint->aspectRatio(); $constraint->aspectRatio();
}); });
} }
} }