Major refactor of photo processing, video processing still to do
This commit is contained in:
@@ -2,54 +2,43 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Traits\ForwardsCalls;
|
||||
use Imagick;
|
||||
|
||||
class Photo extends Abstracted\Catalog
|
||||
{
|
||||
protected $table = 'photo';
|
||||
use ForwardsCalls;
|
||||
|
||||
public const config = 'photo';
|
||||
|
||||
protected static $includeSubSecTime = TRUE;
|
||||
|
||||
// Imagick Object
|
||||
private $_o;
|
||||
// Imagick Objectfile_name
|
||||
private ?Imagick $_o;
|
||||
protected array $init = [
|
||||
'creation_date',
|
||||
'gps',
|
||||
'heightwidth',
|
||||
'signature',
|
||||
'software',
|
||||
'subsectime',
|
||||
];
|
||||
|
||||
// How should the image be rotated, based on the value of orientation
|
||||
private $_rotate = [
|
||||
private array $_rotate = [
|
||||
3=>180,
|
||||
6=>90,
|
||||
8=>-90,
|
||||
];
|
||||
|
||||
public function getIDLinkAttribute()
|
||||
{
|
||||
return $this->HTMLLinkAttribute($this->id,'p/info');
|
||||
}
|
||||
|
||||
public function getHtmlImageURL(): string
|
||||
{
|
||||
return sprintf('<img class="p-3" src="%s">',url('p/thumbnail',$this->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the image, rotated, minus exif data
|
||||
*/
|
||||
public function image()
|
||||
{
|
||||
if (is_null($imo = $this->o()))
|
||||
return NULL;
|
||||
|
||||
if (array_key_exists('exif',$imo->getImageProfiles()))
|
||||
$imo->removeImageProfile('exif');
|
||||
|
||||
return $this->rotate($imo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the GPS coordinates
|
||||
*/
|
||||
public static function latlon(array $coordinate,$hemisphere)
|
||||
public static function latlon(array $coordinate,string $hemisphere): ?float
|
||||
{
|
||||
if (! $coordinate OR ! $hemisphere)
|
||||
if ((! $coordinate) || (! $hemisphere))
|
||||
return NULL;
|
||||
|
||||
for ($i=0; $i<3; $i++) {
|
||||
@@ -67,29 +56,172 @@ class Photo extends Abstracted\Catalog
|
||||
|
||||
list($degrees,$minutes,$seconds) = $coordinate;
|
||||
|
||||
$sign = ($hemisphere == 'W' || $hemisphere == 'S') ? -1 : 1;
|
||||
$sign = ($hemisphere === 'W' || $hemisphere === 'S') ? -1 : 1;
|
||||
|
||||
return round($sign*($degrees+$minutes/60+$seconds/3600),($degrees>100 ? 3 : 4));
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an Imagick object or attribute
|
||||
* Forward calls to Imagick
|
||||
*
|
||||
* @param $method
|
||||
* @param $parameters
|
||||
* @return mixed|null
|
||||
*/
|
||||
protected function o($attr=NULL)
|
||||
public function __call($method,$parameters) {
|
||||
if (str_starts_with($method,'Imagick_')) {
|
||||
$method = preg_replace('/^Imagick_/','',$method);
|
||||
|
||||
return $this->o ? $this->forwardCallTo($this->_o,$method,$parameters) : NULL;
|
||||
|
||||
} else
|
||||
return parent::__call($method,$parameters);
|
||||
}
|
||||
|
||||
public function __get($key): mixed
|
||||
{
|
||||
if (! file_exists($this->file_path()) OR ! is_readable($this->file_path()))
|
||||
return NULL;
|
||||
if ($key === 'o') {
|
||||
if (isset($this->_o))
|
||||
return $this->_o;
|
||||
|
||||
if (is_null($this->_o))
|
||||
$this->_o = new \Imagick($this->file_path());
|
||||
if ((!file_exists($this->file_name(FALSE))) || (!is_readable($this->file_name(FALSE))))
|
||||
return $this->_o = NULL;
|
||||
|
||||
return is_null($attr) ? $this->_o : $this->_o->getImageProperty($attr);
|
||||
if (!isset($this->_o))
|
||||
return $this->_o = new Imagick($this->file_name(FALSE));
|
||||
}
|
||||
|
||||
return parent::__get($key);
|
||||
}
|
||||
|
||||
/* ATTRIBUTES */
|
||||
|
||||
public function getFileSignatureAttribute(string $val=NULL): string
|
||||
{
|
||||
return $val ?: $this->getObjectOriginal('file_signature');
|
||||
}
|
||||
|
||||
public function getHeightAttribute(string $val=NULL): ?int
|
||||
{
|
||||
return $val ?: $this->getObjectOriginal('height');
|
||||
}
|
||||
|
||||
public function getOrientationAttribute(int $val=NULL): ?int
|
||||
{
|
||||
return $val ?: $this->getObjectOriginal('orientation');
|
||||
}
|
||||
|
||||
public function getSignatureAttribute(string $val=NULL): ?string
|
||||
{
|
||||
return $val ?: $this->getObjectOriginal('signature');
|
||||
}
|
||||
|
||||
public function getWidthAttribute(string $val=NULL): ?int
|
||||
{
|
||||
return $val ?: $this->getObjectOriginal('width');
|
||||
}
|
||||
|
||||
/* METHODS */
|
||||
|
||||
public function custom_init(): void
|
||||
{
|
||||
$this->orientation = $this->getObjectOriginal('orientation');
|
||||
|
||||
try {
|
||||
if ($this->isReadable() && $this->o->thumbnailimage(150,150,true)) {
|
||||
$this->o->setImageFormat('jpg');
|
||||
|
||||
$this->thumbnail = $this->o->getImageBlob();
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::info(sprintf('Unable to create thumbnail for %s (%s)',$this->id,$e->getMessage()));
|
||||
}
|
||||
|
||||
if ($this->thumbnail === FALSE)
|
||||
$this->thumbnail = NULL;
|
||||
}
|
||||
|
||||
public function getObjectOriginal(string $property): mixed
|
||||
{
|
||||
switch ($property) {
|
||||
case 'creation_date':
|
||||
if ($this->Imagick_getImageProperty('exif:DateTimeOriginal') === '0000:00:00 00:00:00'
|
||||
&& $this->Imagick_getImageProperty('exif:DateTime') === '0000:00:00 00:00:00')
|
||||
return NULL;
|
||||
|
||||
$result = Carbon::create($x=
|
||||
($this->Imagick_getImageProperty('exif:DateTimeOriginal') && ($this->Imagick_getImageProperty('exif:DateTimeOriginal') !== '0000:00:00 00:00:00'))
|
||||
? $this->Imagick_getImageProperty('exif:DateTimeOriginal').$this->Imagick_getImageProperty('exif:OffsetTimeOriginal')
|
||||
: $this->Imagick_getImageProperty('exif:DateTime').$this->Imagick_getImageProperty('exif:OffsetTime'));
|
||||
|
||||
return $result ?: NULL;
|
||||
|
||||
case 'file_signature':
|
||||
return md5_file($this->file_name(FALSE));
|
||||
|
||||
case 'gps_lat':
|
||||
return self::latlon(preg_split('/,\s?/',$this->Imagick_getImageProperty('exif:GPSLatitude')),$this->Imagick_getImageProperty('exif:GPSLatitudeRef'));
|
||||
|
||||
case 'gps_lon':
|
||||
return self::latlon(preg_split('/,\s?/',$this->Imagick_getImageProperty('exif:GPSLongitude')),$this->Imagick_getImageProperty('exif:GPSLongitudeRef'));
|
||||
|
||||
case 'height':
|
||||
return $this->Imagick_getImageHeight();
|
||||
|
||||
case 'identifier':
|
||||
return NULL;
|
||||
|
||||
case 'make':
|
||||
return $this->Imagick_getImageProperty('exif:Make');
|
||||
|
||||
case 'model':
|
||||
return $this->Imagick_getImageProperty('exif:Model');
|
||||
|
||||
case 'orientation':
|
||||
return $this->Imagick_getImageOrientation();
|
||||
|
||||
case 'signature':
|
||||
return $this->Imagick_getImageSignature();
|
||||
|
||||
case 'software':
|
||||
return $this->Imagick_getImageProperty('exif:Software');
|
||||
|
||||
case 'subsectime':
|
||||
$this->subsectime = (int)$this->Imagick_getImageProperty('exif:SubSecTimeOriginal');
|
||||
|
||||
// In case of an error.
|
||||
if ($this->subsectime > 32767)
|
||||
$this->subsectime = 32767;
|
||||
|
||||
if ($this->subsectime === FALSE)
|
||||
$this->subsectime = 0;
|
||||
|
||||
return $this->subsectime;
|
||||
|
||||
case 'width':
|
||||
return $this->Imagick_getImageWidth();
|
||||
|
||||
default:
|
||||
throw new \Exception('To implement: '.$property);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the image, rotated
|
||||
*/
|
||||
public function image(): ?string
|
||||
{
|
||||
$imo = clone($this->o);
|
||||
|
||||
return $imo ? $this->rotate($imo) : NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the orientation of a photo
|
||||
*/
|
||||
public function orientation() {
|
||||
public function orientation(): string
|
||||
{
|
||||
switch ($this->orientation) {
|
||||
case 1: return 'None!';
|
||||
case 3: return 'Upside Down';
|
||||
@@ -102,118 +234,36 @@ class Photo extends Abstracted\Catalog
|
||||
/**
|
||||
* Rotate the image
|
||||
*/
|
||||
private function rotate(\Imagick $imo)
|
||||
private function rotate(\Imagick $imo,string $format='jpg'): string
|
||||
{
|
||||
if (array_key_exists($this->orientation,$this->_rotate))
|
||||
$imo->rotateImage(new \ImagickPixel('none'),$this->_rotate[$this->orientation]);
|
||||
|
||||
$imo->setImageFormat('jpg');
|
||||
$imo->setImageFormat($format);
|
||||
|
||||
if (array_key_exists('exif',$imo->getImageProfiles()))
|
||||
$imo->removeImageProfile('exif');
|
||||
|
||||
return $imo->getImageBlob();
|
||||
}
|
||||
|
||||
public function property(string $property)
|
||||
{
|
||||
if (! $this->o())
|
||||
return NULL;
|
||||
|
||||
switch ($property) {
|
||||
case 'creationdate':
|
||||
if ($this->property('exif:DateTimeOriginal') == '0000:00:00 00:00:00'
|
||||
&& $this->property('exif:DateTimeOriginal') == '0000:00:00 00:00:00')
|
||||
return NULL;
|
||||
|
||||
return strtotime(
|
||||
$this->property('exif:DateTimeOriginal') && $this->property('exif:DateTimeOriginal') != '0000:00:00 00:00:00'
|
||||
? $this->property('exif:DateTimeOriginal')
|
||||
: $this->property('exif:DateTime'));
|
||||
break;
|
||||
|
||||
case 'height': return $this->_o->getImageHeight();
|
||||
case 'orientation': return $this->_o->getImageOrientation();
|
||||
case 'signature': return $this->_o->getImageSignature();
|
||||
case 'width': return $this->_o->getImageWidth();
|
||||
|
||||
default:
|
||||
return $this->_o->getImageProperty($property);
|
||||
}
|
||||
}
|
||||
|
||||
public function properties()
|
||||
{
|
||||
return $this->o() ? $this->_o->getImageProperties() : [];
|
||||
}
|
||||
|
||||
|
||||
public function setLocation()
|
||||
{
|
||||
$this->gps_lat = static::latlon(preg_split('/,\s?/',$this->property('exif:GPSLatitude')),$this->property('exif:GPSLatitudeRef'));
|
||||
$this->gps_lon = static::latlon(preg_split('/,\s?/',$this->property('exif:GPSLongitude')),$this->property('exif:GPSLongitudeRef'));
|
||||
}
|
||||
|
||||
public function setMakeModel()
|
||||
{
|
||||
$ma = NULL;
|
||||
|
||||
if ($this->property('exif:Make'))
|
||||
$ma = Make::firstOrCreate([
|
||||
'name'=>$this->property('exif:Make'),
|
||||
]);
|
||||
|
||||
$mo = Model::firstOrCreate([
|
||||
'name'=>$this->property('exif:Model') ?: NULL,
|
||||
'make_id'=>$ma ? $ma->id : NULL,
|
||||
]);
|
||||
|
||||
$so = Software::firstOrCreate([
|
||||
'name'=>$this->property('exif:Software') ?: NULL,
|
||||
'model_id'=>$mo->id,
|
||||
]);
|
||||
|
||||
$this->software_id = $so->id;
|
||||
}
|
||||
|
||||
public function setSubSecTime()
|
||||
{
|
||||
$this->subsectime = (int)$this->property('exif:SubSecTimeOriginal');
|
||||
|
||||
// In case of an error.
|
||||
if ($this->subsectime > 32767)
|
||||
$this->subsectime = 32767;
|
||||
|
||||
if ($this->subsectime === FALSE)
|
||||
$this->subsectime = 0;
|
||||
}
|
||||
|
||||
public function setThumbnail()
|
||||
{
|
||||
try {
|
||||
$this->thumbnail = exif_thumbnail($this->file_path());
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// @todo Couldnt get the thumbnail, so we should create one.
|
||||
Log::info(sprintf('Unable to create thumbnail for %s (%s)',$this->id,$e->getMessage()));
|
||||
}
|
||||
|
||||
if ($this->thumbnail === FALSE)
|
||||
$this->thumbnail = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the image's thumbnail
|
||||
*/
|
||||
public function thumbnail($rotate=TRUE)
|
||||
public function thumbnail($rotate=TRUE): ?string
|
||||
{
|
||||
if (! $this->thumbnail) {
|
||||
if ($this->isReadable() AND $this->o()->thumbnailimage(200,200,true,false)) {
|
||||
$this->_o->setImageFormat('jpg');
|
||||
return $this->_o->getImageBlob();
|
||||
if ($this->isReadable() && $this->o->thumbnailimage(150,150,true)) {
|
||||
$this->o->setImageFormat('jpg');
|
||||
|
||||
return $this->o->getImageBlob();
|
||||
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (! $rotate OR ! array_key_exists($this->orientation,$this->_rotate) OR ! extension_loaded('imagick'))
|
||||
if ((! $rotate) || (! array_key_exists($this->orientation,$this->_rotate)) || (! extension_loaded('imagick')))
|
||||
return $this->thumbnail;
|
||||
|
||||
$imo = new \Imagick();
|
||||
@@ -225,10 +275,9 @@ class Photo extends Abstracted\Catalog
|
||||
|
||||
/**
|
||||
* Return the extension of the image
|
||||
* @todo mime-by-ext?
|
||||
*/
|
||||
public function type($mime=FALSE)
|
||||
public function type(bool $mime=FALSE): string
|
||||
{
|
||||
return strtolower($mime ? 'image/jpeg' : pathinfo($this->filename,PATHINFO_EXTENSION));
|
||||
return strtolower($mime ? mime_content_type($this->file_name(FALSE)) : pathinfo($this->filename,PATHINFO_EXTENSION));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user