blue-twilight/app/Console/Commands/ProcessUploadCommand.php

235 lines
6.0 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Album;
use App\Helpers\ImageHelper;
use App\Helpers\ThemeHelper;
use App\Photo;
use App\Upload;
use App\UploadPhoto;
use Illuminate\Console\Command;
class ProcessUploadCommand extends Command
{
const METADATA_VERSION = 1;
/**
* @var ImageHelper
*/
private $imageHelper;
/**
* @var ThemeHelper
*/
private $themeHelper;
/**
* 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
*/
public function __construct(ImageHelper $imageHelper, ThemeHelper $themeHelper)
{
parent::__construct();
$this->imageHelper = $imageHelper;
$this->themeHelper = $themeHelper;
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$uploadsToProcess = Upload::where([
['is_completed', false],
['is_processing', false],
['is_ready', true]
])
->orderBy('created_at')
->get();
/** @var Upload $upload */
foreach ($uploadsToProcess as $upload)
{
$upload->is_processing = 1;
$upload->save();
$this->output->writeln(sprintf('Processing upload #%d', $upload->id));
$this->handleUpload($upload);
$upload->is_completed = 1;
$upload->is_processing = 0;
$upload->save();
}
}
private function handleUpload(Upload $upload)
{
$photos = $upload->uploadPhotos;
/** @var UploadPhoto $photo */
foreach ($photos as $photo)
{
try
{
$this->handlePhoto($photo);
$upload->number_successful++;
}
catch (\Exception $ex)
{
$upload->number_failed++;
$photo->photo->delete();
$photo->delete();
}
$upload->save();
}
}
private function handlePhoto(UploadPhoto $uploadPhoto)
{
/** @var Photo $photo */
$photo = $uploadPhoto->photo;
/** @var Album $album */
$album = $photo->album;
$albumSource = $album->getAlbumSource();
$photoFile = $albumSource->getPathToPhoto($album, $photo);
// 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
$exifData = @exif_read_data($photoFile);
$isExifDataFound = ($exifData !== false && is_array($exifData));
$angleToRotate = 0;
if ($isExifDataFound && isset($exifData['Orientation']))
{
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];
}
}
}
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;
}
$photo->save();
// Generate and save thumbnails
$this->output->writeln('Generating thumbnails');
$themeInfo = $this->themeHelper->info();
$thumbnailsRequired = $themeInfo['thumbnails'];
/** @var mixed $thumbnail */
foreach ($thumbnailsRequired as $thumbnail)
{
$generatedThumbnailPath = $this->imageHelper->generateThumbnail($originalPhotoResource, $photo, $thumbnail);
$albumSource->saveThumbnail($album, $photo, $thumbnail, $generatedThumbnailPath);
$this->output->writeln(sprintf('Thumbnail \'%s\' (%dx%d) created', $thumbnail['name'], $thumbnail['width'], $thumbnail['height']));
}
}
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;
}
}