2016-09-08 23:22:29 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Album;
|
|
|
|
use App\AlbumSources\IAlbumSource;
|
2016-10-28 05:30:57 +01:00
|
|
|
use App\Helpers\FileHelper;
|
2016-09-08 23:22:29 +01:00
|
|
|
use App\Helpers\ImageHelper;
|
2017-09-17 09:20:35 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
2016-09-08 23:22:29 +01:00
|
|
|
use App\Helpers\ThemeHelper;
|
|
|
|
use App\Photo;
|
2016-10-05 05:02:47 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
2016-09-08 23:22:29 +01:00
|
|
|
|
|
|
|
class PhotoService
|
|
|
|
{
|
2017-09-16 08:26:05 +01:00
|
|
|
const METADATA_VERSION = 2;
|
2016-09-08 23:22:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Album
|
|
|
|
*/
|
|
|
|
private $album;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IAlbumSource
|
|
|
|
*/
|
|
|
|
private $albumSource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ImageHelper
|
|
|
|
*/
|
|
|
|
private $imageHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Photo
|
|
|
|
*/
|
|
|
|
private $photo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ThemeHelper
|
|
|
|
*/
|
|
|
|
private $themeHelper;
|
|
|
|
|
|
|
|
public function __construct(Photo $photo)
|
|
|
|
{
|
|
|
|
$this->photo = $photo;
|
|
|
|
$this->album = $photo->album;
|
|
|
|
$this->albumSource = $this->album->getAlbumSource();
|
|
|
|
|
|
|
|
$this->imageHelper = new ImageHelper();
|
|
|
|
$this->themeHelper = new ThemeHelper();
|
|
|
|
}
|
|
|
|
|
2017-09-17 16:04:07 +01:00
|
|
|
public function analyse($queueToken, $isReanalyse = false)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
2016-10-30 18:36:34 +00:00
|
|
|
$queuePath = FileHelper::getQueuePath($queueToken);
|
|
|
|
$photoFile = join(DIRECTORY_SEPARATOR, [$queuePath, $this->photo->storage_file_name]);
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
try
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
2016-10-30 18:36:34 +00:00
|
|
|
$imageInfo = null;
|
|
|
|
$originalPhotoResource = $this->imageHelper->openImage($photoFile, $imageInfo);
|
|
|
|
if ($originalPhotoResource === false)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
2016-10-30 18:36:34 +00:00
|
|
|
throw new \Exception(sprintf('The image "%s" does not appear to be a valid image, or cannot be read', pathinfo($photoFile, PATHINFO_FILENAME)));
|
|
|
|
}
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
$this->photo->width = $imageInfo[0];
|
|
|
|
$this->photo->height = $imageInfo[1];
|
|
|
|
$this->photo->mime_type = $imageInfo['mime'];
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
// Read the Exif data
|
2017-09-17 09:20:35 +01:00
|
|
|
if (empty($this->photo->raw_exif_data))
|
2017-09-16 12:49:34 +01:00
|
|
|
{
|
2017-09-17 09:20:35 +01:00
|
|
|
$exifData = @exif_read_data($photoFile);
|
|
|
|
$isExifDataFound = ($exifData !== false && is_array($exifData));
|
2017-09-16 12:49:34 +01:00
|
|
|
$this->photo->raw_exif_data = $isExifDataFound ? base64_encode(serialize($exifData)) : '';
|
|
|
|
}
|
2017-09-17 09:20:35 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$exifData = unserialize(base64_decode($this->photo->raw_exif_data));
|
|
|
|
$isExifDataFound = ($exifData !== false && is_array($exifData));
|
|
|
|
}
|
2017-09-16 12:49:34 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
$angleToRotate = 0;
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2017-09-17 09:20:35 +01:00
|
|
|
// If Exif data contains an Orientation, ensure we rotate the original image as such (providing we don't
|
|
|
|
// currently have a metadata version - i.e. it hasn't been read and rotated already before)
|
2017-09-17 16:04:07 +01:00
|
|
|
if ($isExifDataFound && isset($exifData['Orientation']) && !$isReanalyse)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
2016-10-30 18:36:34 +00:00
|
|
|
switch ($exifData['Orientation'])
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
$angleToRotate = 180;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
$angleToRotate = 270;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
$angleToRotate = 90;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
if ($angleToRotate > 0)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
2016-10-30 18:36:34 +00:00
|
|
|
$originalPhotoResource = $this->imageHelper->rotateImage($originalPhotoResource, $angleToRotate);
|
|
|
|
|
|
|
|
if ($angleToRotate == 90 || $angleToRotate == 270)
|
|
|
|
{
|
|
|
|
$this->photo->width = $imageInfo[1];
|
|
|
|
$this->photo->height = $imageInfo[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->imageHelper->saveImage($originalPhotoResource, $photoFile, $imageInfo);
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
2016-10-30 18:36:34 +00:00
|
|
|
}
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
if ($isExifDataFound)
|
|
|
|
{
|
|
|
|
$this->photo->metadata_version = self::METADATA_VERSION;
|
2017-09-16 09:02:25 +01:00
|
|
|
$this->photo->taken_at = $this->metadataDateTime($exifData, $this->photo->taken_at);
|
|
|
|
$this->photo->camera_make = $this->metadataCameraMake($exifData, $this->photo->camera_make);
|
|
|
|
$this->photo->camera_model = $this->metadataCameraModel($exifData, $this->photo->camera_model);
|
|
|
|
$this->photo->camera_software = $this->metadataCameraSoftware($exifData, $this->photo->camera_software);
|
2017-09-17 09:20:35 +01:00
|
|
|
$this->photo->aperture_fnumber = $this->metadataApertureFNumber($exifData, $this->photo->aperture_fnumber);
|
|
|
|
$this->photo->iso_number = $this->metadataIsoNumber($exifData, $this->photo->iso_number);
|
|
|
|
$this->photo->focal_length = $this->metadataFocalLength($exifData, $this->photo->focal_length);
|
|
|
|
$this->photo->shutter_speed = $this->metadataExposureTime($exifData, $this->photo->shutter_speed);
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
$this->photo->is_analysed = true;
|
|
|
|
$this->photo->save();
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
// Save the original
|
|
|
|
$this->albumSource->saveThumbnail($this->photo, $photoFile);
|
2016-09-08 23:22:29 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
$this->regenerateThumbnails($originalPhotoResource);
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
throw $ex;
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
@unlink($photoFile);
|
2016-10-28 06:24:34 +01:00
|
|
|
|
2016-10-30 18:36:34 +00:00
|
|
|
// If the queue directory is now empty, get rid of it
|
|
|
|
FileHelper::deleteIfEmpty($queuePath);
|
|
|
|
}
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 05:02:47 +01:00
|
|
|
public function changeAlbum(Album $newAlbum)
|
|
|
|
{
|
|
|
|
/** @var IAlbumSource $currentSource */
|
|
|
|
$currentSource = $this->photo->album->getAlbumSource();
|
|
|
|
$newSource = $newAlbum->getAlbumSource();
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
// First export the original photo from the storage provider
|
|
|
|
$photoPath = $this->downloadToTemporaryFolder();
|
2016-10-05 05:02:47 +01:00
|
|
|
|
|
|
|
// Save to the new album
|
2016-10-28 12:59:36 +01:00
|
|
|
$newSource->saveThumbnail($this->photo, $photoPath);
|
2016-10-05 05:02:47 +01:00
|
|
|
|
|
|
|
// Delete the original
|
|
|
|
$this->delete();
|
|
|
|
|
|
|
|
// Update the ID and new file name
|
|
|
|
$this->photo->album_id = $newAlbum->id;
|
|
|
|
$this->photo->save();
|
|
|
|
|
|
|
|
// Switch to the new album source
|
|
|
|
$this->albumSource = $newSource;
|
|
|
|
|
|
|
|
// Regenerate the thumbnails in the new album
|
|
|
|
$this->regenerateThumbnails();
|
|
|
|
}
|
|
|
|
|
2016-09-09 11:09:03 +01:00
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
// Remove all thumbnails first - so if any fail, we don't delete the original
|
|
|
|
$themeInfo = $this->themeHelper->info();
|
|
|
|
$thumbnailsRequired = $themeInfo['thumbnails'];
|
|
|
|
|
|
|
|
/** @var mixed $thumbnail */
|
|
|
|
foreach ($thumbnailsRequired as $thumbnail)
|
|
|
|
{
|
|
|
|
$this->albumSource->deleteThumbnail($this->photo, $thumbnail['name']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next remove the original photo
|
|
|
|
$this->albumSource->deleteThumbnail($this->photo);
|
|
|
|
|
|
|
|
// Finally remove the database record
|
|
|
|
$this->photo->delete();
|
|
|
|
}
|
|
|
|
|
2017-09-16 08:26:05 +01:00
|
|
|
public function downloadOriginalToFolder($folderPath)
|
|
|
|
{
|
|
|
|
$photoPath = join(DIRECTORY_SEPARATOR, [$folderPath, $this->photo->storage_file_name]);
|
|
|
|
$photoHandle = fopen($photoPath, 'w');
|
|
|
|
|
|
|
|
$stream = $this->albumSource->fetchPhotoContent($this->photo);
|
|
|
|
$stream->rewind();
|
|
|
|
while (!$stream->feof())
|
|
|
|
{
|
|
|
|
fwrite($photoHandle, $stream->read(4096));
|
|
|
|
}
|
|
|
|
fflush($photoHandle);
|
|
|
|
fclose($photoHandle);
|
|
|
|
$stream->close();
|
|
|
|
|
|
|
|
return $photoPath;
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:57:58 +01:00
|
|
|
public function flip($horizontal, $vertical)
|
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
// First export the original photo from the storage provider
|
|
|
|
$photoPath = $this->downloadToTemporaryFolder();
|
|
|
|
|
2016-10-03 15:57:58 +01:00
|
|
|
$imageInfo = array();
|
|
|
|
$originalPhotoImage = $this->imageHelper->openImage($photoPath, $imageInfo);
|
|
|
|
if ($this->imageHelper->flipImage($originalPhotoImage, boolval($horizontal), boolval($vertical)))
|
|
|
|
{
|
|
|
|
$this->imageHelper->saveImage($originalPhotoImage, $photoPath, $imageInfo);
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
// Update and save the original image back to the storage provider
|
|
|
|
$this->albumSource->saveThumbnail($this->photo, $photoPath);
|
|
|
|
|
|
|
|
// Re-create the thumbnails
|
2016-10-03 15:57:58 +01:00
|
|
|
$this->regenerateThumbnails($originalPhotoImage);
|
|
|
|
|
|
|
|
$this->photo->save();
|
|
|
|
}
|
2016-10-28 12:59:36 +01:00
|
|
|
|
|
|
|
// Remove the temp file
|
|
|
|
@unlink($photoPath);
|
2016-10-03 15:57:58 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
public function regenerateThumbnails($originalPhotoResource = null)
|
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
$photoPath = null;
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
if (is_null($originalPhotoResource))
|
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
// First export the original photo from the storage provider
|
|
|
|
$photoPath = $this->downloadToTemporaryFolder();
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$imageInfo = null;
|
2016-10-28 12:59:36 +01:00
|
|
|
$originalPhotoResource = $this->imageHelper->openImage($photoPath, $imageInfo);
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate and save thumbnails
|
|
|
|
$themeInfo = $this->themeHelper->info();
|
|
|
|
$thumbnailsRequired = $themeInfo['thumbnails'];
|
|
|
|
|
|
|
|
/** @var mixed $thumbnail */
|
|
|
|
foreach ($thumbnailsRequired as $thumbnail)
|
|
|
|
{
|
|
|
|
$generatedThumbnailPath = $this->imageHelper->generateThumbnail($originalPhotoResource, $this->photo, $thumbnail);
|
2016-10-28 06:24:34 +01:00
|
|
|
$this->albumSource->saveThumbnail($this->photo, $generatedThumbnailPath, $thumbnail['name']);
|
2016-10-30 18:36:34 +00:00
|
|
|
@unlink($generatedThumbnailPath);
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
2016-10-28 12:59:36 +01:00
|
|
|
|
|
|
|
if (is_null($originalPhotoResource) && !is_null($photoPath))
|
|
|
|
{
|
|
|
|
// Remove the temp file
|
|
|
|
@unlink($photoPath);
|
|
|
|
}
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function rotate($angle)
|
|
|
|
{
|
|
|
|
$imageInfo = array();
|
2016-10-28 12:59:36 +01:00
|
|
|
|
|
|
|
// First export the photo from the storage provider
|
|
|
|
$photoPath = $this->downloadToTemporaryFolder();
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
$originalPhotoImage = $this->imageHelper->openImage($photoPath, $imageInfo);
|
|
|
|
$originalPhotoImage = $this->imageHelper->rotateImage($originalPhotoImage, intval($angle));
|
|
|
|
$this->imageHelper->saveImage($originalPhotoImage, $photoPath, $imageInfo);
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
// Update and save the original image back to the storage provider
|
|
|
|
$this->albumSource->saveThumbnail($this->photo, $photoPath);
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
if ($angle == 90 || $angle == 270)
|
|
|
|
{
|
|
|
|
$width = $this->photo->width;
|
|
|
|
$this->photo->width = $this->photo->height;
|
|
|
|
$this->photo->height = $width;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->regenerateThumbnails($originalPhotoImage);
|
|
|
|
|
|
|
|
$this->photo->save();
|
2016-10-28 12:59:36 +01:00
|
|
|
|
|
|
|
// Remove the temp file
|
|
|
|
@unlink($photoPath);
|
|
|
|
}
|
|
|
|
|
2017-09-17 09:20:35 +01:00
|
|
|
private function calculateValueFromFraction($input)
|
|
|
|
{
|
|
|
|
$split = explode('/', $input);
|
|
|
|
if (count($split) != 2)
|
|
|
|
{
|
|
|
|
return $split;
|
|
|
|
}
|
|
|
|
|
|
|
|
$numerator = intval($split[0]);
|
|
|
|
$denominator = intval($split[1]);
|
|
|
|
|
|
|
|
return $denominator == 0
|
|
|
|
? 0
|
|
|
|
: ($numerator / $denominator);
|
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
private function downloadToTemporaryFolder()
|
|
|
|
{
|
|
|
|
$photoPath = tempnam(sys_get_temp_dir(), 'BlueTwilight_');
|
|
|
|
$photoHandle = fopen($photoPath, 'w');
|
|
|
|
|
|
|
|
$stream = $this->albumSource->fetchPhotoContent($this->photo);
|
|
|
|
$stream->rewind();
|
|
|
|
while (!$stream->feof())
|
|
|
|
{
|
|
|
|
fwrite($photoHandle, $stream->read(4096));
|
|
|
|
}
|
|
|
|
fflush($photoHandle);
|
|
|
|
fclose($photoHandle);
|
|
|
|
$stream->close();
|
|
|
|
|
|
|
|
return $photoPath;
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
2017-09-17 09:20:35 +01:00
|
|
|
private function metadataApertureFNumber(array $exifData, $originalValue = null)
|
|
|
|
{
|
|
|
|
if (isset($exifData['FNumber']))
|
|
|
|
{
|
|
|
|
$value = $this->calculateValueFromFraction($exifData['FNumber']);
|
|
|
|
|
|
|
|
if (intval($value) === $value)
|
|
|
|
{
|
|
|
|
return sprintf('f/%d', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('f/%0.1f', $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $originalValue;
|
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
private function metadataCameraMake(array $exifData, $originalValue = null)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
if (isset($exifData['Make']))
|
|
|
|
{
|
|
|
|
return $exifData['Make'];
|
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
return $originalValue;
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
private function metadataCameraModel(array $exifData, $originalValue = null)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
if (isset($exifData['Model']))
|
|
|
|
{
|
|
|
|
return $exifData['Model'];
|
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
return $originalValue;
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
private function metadataCameraSoftware(array $exifData, $originalValue = null)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
if (isset($exifData['Software']))
|
|
|
|
{
|
|
|
|
return $exifData['Software'];
|
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
return $originalValue;
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
private function metadataDateTime(array $exifData, $originalValue = null)
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
$dateTime = null;
|
2016-10-28 15:28:25 +01:00
|
|
|
if (isset($exifData['DateTimeOriginal']))
|
|
|
|
{
|
|
|
|
$dateTime = $exifData['DateTimeOriginal'];
|
|
|
|
}
|
|
|
|
else if (isset($exifData['DateTime']))
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
|
|
|
$dateTime = $exifData['DateTime'];
|
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
if (is_null($dateTime))
|
2016-09-08 23:22:29 +01:00
|
|
|
{
|
2017-09-16 09:02:25 +01:00
|
|
|
return $originalValue;
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 09:02:25 +01:00
|
|
|
return preg_replace('/^([\d]{4}):([\d]{2}):([\d]{2})/', '$1-$2-$3', $dateTime);
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|
2017-09-17 09:20:35 +01:00
|
|
|
|
|
|
|
private function metadataExposureTime(array $exifData, $originalValue = null)
|
|
|
|
{
|
|
|
|
if (isset($exifData['ExposureTime']))
|
|
|
|
{
|
|
|
|
$decimal = $this->calculateValueFromFraction($exifData['ExposureTime']);
|
|
|
|
$fraction = MiscHelper::decimalToFraction($decimal);
|
|
|
|
return sprintf('%d/%d', $fraction[0], $fraction[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $originalValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataFocalLength(array $exifData, $originalValue = null)
|
|
|
|
{
|
|
|
|
if (isset($exifData['FocalLength']))
|
|
|
|
{
|
|
|
|
return $this->calculateValueFromFraction($exifData['FocalLength']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $originalValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataIsoNumber(array $exifData, $originalValue = null)
|
|
|
|
{
|
|
|
|
if (isset($exifData['ISOSpeedRatings']))
|
|
|
|
{
|
|
|
|
return $exifData['ISOSpeedRatings'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $originalValue;
|
|
|
|
}
|
2016-09-08 23:22:29 +01:00
|
|
|
}
|