Installation:
composer require plutuss/gif-creator-laravel
For Laravel < 5.5, register the service provider and facade in config/app.php.
First Use Case: Convert an array of images into a GIF and display it in a browser:
use Plutuss\Gif\Facades\GifCreator;
$gif = GifCreator::storage('public')
->frames(['frame1.png', 'frame2.png'], 'path/to/images/')
->durations([500, 500]) // Durations in milliseconds
->makeGif();
return $gif->show(); // Directly output the GIF in a view
Where to Look First:
GifCreator for fluent method chaining.storage('disk-name') to specify where frames are read from (e.g., public, s3).frames(), durations(), and makeGif() for basic workflows.Dynamic GIF Generation from User Uploads:
$request->validate(['images' => 'required|array']);
$files = $request->file('images');
$gif = GifCreator::storage('public')
->frames(array_map(fn($file) => $file->hashName(), $files), storage_path('app/public'))
->durations(array_fill(0, count($files), 300)) // 300ms per frame
->loop(0) // Infinite loop
->makeGif();
return $gif->save('user-gifs/', 'user-'.auth()->id().'.gif');
Background Processing with Queues: Use Laravel Queues to offload GIF creation:
// Dispatch a job
CreateGifJob::dispatch($imagePaths, $durations, $outputPath);
// Job class
public function handle() {
$gif = GifCreator::storage('public')
->frames($this->imagePaths, storage_path('app/public'))
->durations($this->durations)
->makeGif();
Storage::disk('public')->put($this->outputPath, $gif->getGif());
}
Reusing GIF Data: Cache generated GIFs to avoid reprocessing:
$cacheKey = "gif_{$request->input('config_hash')}";
if (Cache::has($cacheKey)) {
return response()->make(Cache::get($cacheKey), 200, ['Content-Type' => 'image/gif']);
}
$gif = GifCreator::storage('public')
->frames($frames)
->durations($durations)
->makeGif();
Cache::put($cacheKey, $gif->getGif(), now()->addHours(1));
return $gif->show();
$this->validateFrames($frames, $path);
$this->validateDurations($durations, count($frames));
makeGif() in try-catch to handle missing files or corrupt images.Frame Path Resolution:
frames() expects paths relative to the disk’s root (e.g., storage/app/public for public disk).storage_path():
$frames = array_map(fn($file) => "public/{$file}", $request->input('frames'));
Memory Limits:
memory_limit.memory_limit in .env or process frames in batches.Disk Permissions:
Storage::put() fails silently if the disk lacks write permissions.if (!Storage::disk('public')->exists('gif/')) {
Storage::disk('public')->makeDirectory('gif/');
}
Facade Not Registered:
GifCreator throws ClassNotFound if the service provider isn’t registered (Laravel < 5.5).config/app.php.\Log::debug('Frame paths:', ['paths' => $frames, 'disk' => $disk]);
dd($gif->getGif()) to verify binary output before saving.Custom Frame Sources: Extend the facade to support dynamic frame generation (e.g., Canvas API):
// Hypothetical extension
GifCreator::canvasFrames(function() {
return collect(range(1, 10))->map(fn($i) => Canvas::drawFrame($i));
});
Post-Processing:
Chain additional methods after makeGif():
$gif->optimize(); // Hypothetical method to reduce file size
Event Listeners: Trigger events for GIF creation/saving:
event(new GifCreated($gif, $outputPath));
local disk if none is specified. Explicitly set it for clarity:
GifCreator::storage('public')->frames(...);
How can I help you explore Laravel packages today?