-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from ijpatricio/fix-slow-seed
Use local images for seeding.
- Loading branch information
Showing
53 changed files
with
130 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Database\Seeders\LocalImages; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Str; | ||
|
||
class GetRandomImages extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'app:get-random-images'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Get random images stored locally, so that the seed process can be fast.'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
// Use an empty string to get random images | ||
// We could fine-tune with search terms, examples: 'nature', 'people', 'city', 'abstract', 'food', 'sports', 'technics', 'transport', 'animals' | ||
// This needs some deeper research to get the best results | ||
|
||
$schemas = [ | ||
['amount' => 40, 'size' => LocalImages::SIZE_200x200, 'terms' => ['']], | ||
['amount' => 40, 'size' => LocalImages::SIZE_1280x720, 'terms' => ['']], | ||
]; | ||
|
||
foreach ($schemas as $schema) { | ||
$this->getRandomImages($schema); | ||
} | ||
|
||
foreach ($schemas as $schema) { | ||
$this->removeDuplicates($schema); | ||
} | ||
} | ||
|
||
protected function getRandomImages($schema) | ||
{ | ||
['amount' => $amount, 'size' => $size, 'terms' => $terms] = $schema; | ||
|
||
$this->comment("Getting $amount random images of size $size, of topic: " . implode(', ', $terms)); | ||
|
||
File::deleteDirectory(database_path('seeders/local_images/' . $size)); | ||
|
||
$progressBar = $this->output->createProgressBar($amount); | ||
$progressBar->start(); | ||
|
||
foreach (range(1, $amount) as $i) { | ||
$url = "https://source.unsplash.com/{$size}/?img=1," . implode(',', $terms); | ||
$image = file_get_contents($url); | ||
|
||
File::ensureDirectoryExists(database_path('seeders/local_images/' . $size)); | ||
$filename = Str::uuid() . '.jpg'; | ||
|
||
File::put( | ||
database_path( | ||
path: "seeders/local_images/{$size}/{$filename}" | ||
), | ||
contents: $image | ||
); | ||
|
||
$progressBar->advance(); | ||
} | ||
|
||
$progressBar->finish(); | ||
|
||
$this->newLine(); | ||
$this->info('Done!'); | ||
} | ||
|
||
protected function removeDuplicates($schema) | ||
{ | ||
['size' => $size] = $schema; | ||
|
||
$allFiles = fn () => collect(File::files(database_path('seeders/local_images/' . $size))); | ||
|
||
$uniqueImageSet = $allFiles() | ||
->mapWithKeys(fn ($file) => [md5_file($file->getPathname()) => $file->getPathname()]) | ||
->values(); | ||
|
||
$allFiles() | ||
->map(fn ($file) => $file->getPathname()) | ||
->diff($uniqueImageSet) | ||
->each(fn ($file) => File::delete($file)); | ||
|
||
$this->info('Kept ' . $uniqueImageSet->count() . " unique files from size $size"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Database\Seeders; | ||
|
||
use Illuminate\Support\Facades\File; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
|
||
class LocalImages | ||
{ | ||
public const SIZE_200x200 = '200x200'; | ||
|
||
public const SIZE_1280x720 = '1280x720'; | ||
|
||
public static function getRandomFile(?string $size = LocalImages::SIZE_200x200): SplFileInfo | ||
{ | ||
return collect( | ||
File::files(database_path('seeders/local_images/' . $size)) | ||
)->random(); | ||
} | ||
} |
Binary file added
BIN
+72.1 KB
database/seeders/local_images/1280x720/05e68fd7-d7ac-46a3-837e-20937f8dc899.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+189 KB
database/seeders/local_images/1280x720/3188fb8e-15a9-4afb-9182-1aebdcf11b83.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+58.1 KB
database/seeders/local_images/1280x720/50f608f8-2549-4ecf-b6f6-a5997c13cb63.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+121 KB
database/seeders/local_images/1280x720/5b22e6fe-5958-4a9c-99f1-33bf59085469.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+66.1 KB
database/seeders/local_images/1280x720/609dd36a-1011-4185-b736-63971a6fb854.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+90.3 KB
database/seeders/local_images/1280x720/708c814b-0bbf-40f9-af54-f15673585d99.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+49.5 KB
database/seeders/local_images/1280x720/80043582-b25d-4403-8166-03baddfb96bc.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+193 KB
database/seeders/local_images/1280x720/80654c36-50f4-4144-91e9-58fc49b83f90.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+60.4 KB
database/seeders/local_images/1280x720/9144a5b3-efc6-42de-8b2d-fb77ea0656c6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+175 KB
database/seeders/local_images/1280x720/99b95dcc-4a4f-4523-922a-b6f776d09faa.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+161 KB
database/seeders/local_images/1280x720/9b3227b0-584c-48f4-962f-b42318bade61.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+133 KB
database/seeders/local_images/1280x720/9cc59994-a348-4a55-8885-b6d478e2d2c7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+80.6 KB
database/seeders/local_images/1280x720/9eda78f4-ac0c-4af9-b6a8-14f8727294dd.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+37.7 KB
database/seeders/local_images/1280x720/b5d43aee-bf02-4331-8916-b10523ef861f.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+393 KB
database/seeders/local_images/1280x720/cb6ca3f6-3643-4bc1-a0de-c113db3a92e0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+56.6 KB
database/seeders/local_images/1280x720/ccd5cbc5-afa7-4709-b572-9f72c6716846.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+144 KB
database/seeders/local_images/1280x720/d970b8da-95e6-456e-9ddb-7cb1aad2de56.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+71.4 KB
database/seeders/local_images/1280x720/dc5d4460-1637-4ae5-ab05-6732b0e9d3b8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+109 KB
database/seeders/local_images/1280x720/dd133d03-b444-4a19-a728-2eaab2a547ee.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+96.9 KB
database/seeders/local_images/1280x720/e837e969-cbfe-4995-aaea-ee32cf4ba4d0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+47.5 KB
database/seeders/local_images/1280x720/f4e246e7-8bee-4011-8c8d-debff845d198.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+63 KB
database/seeders/local_images/1280x720/f9697b3c-72df-4917-8317-6c3bdc530225.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+112 KB
database/seeders/local_images/1280x720/ffe9e024-8a9c-4bbb-ae13-e99a620ae2f7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.51 KB
database/seeders/local_images/200x200/1aa69e68-a504-4495-bc38-5c520d035d8b.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+17.9 KB
database/seeders/local_images/200x200/20d2e61e-3273-4521-9c47-e56a68c892fb.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+8.02 KB
database/seeders/local_images/200x200/248eadf6-5227-42f3-ad31-1c320febdb7b.jpg
Oops, something went wrong.
Binary file added
BIN
+9.48 KB
database/seeders/local_images/200x200/2f5c09b5-6268-47b2-8e0b-c4890850e727.jpg
Oops, something went wrong.
Binary file added
BIN
+11 KB
database/seeders/local_images/200x200/3b95f9cc-e3e3-48d3-b743-443210fa19ec.jpg
Oops, something went wrong.
Binary file added
BIN
+10.6 KB
database/seeders/local_images/200x200/3b9f504b-d276-4d4a-9997-68979f1bddcc.jpg
Oops, something went wrong.
Binary file added
BIN
+8.38 KB
database/seeders/local_images/200x200/447a4c77-e7a2-49a3-8015-2dc5ba055c24.jpg
Oops, something went wrong.
Binary file added
BIN
+5.59 KB
database/seeders/local_images/200x200/482659c5-764a-4071-848a-fa1fbd786fd5.jpg
Oops, something went wrong.
Binary file added
BIN
+8.81 KB
database/seeders/local_images/200x200/503a4fac-68d7-4b64-ad44-5c2ec3986b94.jpg
Oops, something went wrong.
Binary file added
BIN
+6.32 KB
database/seeders/local_images/200x200/57623f03-f7c2-4179-a366-f1530993bc09.jpg
Oops, something went wrong.
Binary file added
BIN
+4.84 KB
database/seeders/local_images/200x200/680520bc-e880-4704-8dc7-a425d55b7a81.jpg
Oops, something went wrong.
Binary file added
BIN
+9.65 KB
database/seeders/local_images/200x200/74d81728-0dc2-43e2-a7da-332e2e2e6c89.jpg
Oops, something went wrong.
Binary file added
BIN
+5.36 KB
database/seeders/local_images/200x200/7e4b0b96-99df-4d1e-84fa-dad2b398f172.jpg
Oops, something went wrong.
Binary file added
BIN
+6.27 KB
database/seeders/local_images/200x200/873353ff-3d3a-4bfc-a02d-22949e6e4558.jpg
Oops, something went wrong.
Binary file added
BIN
+6.48 KB
database/seeders/local_images/200x200/948bfac1-6d9b-430f-86fc-8d2d6ef680ed.jpg
Oops, something went wrong.
Binary file added
BIN
+12.5 KB
database/seeders/local_images/200x200/b02337ca-473b-4ae1-957b-0c7854161f8e.jpg
Oops, something went wrong.
Binary file added
BIN
+4.65 KB
database/seeders/local_images/200x200/b14d2dca-4d45-4cc0-8d8b-864e10081dac.jpg
Oops, something went wrong.
Binary file added
BIN
+9.18 KB
database/seeders/local_images/200x200/b37987d1-a9ad-4671-9066-9812a033fdf4.jpg
Oops, something went wrong.
Binary file added
BIN
+9.01 KB
database/seeders/local_images/200x200/cf755312-0709-4f40-be4a-bba7edbcdabb.jpg
Oops, something went wrong.
Binary file added
BIN
+5.88 KB
database/seeders/local_images/200x200/dce10027-9b5d-44a5-91ed-7caeb23100bd.jpg
Oops, something went wrong.
Binary file added
BIN
+8.98 KB
database/seeders/local_images/200x200/e5d18094-036b-4c48-8232-8e0d9971cca0.jpg
Oops, something went wrong.
Binary file added
BIN
+7.67 KB
database/seeders/local_images/200x200/edc07898-49a5-4eb0-8af8-2a9ceeb3baf5.jpg
Oops, something went wrong.
Binary file added
BIN
+7.63 KB
database/seeders/local_images/200x200/f3f0ce20-b7a3-47db-b907-c77c5e6ca038.jpg
Oops, something went wrong.
Binary file added
BIN
+8.99 KB
database/seeders/local_images/200x200/f8c8557d-bd70-4cf7-978c-89f35e918c8f.jpg
Oops, something went wrong.