103 lines
3.6 KiB
PHP
103 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Person;
|
|
use App\Models\Photo;
|
|
use Illuminate\Database\Seeder;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
class PhotoSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
// Create the storage directory if it doesn't exist
|
|
if (!File::exists(storage_path('app/public/photos'))) {
|
|
File::makeDirectory(storage_path('app/public/photos'), 0755, true);
|
|
}
|
|
|
|
// Sample image URLs for testing
|
|
$sampleImages = [
|
|
'https://randomuser.me/api/portraits/men/1.jpg',
|
|
'https://randomuser.me/api/portraits/men/2.jpg',
|
|
'https://randomuser.me/api/portraits/men/3.jpg',
|
|
'https://randomuser.me/api/portraits/women/1.jpg',
|
|
'https://randomuser.me/api/portraits/women/2.jpg',
|
|
'https://randomuser.me/api/portraits/women/3.jpg',
|
|
];
|
|
|
|
// Sample captions
|
|
$sampleCaptions = [
|
|
'Official ID photo',
|
|
'Family portrait',
|
|
'At work',
|
|
'Travel document photo',
|
|
'Residence permit photo',
|
|
'Personal photo',
|
|
];
|
|
|
|
// Get all person records
|
|
$persons = Person::all();
|
|
|
|
// Process each person
|
|
foreach ($persons as $person) {
|
|
// Skip some persons to have variety (30% chance to skip)
|
|
if (rand(1, 10) <= 3) {
|
|
continue;
|
|
}
|
|
|
|
// Create 1-3 photos for this person
|
|
$numPhotos = rand(1, 3);
|
|
$profilePhotoSet = false;
|
|
|
|
for ($i = 0; $i < $numPhotos; $i++) {
|
|
// Choose a random sample image
|
|
$imageUrl = $sampleImages[array_rand($sampleImages)];
|
|
$imageData = file_get_contents($imageUrl);
|
|
|
|
// Create directory for this person if it doesn't exist
|
|
$personDir = storage_path('app/public/photos/' . $person->person_id);
|
|
if (!File::exists($personDir)) {
|
|
File::makeDirectory($personDir, 0755, true);
|
|
}
|
|
|
|
// Generate a unique filename
|
|
$filename = Str::uuid() . '.jpg';
|
|
$fullPath = $personDir . '/' . $filename;
|
|
|
|
// Save the image file
|
|
file_put_contents($fullPath, $imageData);
|
|
|
|
// Choose if this should be a profile photo
|
|
// First photo has 70% chance, otherwise 0% chance
|
|
$isProfilePhoto = !$profilePhotoSet && (rand(1, 10) <= 7);
|
|
|
|
if ($isProfilePhoto) {
|
|
$profilePhotoSet = true;
|
|
}
|
|
|
|
// Create the photo record
|
|
$photo = new Photo([
|
|
'person_id' => $person->person_id,
|
|
'filename' => $filename,
|
|
'original_filename' => 'sample_' . rand(1000, 9999) . '.jpg',
|
|
'file_path' => '/storage/photos/' . $person->person_id . '/' . $filename,
|
|
'mime_type' => 'image/jpeg',
|
|
'file_size' => strlen($imageData) / 1024, // Convert to KB
|
|
'caption' => $sampleCaptions[array_rand($sampleCaptions)],
|
|
'is_profile_photo' => $isProfilePhoto
|
|
]);
|
|
|
|
$photo->save();
|
|
}
|
|
}
|
|
|
|
$this->command->info('Created photos for ' . Photo::count() . ' person records');
|
|
}
|
|
}
|