2017-10-03 17:05:46 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Meoran\Images\Model;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-10-03 18:10:59 +02:00
|
|
|
use Intervention\Image\Exception\NotReadableException;
|
|
|
|
use Meoran\Images\Exception\InvalidContent;
|
2017-10-03 17:05:46 +02:00
|
|
|
use Spatie\ImageOptimizer\OptimizerChain;
|
2017-10-03 18:10:59 +02:00
|
|
|
use \Intervention\Image\Image as InterventionImage;
|
2017-10-03 17:05:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Image
|
|
|
|
* @property Image $content
|
|
|
|
* @package App\Model
|
|
|
|
*/
|
|
|
|
class Image extends Model
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $_content;
|
|
|
|
|
2017-10-03 18:10:59 +02:00
|
|
|
protected $table = 'images';
|
|
|
|
|
2017-10-03 17:05:46 +02:00
|
|
|
public $fillable = [
|
|
|
|
'content',
|
|
|
|
'position',
|
|
|
|
'created',
|
|
|
|
'updated'
|
|
|
|
];
|
|
|
|
protected $dates = ['created_at', 'updated_at'];
|
|
|
|
protected $appends = ['url'];
|
|
|
|
protected $hidden = [];
|
|
|
|
|
|
|
|
public function getUrlAttribute()
|
|
|
|
{
|
2017-10-03 18:10:59 +02:00
|
|
|
return app('url')->to('/' . config('image.route') . '/original/' . $this->filename);
|
2017-10-03 17:05:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected static function boot()
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPath()
|
|
|
|
{
|
|
|
|
if (empty($this->filename)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::getAbsolutePath($this->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getAbsolutePath($filename)
|
|
|
|
{
|
|
|
|
$basePath = config('image.path');
|
|
|
|
|
|
|
|
$parts = array_slice(str_split($filename, 2), 0, 2);
|
|
|
|
|
2017-10-03 18:10:59 +02:00
|
|
|
$path = $basePath . '/' . implode('/', $parts) . '/' . $filename;
|
2017-10-03 17:05:46 +02:00
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static function generateRandomFilename()
|
|
|
|
{
|
|
|
|
return mb_strtolower(str_random(60));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function generateFilename($force = false)
|
|
|
|
{
|
|
|
|
if ($this->filename && !$force) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->filename = self::generateRandomFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setContentAttribute($content)
|
|
|
|
{
|
|
|
|
$this->_content = app('image')->make($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getContentAttribute($value)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toArray()
|
|
|
|
{
|
|
|
|
$attributes = parent::toArray();
|
|
|
|
|
|
|
|
if (isset($attributes['pivot']) && array_key_exists('position', $attributes['pivot'])) {
|
|
|
|
$attributes['position'] = $attributes['pivot']['position'];
|
|
|
|
}
|
|
|
|
unset($attributes['pivot']);
|
|
|
|
return $attributes;
|
|
|
|
}
|
|
|
|
}
|