images/src/Model/Image.php

291 lines
7.1 KiB
PHP
Raw Normal View History

2017-10-03 17:05:46 +02:00
<?php
namespace Meoran\Images\Model;
2020-11-27 23:35:34 +01:00
use Illuminate\Contracts\Container\BindingResolutionException;
2017-10-03 17:05:46 +02:00
use Illuminate\Database\Eloquent\Model;
2017-10-04 14:41:23 +02:00
use Illuminate\Database\Eloquent\Relations\MorphToMany;
2020-11-27 23:35:34 +01:00
use Illuminate\Support\Str;
2017-10-03 18:10:59 +02:00
use Intervention\Image\Exception\NotReadableException;
2017-10-04 16:46:56 +02:00
use Intervention\Image\Image as InterventionImage;
2017-10-03 18:10:59 +02:00
use Meoran\Images\Exception\InvalidContent;
2020-11-27 23:35:34 +01:00
use RuntimeException;
2017-10-03 17:05:46 +02:00
use Spatie\ImageOptimizer\OptimizerChain;
/**
* Class Image
2017-10-11 13:37:06 +02:00
* @property InterventionImage $content
2020-11-27 23:35:34 +01:00
* @property string filename
* @property string hash
2017-10-03 17:05:46 +02:00
* @package App\Model
*/
class Image extends Model
{
2020-11-27 23:35:34 +01:00
/** @var string[] */
2017-10-03 17:05:46 +02:00
public $fillable = [
2018-10-16 00:11:32 +02:00
'content',
2017-10-04 00:11:25 +02:00
'filename',
2018-10-15 23:42:47 +02:00
'created_at',
2020-11-27 23:35:34 +01:00
'updated_at',
2017-10-03 17:05:46 +02:00
];
2020-11-27 23:35:34 +01:00
/** @var InterventionImage */
protected $_content;
/** @var string */
protected $table = 'images';
/** @var string[] */
2017-10-03 17:05:46 +02:00
protected $dates = ['created_at', 'updated_at'];
2020-11-27 23:35:34 +01:00
/** @var string[] */
2017-10-03 17:05:46 +02:00
protected $appends = ['url'];
2020-11-27 23:35:34 +01:00
/**
* @param string $filename
* @return string
*/
public static function sanitizeFilename(string $filename): string
2017-10-03 17:05:46 +02:00
{
2020-11-27 23:35:34 +01:00
return Str::slug($filename, '-');
2017-10-03 17:05:46 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @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
2017-10-03 17:05:46 +02:00
{
parent::boot();
2017-10-03 18:10:59 +02:00
static::creating(function (Image $model) {
2017-10-03 17:05:46 +02:00
if (empty($model->content)) {
2017-10-03 18:10:59 +02:00
throw new InvalidContent("Content must be defined to save image");
}
if (!($model->content instanceof InterventionImage)) {
throw new InvalidContent("Content must be an instance of Intervention\Image");
2017-10-03 17:05:46 +02:00
}
$model->savePicture();
});
static::updating(function (Image $model) {
$model->savePicture();
});
static::deleted(function (Image $model) {
$model->deletePicture();
});
}
2020-11-27 23:35:34 +01:00
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
2017-10-03 17:05:46 +02:00
{
if (empty($this->filename)) {
return null;
}
return self::getAbsolutePath($this->filename);
}
2020-11-27 23:35:34 +01:00
/**
* @param $filename
* @return string
*/
public static function getAbsolutePath($filename): string
2017-10-03 17:05:46 +02:00
{
$basePath = config('image.path');
2017-10-11 17:20:42 +02:00
if (empty($basePath)) {
2020-11-27 23:35:34 +01:00
throw new RuntimeException('You must defined config image.path');
2017-10-11 17:20:42 +02:00
}
2017-10-27 13:19:11 +02:00
$parts = array_slice(str_split(md5($filename), 2), 0, 2);
2020-11-27 23:35:34 +01:00
return $basePath . '/' . implode('/', $parts) . '/' . $filename;
2017-10-03 17:05:46 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @return bool
*/
protected function deletePicture(): bool
2017-10-03 17:05:46 +02:00
{
2020-11-27 23:35:34 +01:00
$path = $this->getPath();
if (is_file($path)) {
return unlink($path);
}
return true;
2017-10-03 17:05:46 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @return string
*/
public function getUrlAttribute(): ?string
2017-10-04 00:11:25 +02:00
{
2020-11-27 23:35:34 +01:00
$route = config('image.route');
if (empty($route)) {
return null;
}
return app('url')->to('/' . $route . '/' . $this->filename);
2017-10-04 00:11:25 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @param string $value
*/
public function setFilenameAttribute(string $value): void
2017-10-04 00:11:25 +02:00
{
$pattern = '/[^a-z_\-\.0-9]/i';
2017-10-11 13:37:06 +02:00
$patternLetter = '/[a-z0-9]+/i';
if (preg_match($pattern, $value) || !preg_match($patternLetter, $value)) {
2017-10-04 00:11:25 +02:00
throw new \InvalidArgumentException("Invalid filename. Must be only composed only with a-z, A-Z, 0-9 and dot minus underscore");
}
$this->attributes['filename'] = $value;
}
2020-11-27 23:35:34 +01:00
/**
* @return bool
*/
public function fileExist(): bool
2017-10-04 16:46:56 +02:00
{
2017-10-04 00:11:25 +02:00
return is_file($this->getPath());
}
2020-11-27 23:35:34 +01:00
/**
* @param $content
* @throws BindingResolutionException
*/
public function setContentAttribute($content): void
2017-10-03 17:05:46 +02:00
{
$this->_content = app('image')->make($content)->orientate();
2017-10-11 13:37:06 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @return string|null
*/
public function getHashAttribute(): ?string
2017-10-11 13:37:06 +02:00
{
2017-10-11 17:20:42 +02:00
if (empty($this->content)) {
return null;
}
if (empty($this->content->getEncoded())) {
$this->content->encode();
2017-10-11 13:37:06 +02:00
}
2017-10-11 17:20:42 +02:00
return sha1($this->content->getEncoded());
2017-10-11 13:37:06 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @return string|null
*/
public function getExtensionAttribute(): ?string
2017-10-11 13:37:06 +02:00
{
2017-10-11 17:20:42 +02:00
$mime = $this->content->mime();
if (empty($mime)) {
return null;
}
return str_replace('image/', '', $mime);
}
2020-11-27 23:35:34 +01:00
/**
* @return string|null
*/
public function getMimeAttribute(): ?string
{
2017-10-11 13:37:06 +02:00
if (empty($this->content)) {
2017-10-11 17:20:42 +02:00
return null;
2017-10-11 13:37:06 +02:00
}
2017-10-11 17:20:42 +02:00
return $this->content->mime();
2017-10-11 13:37:06 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @param Image $image
* @return bool
*/
public function same(Image $image): bool
2017-10-11 13:37:06 +02:00
{
$hash1 = $this->hash;
$hash2 = $image->hash;
return !empty($hash1) && !empty($hash2) && $hash1 === $hash2;
2017-10-03 17:05:46 +02:00
}
2020-11-27 23:35:34 +01:00
/**
* @return InterventionImage|null
* @throws BindingResolutionException
*/
public function getContentAttribute(): ?InterventionImage
2017-10-03 17:05:46 +02:00
{
2017-10-03 18:10:59 +02:00
if (empty($this->_content)) {
try {
$this->_content = app('image')->make($this->getPath());
} catch (NotReadableException $e) {
return null;
}
}
2017-10-03 17:05:46 +02:00
return $this->_content;
}
}