photo/app/Models/Photo.php

251 lines
6.1 KiB
PHP
Raw Normal View History

2016-06-22 05:49:20 +00:00
<?php
2019-11-08 12:51:47 +00:00
namespace App\Models;
2016-06-22 05:49:20 +00:00
use Illuminate\Support\Facades\Log;
2016-06-22 05:49:20 +00:00
2018-01-11 12:59:53 +00:00
class Photo extends Abstracted\Catalog
2016-06-22 05:49:20 +00:00
{
protected $table = 'photo';
2019-12-14 11:29:54 +00:00
protected static $includeSubSecTime = TRUE;
2016-06-22 05:49:20 +00:00
// Imagick Object
2018-01-11 12:59:53 +00:00
private $_o;
2016-06-22 05:49:20 +00:00
// How should the image be rotated, based on the value of orientation
private $_rotate = [
3=>180,
6=>90,
8=>-90,
];
2019-11-27 10:30:43 +00:00
public function software()
{
return $this->belongsTo(Software::class);
}
2019-12-14 11:29:54 +00:00
public function getIDLinkAttribute()
{
return $this->HTMLLinkAttribute($this->id,url('p/info').'/');
}
2016-06-29 04:04:02 +00:00
/**
2018-01-11 12:59:53 +00:00
* Date the photo was taken
2016-06-29 04:04:02 +00:00
*/
2019-11-23 01:51:30 +00:00
public function date_taken(): string
2016-06-29 23:32:57 +00:00
{
2019-12-14 11:29:54 +00:00
return $this->date_created
? ($this->date_created->format('Y-m-d H:i:s').($this->subsectime ? '.'.$this->subsectime : ''))
: 'UNKNOWN';
2016-06-29 23:32:57 +00:00
}
2016-06-22 05:49:20 +00:00
/**
* Determine the new name for the image
2019-11-23 01:51:30 +00:00
* @todo Implement in Parent
2016-06-22 05:49:20 +00:00
*/
2016-06-29 23:32:57 +00:00
public function file_path($short=FALSE,$new=FALSE)
{
2016-06-22 05:49:20 +00:00
$file = $this->filename;
if ($new)
2018-01-11 12:59:53 +00:00
$file = sprintf('%s.%s',((is_null($this->date_created) OR ! $this->date_created)
2016-06-22 05:49:20 +00:00
? sprintf('UNKNOWN/%07s',$this->file_path_id())
2019-11-27 10:30:43 +00:00
: sprintf('%s_%03s',$this->date_created->format('Y/m/d-His'),$this->subsectime).
2018-01-11 12:59:53 +00:00
($this->subsectime ? '' : sprintf('-%05s',$this->id))),$this->type());
2016-06-22 05:49:20 +00:00
return (($short OR preg_match('/^\//',$file)) ? '' : config('photo.dir').DIRECTORY_SEPARATOR).$file;
}
2019-12-14 11:29:54 +00:00
public function getHtmlImageURL(): string
2016-06-29 23:32:57 +00:00
{
2019-12-15 12:34:42 +00:00
return sprintf('<img class="p-3" src="%s">',url('p/thumbnail',$this->id));
2016-06-22 06:51:31 +00:00
}
/**
* Return the image, rotated, minus exif data
*/
2016-06-29 23:32:57 +00:00
public function image()
{
2018-01-11 12:59:53 +00:00
if (is_null($imo = $this->o()))
2016-07-04 06:00:33 +00:00
return NULL;
if (array_key_exists('exif',$imo->getImageProfiles()))
$imo->removeImageProfile('exif');
2019-12-14 11:29:54 +00:00
return $this->rotate($imo);
}
2016-06-22 06:51:31 +00:00
/**
* Calculate the GPS coordinates
*/
2016-06-29 23:32:57 +00:00
public static function latlon(array $coordinate,$hemisphere)
{
2016-06-22 05:49:20 +00:00
if (! $coordinate OR ! $hemisphere)
return NULL;
2019-12-14 11:29:54 +00:00
for ($i=0; $i<3; $i++) {
2016-06-22 05:49:20 +00:00
$part = explode('/', $coordinate[$i]);
if (count($part) == 1)
$coordinate[$i] = $part[0];
elseif (count($part) == 2)
$coordinate[$i] = floatval($part[0])/floatval($part[1]);
else
$coordinate[$i] = 0;
}
2019-12-14 11:29:54 +00:00
list($degrees,$minutes,$seconds) = $coordinate;
2016-06-22 05:49:20 +00:00
$sign = ($hemisphere == 'W' || $hemisphere == 'S') ? -1 : 1;
2019-12-14 11:29:54 +00:00
return round($sign*($degrees+$minutes/60+$seconds/3600),($degrees>100 ? 3 : 4));
2016-06-22 05:49:20 +00:00
}
2016-06-29 04:04:02 +00:00
/**
2018-01-11 12:59:53 +00:00
* Return an Imagick object or attribute
2016-06-29 04:04:02 +00:00
*/
2018-01-11 12:59:53 +00:00
protected function o($attr=NULL)
2016-06-29 23:32:57 +00:00
{
2018-01-11 12:59:53 +00:00
if (! file_exists($this->file_path()) OR ! is_readable($this->file_path()))
return NULL;
2016-06-29 04:04:02 +00:00
2018-01-11 12:59:53 +00:00
if (is_null($this->_o))
$this->_o = new \Imagick($this->file_path());
2016-06-29 04:04:02 +00:00
2018-01-11 12:59:53 +00:00
return is_null($attr) ? $this->_o : $this->_o->getImageProperty($attr);
}
2016-06-30 06:01:12 +00:00
/**
* Display the orientation of a photo
*/
public function orientation() {
switch ($this->orientation) {
case 1: return 'None!';
case 3: return 'Upside Down';
case 6: return 'Rotate Right';
case 8: return 'Rotate Left';
2019-12-14 11:29:54 +00:00
default: return 'unknown?';
2016-06-30 06:01:12 +00:00
}
}
2016-06-22 05:49:20 +00:00
/**
* Rotate the image
*/
private function rotate(\Imagick $imo)
{
if (array_key_exists($this->orientation,$this->_rotate))
$imo->rotateImage(new \ImagickPixel('none'),$this->_rotate[$this->orientation]);
return $imo->getImageBlob();
}
2019-12-15 12:34:42 +00:00
public function property(string $property)
2016-06-30 06:01:12 +00:00
{
2018-01-11 12:59:53 +00:00
if (! $this->o())
2016-06-30 06:01:12 +00:00
return NULL;
2019-12-14 11:29:54 +00:00
switch ($property) {
2019-11-15 12:39:20 +00:00
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();
2016-06-30 06:01:12 +00:00
default:
2018-01-11 12:59:53 +00:00
return $this->_o->getImageProperty($property);
2016-06-30 06:01:12 +00:00
}
}
public function properties()
{
2018-01-11 12:59:53 +00:00
return $this->o() ? $this->_o->getImageProperties() : [];
}
2018-01-11 12:59:53 +00:00
public function setDateCreated()
{
2019-11-15 12:39:20 +00:00
$this->date_created = $this->property('creationdate');
}
2018-01-11 12:59:53 +00:00
public function setLocation()
{
2018-01-11 12:59:53 +00:00
$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'));
}
2019-12-14 11:29:54 +00:00
// @todo Now set software_id
2018-01-11 12:59:53 +00:00
public function setMakeModel()
{
$this->make = $this->property('exif:Make') ? $this->property('exif:Make') : NULL;
$this->model = $this->property('exif:Model') ? $this->property('exif:Model') : NULL;
$this->software = $this->property('exif:Software') ? $this->property('exif:Software') : NULL;
}
public function setSignature()
{
$this->signature = $this->property('signature');
$this->file_signature = md5_file($this->file_path());
}
public function setSubSecTime()
{
$this->subsectime = $this->property('exif:SubSecTimeOriginal');
// In case of an error.
if ($this->subsectime > 32767)
$this->subsectime = 32767;
2018-01-11 12:59:53 +00:00
}
public function setThumbnail()
2016-06-22 06:51:31 +00:00
{
2018-01-11 12:59:53 +00:00
try {
$this->thumbnail = exif_thumbnail($this->file_path());
2019-12-14 11:29:54 +00:00
2018-01-11 12:59:53 +00:00
} 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()));
2018-01-11 12:59:53 +00:00
}
if ($this->thumbnail === FALSE)
$this->thumbnail = NULL;
2016-06-22 06:51:31 +00:00
}
2016-06-22 05:49:20 +00:00
/**
* Return the image's thumbnail
*/
public function thumbnail($rotate=TRUE)
{
2019-12-14 11:29:54 +00:00
if (! $this->thumbnail) {
2018-01-11 12:59:53 +00:00
return $this->o()->thumbnailimage(200,200,true,true) ? $this->_o->getImageBlob() : NULL;
}
2016-06-22 05:49:20 +00:00
if (! $rotate OR ! array_key_exists($this->orientation,$this->_rotate) OR ! extension_loaded('imagick'))
return $this->thumbnail;
$imo = new \Imagick();
$imo->readImageBlob($this->thumbnail);
return $this->rotate($imo);
}
2016-06-22 06:51:31 +00:00
/**
* Return the extension of the image
2018-01-11 12:59:53 +00:00
* @todo mime-by-ext?
2016-06-22 06:51:31 +00:00
*/
2016-06-29 23:32:57 +00:00
public function type($mime=FALSE)
{
2016-07-04 06:00:33 +00:00
return strtolower($mime ? 'image/jpeg' : pathinfo($this->filename,PATHINFO_EXTENSION));
2016-06-22 06:51:31 +00:00
}
2019-11-08 12:51:47 +00:00
}