photo/app/Models/Photo.php

239 lines
5.9 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 DB;
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';
// 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,
];
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
*/
2016-06-29 23:32:57 +00:00
public function date_taken()
{
2018-01-11 12:59:53 +00:00
return $this->date_created ? (date('Y-m-d H:i:s',$this->date_created).($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
*/
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())
2018-01-11 12:59:53 +00:00
: sprintf('%s_%03s',date('Y/m/d-His',$this->date_created),$this->subsectime).
($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;
}
2018-01-11 12:59:53 +00:00
public function getIDLinkAttribute()
2016-06-29 23:32:57 +00:00
{
2018-01-11 12:59:53 +00:00
return $this->HTMLLinkAttribute($this->id,url('p/info').'/');
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');
$this->rotate($imo);
return $imo->getImageBlob();
}
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;
2016-06-29 23:32:57 +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;
}
list($degrees, $minutes, $seconds) = $coordinate;
$sign = ($hemisphere == 'W' || $hemisphere == 'S') ? -1 : 1;
return round($sign*($degrees+$minutes/60+$seconds/3600),$degrees > 100 ? 3 : 4);
}
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';
default:
return 'unknown?';
}
}
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();
}
2016-06-30 06:01:12 +00:00
public function property($property)
{
2018-01-11 12:59:53 +00:00
if (! $this->o())
2016-06-30 06:01:12 +00:00
return NULL;
switch ($property)
{
2018-01-11 12:59:53 +00:00
case 'creationdate': return strtotime($this->property('exif:DateTimeOriginal') ? $this->property('exif:DateTimeOriginal') : $this->property('exif:DateTime')); break;
case 'height': return $this->_o->getImageHeight(); break;
case 'orientation': return $this->_o->getImageOrientation(); break;
case 'signature': return $this->_o->getImageSignature(); break;
case 'width': return $this->_o->getImageWidth(); break;
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()
{
2018-01-11 12:59:53 +00:00
if ($this->property('creationdate'))
$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'));
#$this->gps_altitude = $this->property('gps_altitude');
}
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());
#$this->identifier = $this->property('identifier');
}
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());
} 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)
{
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
}
2018-01-11 12:59:53 +00:00
public function view()
2016-06-29 23:32:57 +00:00
{
2018-01-11 12:59:53 +00:00
return sprintf('<img height="240" src="%s"></img>',url('/p/thumbnail/'.$this->id));
2016-06-22 05:49:20 +00:00
}
2019-11-08 12:51:47 +00:00
}