MAJ image
This commit is contained in:
@@ -4,6 +4,7 @@ namespace Meoran\Images\Model;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
use Intervention\Image\Exception\NotReadableException;
|
||||
use Intervention\Image\Image as InterventionImage;
|
||||
use Meoran\Images\Exception\InvalidContent;
|
||||
@@ -11,12 +12,15 @@ use Spatie\ImageOptimizer\OptimizerChain;
|
||||
|
||||
/**
|
||||
* Class Image
|
||||
* @property Image $content
|
||||
* @property InterventionImage $content
|
||||
* @package App\Model
|
||||
*/
|
||||
class Image extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* @var InterventionImage $_content
|
||||
*/
|
||||
protected $_content;
|
||||
|
||||
protected $table = 'images';
|
||||
@@ -99,7 +103,8 @@ class Image extends Model
|
||||
public function setFilenameAttribute($value)
|
||||
{
|
||||
$pattern = '/[^a-z_\-\.0-9]/i';
|
||||
if (preg_match($pattern, $value)) {
|
||||
$patternLetter = '/[a-z0-9]+/i';
|
||||
if (preg_match($pattern, $value) || !preg_match($patternLetter, $value)) {
|
||||
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;
|
||||
@@ -121,6 +126,31 @@ class Image extends Model
|
||||
public function setContentAttribute($content)
|
||||
{
|
||||
$this->_content = app('image')->make($content);
|
||||
$this->setHash();
|
||||
}
|
||||
|
||||
public function getHashAttribute()
|
||||
{
|
||||
if (!array_key_exists('hash', $this->attributes)) {
|
||||
$this->setHash();
|
||||
}
|
||||
return $this->attributes['hash'];
|
||||
}
|
||||
|
||||
protected function setHash()
|
||||
{
|
||||
if (empty($this->content)) {
|
||||
$this->attributes['hash'] = null;
|
||||
return;
|
||||
}
|
||||
$this->attributes['hash'] = sha1($this->content->getEncoded());
|
||||
}
|
||||
|
||||
public function same(Image $image)
|
||||
{
|
||||
$hash1 = $this->hash;
|
||||
$hash2 = $image->hash;
|
||||
return !empty($hash1) && !empty($hash2) && $hash1 === $hash2;
|
||||
}
|
||||
|
||||
public function getContentAttribute($value)
|
||||
@@ -199,4 +229,8 @@ class Image extends Model
|
||||
|
||||
return $attributes;
|
||||
}
|
||||
|
||||
public function scopeContent(Builder $builder, Image $image) {
|
||||
$builder->where('hash', $image->hash);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user