120 lines
3.1 KiB
PHP
120 lines
3.1 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Console\Commands;
|
||
|
|
||
|
use App\Album;
|
||
|
use App\Helpers\ImageHelper;
|
||
|
use App\Helpers\ThemeHelper;
|
||
|
use App\Photo;
|
||
|
use Illuminate\Console\Command;
|
||
|
|
||
|
class RegenerateThumbnailsCommand extends Command
|
||
|
{
|
||
|
/**
|
||
|
* The name and signature of the console command.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $signature = 'twilight:regenerate-thumbnails {--album} {id}';
|
||
|
|
||
|
/**
|
||
|
* The console command description.
|
||
|
*
|
||
|
* @var string
|
||
|
*/
|
||
|
protected $description = 'Recreates thumbnails for an image or an album.';
|
||
|
|
||
|
/**
|
||
|
* @var ImageHelper
|
||
|
*/
|
||
|
private $imageHelper;
|
||
|
|
||
|
/**
|
||
|
* @var ThemeHelper
|
||
|
*/
|
||
|
private $themeHelper;
|
||
|
|
||
|
/**
|
||
|
* 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()
|
||
|
{
|
||
|
$id = intval($this->argument('id'));
|
||
|
if ($this->option('album'))
|
||
|
{
|
||
|
$this->handleAlbum($id);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->handlePhoto($id);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function handleAlbum($id)
|
||
|
{
|
||
|
$album = Album::where('id', $id)->first();
|
||
|
if (is_null($album))
|
||
|
{
|
||
|
throw new \Exception(sprintf('The album with ID %d could not be found', $id));
|
||
|
}
|
||
|
|
||
|
/** @var Photo $photo */
|
||
|
foreach ($album->photos as $photo)
|
||
|
{
|
||
|
$this->regenerateThumbnailsForPhoto($album, $photo);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private function handlePhoto($id)
|
||
|
{
|
||
|
$photo = Photo::where('id', $id)->first();
|
||
|
if (is_null($photo))
|
||
|
{
|
||
|
throw new \Exception(sprintf('The photo with ID %d could not be found', $id));
|
||
|
}
|
||
|
|
||
|
/** @var Album $album */
|
||
|
$album = $photo->album;
|
||
|
$this->regenerateThumbnailsForPhoto($album, $photo);
|
||
|
}
|
||
|
|
||
|
private function regenerateThumbnailsForPhoto(Album $album, Photo $photo)
|
||
|
{
|
||
|
$albumSource = $album->getAlbumSource();
|
||
|
|
||
|
$originalPhotoResource = $this->imageHelper->openImage($albumSource->getPathToPhoto($album, $photo), $imageInfo);
|
||
|
if (!is_resource($originalPhotoResource))
|
||
|
{
|
||
|
throw new \Exception(sprintf('The original image for photo ID %d could not be found', $id));
|
||
|
}
|
||
|
|
||
|
$this->output->writeln(sprintf('Generating thumbnails for "%s"...', $photo->file_name));
|
||
|
|
||
|
$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']));
|
||
|
}
|
||
|
}
|
||
|
}
|