mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2024-11-25 06:27:59 +01:00
Moved rebuilding code into Image and added error handling.
This commit is contained in:
parent
e5c5dec14a
commit
821b5c5bdd
2 changed files with 25 additions and 6 deletions
|
@ -21,6 +21,7 @@ namespace Poniverse\Ponyfm\Console\Commands;
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Poniverse\Ponyfm\Models\Image;
|
use Poniverse\Ponyfm\Models\Image;
|
||||||
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
||||||
use Symfony\Component\HttpFoundation\File\File;
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
|
|
||||||
class RebuildImages extends Command
|
class RebuildImages extends Command
|
||||||
|
@ -61,11 +62,13 @@ class RebuildImages extends Command
|
||||||
|
|
||||||
Image::chunk(1000, function($images) use ($progressBar) {
|
Image::chunk(1000, function($images) use ($progressBar) {
|
||||||
foreach ($images as $image) {
|
foreach ($images as $image) {
|
||||||
$image->clearExisting();
|
try {
|
||||||
|
$image->buildCovers();
|
||||||
|
} catch (FileNotFoundException $e) {
|
||||||
|
$name = $image->filename;
|
||||||
|
$id = $image->id;
|
||||||
|
|
||||||
$originalFile = new File($image->getFile(Image::ORIGINAL));
|
$this->error("Unable to process image $name (id: $id): ".$e->getMessage());
|
||||||
foreach (Image::$ImageTypes as $imageType) {
|
|
||||||
Image::processFile($originalFile, $image->getFile($imageType['id']), $imageType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$progressBar->advance();
|
$progressBar->advance();
|
||||||
|
|
|
@ -24,6 +24,7 @@ use External;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Config;
|
use Config;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
|
||||||
use Symfony\Component\HttpFoundation\File\File;
|
use Symfony\Component\HttpFoundation\File\File;
|
||||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||||
|
|
||||||
|
@ -64,6 +65,8 @@ class Image extends Model
|
||||||
self::THUMBNAIL => ['id' => self::THUMBNAIL, 'name' => 'thumbnail', 'width' => 50, 'height' => 50, 'geometry' => '50x50^']
|
self::THUMBNAIL => ['id' => self::THUMBNAIL, 'name' => 'thumbnail', 'width' => 50, 'height' => 50, 'geometry' => '50x50^']
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const MIME_JPEG = 'image/jpeg';
|
||||||
|
|
||||||
public static function getImageTypeFromName($name)
|
public static function getImageTypeFromName($name)
|
||||||
{
|
{
|
||||||
foreach (self::$ImageTypes as $cover) {
|
foreach (self::$ImageTypes as $cover) {
|
||||||
|
@ -133,13 +136,13 @@ class Image extends Model
|
||||||
* @param int $coverType The type to process the image to
|
* @param int $coverType The type to process the image to
|
||||||
*/
|
*/
|
||||||
public static function processFile(File $image, string $path, $coverType) {
|
public static function processFile(File $image, string $path, $coverType) {
|
||||||
if ($coverType['id'] === self::ORIGINAL && $image->getMimeType() === 'image/jpeg') {
|
if ($coverType['id'] === self::ORIGINAL && $image->getMimeType() === self::MIME_JPEG) {
|
||||||
$command = 'cp "'.$image->getPathname().'" '.$path;
|
$command = 'cp "'.$image->getPathname().'" '.$path;
|
||||||
} else {
|
} else {
|
||||||
// ImageMagick options reference: http://www.imagemagick.org/script/command-line-options.php
|
// ImageMagick options reference: http://www.imagemagick.org/script/command-line-options.php
|
||||||
$command = 'convert 2>&1 "'.$image->getPathname().'" -background white -alpha remove -alpha off -strip';
|
$command = 'convert 2>&1 "'.$image->getPathname().'" -background white -alpha remove -alpha off -strip';
|
||||||
|
|
||||||
if ($image->getMimeType() === 'image/jpeg') {
|
if ($image->getMimeType() === self::MIME_JPEG) {
|
||||||
$command .= ' -quality 100 -format jpeg';
|
$command .= ' -quality 100 -format jpeg';
|
||||||
} else {
|
} else {
|
||||||
$command .= ' -quality 95 -format png';
|
$command .= ' -quality 95 -format png';
|
||||||
|
@ -216,4 +219,17 @@ class Image extends Model
|
||||||
unlink($this->getDirectory().'/'.$file);
|
unlink($this->getDirectory().'/'.$file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the cover images for the image, overwriting if needed.
|
||||||
|
*
|
||||||
|
* @throws FileNotFoundException If the original file cannot be found.
|
||||||
|
*/
|
||||||
|
public function buildCovers() {
|
||||||
|
$originalFile = new File($this->getFile(self::ORIGINAL));
|
||||||
|
|
||||||
|
foreach (self::$ImageTypes as $imageType) {
|
||||||
|
self::processFile($originalFile, $this->getFile($imageType['id']), $imageType);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue