diff --git a/app/Console/Commands/RebuildImages.php b/app/Console/Commands/RebuildImages.php index 38959af9..408af47a 100644 --- a/app/Console/Commands/RebuildImages.php +++ b/app/Console/Commands/RebuildImages.php @@ -21,6 +21,7 @@ namespace Poniverse\Ponyfm\Console\Commands; use Illuminate\Console\Command; use Poniverse\Ponyfm\Models\Image; +use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; use Symfony\Component\HttpFoundation\File\File; class RebuildImages extends Command @@ -61,11 +62,13 @@ class RebuildImages extends Command Image::chunk(1000, function($images) use ($progressBar) { 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)); - foreach (Image::$ImageTypes as $imageType) { - Image::processFile($originalFile, $image->getFile($imageType['id']), $imageType); + $this->error("Unable to process image $name (id: $id): ".$e->getMessage()); } $progressBar->advance(); diff --git a/app/Models/Image.php b/app/Models/Image.php index 5f4e99e8..ce2875c8 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -24,6 +24,7 @@ use External; use Illuminate\Database\Eloquent\Model; use Config; use Illuminate\Support\Str; +use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; use Symfony\Component\HttpFoundation\File\File; 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^'] ]; + const MIME_JPEG = 'image/jpeg'; + public static function getImageTypeFromName($name) { foreach (self::$ImageTypes as $cover) { @@ -133,13 +136,13 @@ class Image extends Model * @param int $coverType The type to process the image to */ 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; } else { // 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'; - if ($image->getMimeType() === 'image/jpeg') { + if ($image->getMimeType() === self::MIME_JPEG) { $command .= ' -quality 100 -format jpeg'; } else { $command .= ' -quality 95 -format png'; @@ -216,4 +219,17 @@ class Image extends Model 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); + } + } }