diff --git a/src/app/Actions/CreateImageVariants.php b/src/app/Actions/CreateImageVariants.php index f24f70f..c873ff6 100644 --- a/src/app/Actions/CreateImageVariants.php +++ b/src/app/Actions/CreateImageVariants.php @@ -5,12 +5,12 @@ use App\Models\Image; use App\Models\ImageVariant; use App\Enums\ImageSizeType; -use Illuminate\Http\UploadedFile; + class CreateImageVariants { - public function handleVariant(Image $image, UploadedFile $file, array $sizeTypes, string $path = 'posts/images/'): void + public function handleVariant(Image $image, $file, array $sizeTypes, string $path = 'posts/images/'): void { foreach ($sizeTypes as $sizeType) { $sizeTypeEnum = ImageSizeType::from($sizeType); diff --git a/src/app/Actions/StoreImage.php b/src/app/Actions/StoreImage.php index 45aa517..437d662 100644 --- a/src/app/Actions/StoreImage.php +++ b/src/app/Actions/StoreImage.php @@ -5,24 +5,27 @@ use Intervention\Image\Laravel\Facades\Image; // Import Image facade use Illuminate\Support\Facades\Storage; use App\Enums\ImageSizeType; -use Illuminate\Http\UploadedFile; class StoreImage { - public function handleStore(UploadedFile $file, ImageSizeType $sizeType, string $filePath): array + public function handleStore($file, ImageSizeType $sizeType, string $filePath): array { // Load the image $image = Image::read($file); + // reading the image width and height + $width = $image->width(); + $height = $image->height(); + // scale the image, only if its an avatar, make it scale and crop (cover) as a square // than set quality and make progressive, better for web loading if ($sizeType == ImageSizeType::EXTRA_SMALL or $sizeType == ImageSizeType::SMALL) { - $image->cover( + $image->coverDown( width: $sizeType->getMaxWidth(), height: $sizeType->getMaxHeight() ); } else { - $image->scale( + $image->scaleDown( width: $sizeType->getMaxWidth(), height: $sizeType->getMaxHeight() ); @@ -31,10 +34,6 @@ public function handleStore(UploadedFile $file, ImageSizeType $sizeType, string $encodedImage = $image->toJpeg(quality: $sizeType->getQuality(), progressive: true); Storage::disk('public')->put($filePath, $encodedImage); - // reading the image width and height - $width = $image->width(); - $height = $image->height(); - return [ 'path' => $filePath, 'width' => $width, diff --git a/src/app/Console/Commands/MigrateLegacyImages.php b/src/app/Console/Commands/MigrateLegacyImages.php new file mode 100644 index 0000000..7c0678d --- /dev/null +++ b/src/app/Console/Commands/MigrateLegacyImages.php @@ -0,0 +1,87 @@ +error("Legacy image directory does not exist: $legacyImagePath"); + return 1; // Return error code + } + + // Fetch all legacy image files + $files = File::allFiles($legacyImagePath); + + + foreach ($files as $file) { + // Extract file information + $filename = pathinfo($file->getFilename(), PATHINFO_FILENAME); + $extension = $file->getExtension(); + $fullFileName = $filename . '.' . $extension; + + // Find the post by title + $post = Post::where('title', $filename)->first(); + + if (!$post) { + $this->error("Post not found for image: $fullFileName"); + continue; // Skip to the next file if no matching post is found + } + + // Create a new image record + $image = Image::create([ + 'post_id' => $post->id, + 'upload_size' => filesize($file), + ]); + + + // Define the desired (wanted) image sizes + $desiredSizes = [ + 's' => ImageSizeType::SMALL->value, + 'm' => ImageSizeType::MEDIUM->value, + 'l' => ImageSizeType::LARGE->value, + 'xl' => ImageSizeType::EXTRA_LARGE->value, + ]; + + // Save models and files in storage + app(CreateImageVariants::class)->handleVariant($image, $file, $desiredSizes, 'posts/images/'); + } + + $this->info('Legacy images migrated successfully.'); + } +}