Skip to content

Commit

Permalink
add migration script for old images to new variants structure
Browse files Browse the repository at this point in the history
  • Loading branch information
butburg committed Aug 17, 2024
1 parent edbaee1 commit 050576a
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/app/Actions/CreateImageVariants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 7 additions & 8 deletions src/app/Actions/StoreImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand All @@ -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,
Expand Down
87 changes: 87 additions & 0 deletions src/app/Console/Commands/MigrateLegacyImages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Post;
use App\Models\Image;
use App\Actions\CreateImageVariants;
use App\Enums\ImageSizeType;
use Illuminate\Support\Facades\File;


use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\File\UploadedFile;



class MigrateLegacyImages extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:migrate-legacy-images';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Migrate old images from legacy directory to the new image system';

/**
* Execute the console command.
*/
public function handle()
{
$legacyImagePath = '/var/www/html/storage/app/public/files/posts/images_legacy';

// Check if the directory exists
if (!File::exists($legacyImagePath)) {
$this->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.');
}
}

0 comments on commit 050576a

Please sign in to comment.