From e5c5dec14aee3ffba84f26d0b11c60847f0418fc Mon Sep 17 00:00:00 2001 From: Isaac Date: Tue, 28 Nov 2017 11:19:36 -0800 Subject: [PATCH] Some small changes --- app/Console/Commands/RebuildImages.php | 21 +++++++++++++-------- app/Models/Image.php | 12 ++++++++---- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/Console/Commands/RebuildImages.php b/app/Console/Commands/RebuildImages.php index 30b28dda..38959af9 100644 --- a/app/Console/Commands/RebuildImages.php +++ b/app/Console/Commands/RebuildImages.php @@ -56,15 +56,20 @@ class RebuildImages extends Command */ public function handle() { - $images = Image::all(); - foreach ($images as $image) { - $this->info("Regenerating images for id:".$image->id. " (".$image->filename.")"); - $image->clearExisting(); + $this->info("Regenerating Images"); + $progressBar = $this->output->createProgressBar(Image::count()); - $originalFile = new File($image->getFile(Image::ORIGINAL)); - foreach (Image::$ImageTypes as $imageType) { - Image::processFile($originalFile, $image->getFile($imageType['id']), $imageType); + Image::chunk(1000, function($images) use ($progressBar) { + foreach ($images as $image) { + $image->clearExisting(); + + $originalFile = new File($image->getFile(Image::ORIGINAL)); + foreach (Image::$ImageTypes as $imageType) { + Image::processFile($originalFile, $image->getFile($imageType['id']), $imageType); + } + + $progressBar->advance(); } - } + }); } } diff --git a/app/Models/Image.php b/app/Models/Image.php index 253ce857..5f4e99e8 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -196,16 +196,20 @@ class Image extends Model /** * Deletes any generated files if they exist - * @param bool $includeOriginal Deletes + * @param bool $includeOriginal Set to true if the original image should be deleted as well. */ - public function clearExisting($includeOriginal = false) { + public function clearExisting(bool $includeOriginal = false) { $files = scandir($this->getDirectory()); $filePrefix = $this->id.'_'; $originalName = $filePrefix.Image::$ImageTypes[Image::ORIGINAL]['name']; $files = array_filter($files, function($file) use ($originalName, $includeOriginal, $filePrefix) { - if (Str::startsWith($file,$originalName) && !$includeOriginal) return false; - else return (Str::startsWith($file, $filePrefix)); + if (Str::startsWith($file,$originalName) && !$includeOriginal) { + return false; + } + else { + return (Str::startsWith($file, $filePrefix)); + } }); foreach ($files as $file) {