2016-09-02 21:27:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Album;
|
2016-09-03 17:09:49 +01:00
|
|
|
use App\Helpers\ImageHelper;
|
|
|
|
use App\Helpers\ThemeHelper;
|
2016-09-02 21:27:50 +01:00
|
|
|
use App\Photo;
|
|
|
|
use App\Upload;
|
|
|
|
use App\UploadPhoto;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
|
|
|
class ProcessUploadCommand extends Command
|
|
|
|
{
|
2016-09-02 22:18:40 +01:00
|
|
|
const METADATA_VERSION = 1;
|
|
|
|
|
2016-09-03 17:09:49 +01:00
|
|
|
/**
|
|
|
|
* @var ImageHelper
|
|
|
|
*/
|
|
|
|
private $imageHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ThemeHelper
|
|
|
|
*/
|
|
|
|
private $themeHelper;
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $signature = 'twilight:process-uploads';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Processes uploads made through the web application.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new command instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-03 17:09:49 +01:00
|
|
|
public function __construct(ImageHelper $imageHelper, ThemeHelper $themeHelper)
|
2016-09-02 21:27:50 +01:00
|
|
|
{
|
|
|
|
parent::__construct();
|
2016-09-03 17:09:49 +01:00
|
|
|
|
|
|
|
$this->imageHelper = $imageHelper;
|
|
|
|
$this->themeHelper = $themeHelper;
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
|
|
|
$uploadsToProcess = Upload::where([
|
|
|
|
['is_completed', false],
|
2016-09-05 12:01:30 +01:00
|
|
|
['is_processing', false],
|
|
|
|
['is_ready', true]
|
2016-09-02 21:27:50 +01:00
|
|
|
])
|
2016-09-03 17:09:49 +01:00
|
|
|
->orderBy('created_at')
|
2016-09-02 21:27:50 +01:00
|
|
|
->get();
|
|
|
|
|
2016-09-05 12:01:30 +01:00
|
|
|
/** @var Upload $upload */
|
2016-09-02 21:27:50 +01:00
|
|
|
foreach ($uploadsToProcess as $upload)
|
|
|
|
{
|
2016-09-05 12:01:30 +01:00
|
|
|
$upload->is_processing = 1;
|
|
|
|
$upload->save();
|
|
|
|
|
2016-09-02 21:27:50 +01:00
|
|
|
$this->output->writeln(sprintf('Processing upload #%d', $upload->id));
|
|
|
|
$this->handleUpload($upload);
|
2016-09-05 12:01:30 +01:00
|
|
|
|
|
|
|
$upload->is_completed = 1;
|
|
|
|
$upload->is_processing = 0;
|
|
|
|
$upload->save();
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handleUpload(Upload $upload)
|
|
|
|
{
|
|
|
|
$photos = $upload->uploadPhotos;
|
2016-09-05 12:01:30 +01:00
|
|
|
|
|
|
|
/** @var UploadPhoto $photo */
|
2016-09-02 21:27:50 +01:00
|
|
|
foreach ($photos as $photo)
|
|
|
|
{
|
2016-09-05 12:01:30 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$this->handlePhoto($photo);
|
|
|
|
$upload->number_successful++;
|
|
|
|
}
|
|
|
|
catch (\Exception $ex)
|
|
|
|
{
|
|
|
|
$upload->number_failed++;
|
2016-09-05 14:06:41 +01:00
|
|
|
$photo->photo->delete();
|
2016-09-05 12:01:30 +01:00
|
|
|
$photo->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
$upload->save();
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function handlePhoto(UploadPhoto $uploadPhoto)
|
|
|
|
{
|
2016-09-02 22:18:40 +01:00
|
|
|
/** @var Photo $photo */
|
2016-09-02 21:27:50 +01:00
|
|
|
$photo = $uploadPhoto->photo;
|
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
/** @var Album $album */
|
2016-09-02 21:27:50 +01:00
|
|
|
$album = $photo->album;
|
2016-09-02 22:00:42 +01:00
|
|
|
$albumSource = $album->getAlbumSource();
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
$photoFile = $albumSource->getPathToPhoto($album, $photo);
|
2016-09-02 21:27:50 +01:00
|
|
|
|
2016-09-03 17:09:49 +01:00
|
|
|
// Read and analyse Exif data
|
|
|
|
$this->output->writeln(sprintf('Analysing photo #%d: %s', $photo->id, $photo->name));
|
|
|
|
|
|
|
|
// Open the photo
|
|
|
|
$imageInfo = null;
|
|
|
|
$originalPhotoResource = $this->imageHelper->openImage($photoFile, $imageInfo);
|
|
|
|
$photo->width = $imageInfo[0];
|
|
|
|
$photo->height = $imageInfo[1];
|
|
|
|
$photo->mime_type = $imageInfo['mime'];
|
|
|
|
|
|
|
|
// Read the Exif data
|
2016-09-02 22:18:40 +01:00
|
|
|
$exifData = @exif_read_data($photoFile);
|
2016-09-05 14:06:41 +01:00
|
|
|
$isExifDataFound = ($exifData !== false && is_array($exifData));
|
2016-09-05 09:58:37 +01:00
|
|
|
$angleToRotate = 0;
|
2016-09-03 17:09:49 +01:00
|
|
|
|
2016-09-05 14:06:41 +01:00
|
|
|
if ($isExifDataFound && isset($exifData['Orientation']))
|
2016-09-05 09:58:37 +01:00
|
|
|
{
|
|
|
|
switch ($exifData['Orientation'])
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
$angleToRotate = 180;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 6:
|
|
|
|
$angleToRotate = 270;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
$angleToRotate = 90;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($angleToRotate > 0)
|
|
|
|
{
|
|
|
|
$originalPhotoResource = $this->imageHelper->rotateImage($originalPhotoResource, $angleToRotate);
|
|
|
|
|
|
|
|
if ($angleToRotate == 90 || $angleToRotate == 270)
|
|
|
|
{
|
|
|
|
$photo->width = $imageInfo[1];
|
|
|
|
$photo->height = $imageInfo[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-02 22:18:40 +01:00
|
|
|
|
2016-09-05 14:06:41 +01:00
|
|
|
if ($isExifDataFound)
|
|
|
|
{
|
|
|
|
$photo->metadata_version = ProcessUploadCommand::METADATA_VERSION;
|
|
|
|
$photo->taken_at = $this->metadataDateTime($exifData);
|
|
|
|
$photo->camera_make = $this->metadataCameraMake($exifData);
|
|
|
|
$photo->camera_model = $this->metadataCameraModel($exifData);
|
|
|
|
$photo->camera_software = $this->metadataCameraSoftware($exifData);
|
|
|
|
$photo->rotation = $angleToRotate;
|
|
|
|
}
|
|
|
|
|
2016-09-02 22:18:40 +01:00
|
|
|
$photo->save();
|
2016-09-03 17:09:49 +01:00
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
// Generate and save thumbnails
|
2016-09-03 17:09:49 +01:00
|
|
|
$this->output->writeln('Generating thumbnails');
|
|
|
|
$themeInfo = $this->themeHelper->info();
|
|
|
|
$thumbnailsRequired = $themeInfo['thumbnails'];
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
/** @var mixed $thumbnail */
|
2016-09-03 17:09:49 +01:00
|
|
|
foreach ($thumbnailsRequired as $thumbnail)
|
|
|
|
{
|
2016-09-03 22:13:05 +01:00
|
|
|
$generatedThumbnailPath = $this->imageHelper->generateThumbnail($originalPhotoResource, $photo, $thumbnail);
|
|
|
|
$albumSource->saveThumbnail($album, $photo, $thumbnail, $generatedThumbnailPath);
|
2016-09-03 17:09:49 +01:00
|
|
|
|
|
|
|
$this->output->writeln(sprintf('Thumbnail \'%s\' (%dx%d) created', $thumbnail['name'], $thumbnail['width'], $thumbnail['height']));
|
|
|
|
}
|
2016-09-02 22:18:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataCameraMake(array $exifData)
|
|
|
|
{
|
|
|
|
if (isset($exifData['Make']))
|
|
|
|
{
|
|
|
|
return $exifData['Make'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataCameraModel(array $exifData)
|
|
|
|
{
|
|
|
|
if (isset($exifData['Model']))
|
|
|
|
{
|
|
|
|
return $exifData['Model'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataCameraSoftware(array $exifData)
|
|
|
|
{
|
|
|
|
if (isset($exifData['Software']))
|
|
|
|
{
|
|
|
|
return $exifData['Software'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function metadataDateTime(array $exifData)
|
|
|
|
{
|
|
|
|
$dateTime = null;
|
|
|
|
if (isset($exifData['DateTime']))
|
|
|
|
{
|
|
|
|
$dateTime = $exifData['DateTime'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($dateTime))
|
|
|
|
{
|
|
|
|
$dateTime = preg_replace('/^([\d]{4}):([\d]{2}):([\d]{2})/', '$1-$2-$3', $dateTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dateTime;
|
2016-09-02 21:27:50 +01:00
|
|
|
}
|
|
|
|
}
|