devmahmoudmustafa/laravel-imagekit
Installation:
composer require devmahmoudmustafa/laravel-imagekit
php artisan vendor:publish --provider="DevMahmoudMustafa\ImageKit\ImageKitServiceProvider"
Publish the config file to config/imagekit.php.
First Use Case: Upload and process an image in a controller:
use DevMahmoudMustafa\ImageKit\Facades\ImageKit;
public function uploadImage(Request $request) {
$image = $request->file('image');
$path = ImageKit::upload($image, [
'disk' => 'public',
'folder' => 'uploads',
'resize' => ['width' => 800, 'height' => 600, 'fit' => 'crop'],
'compress' => 80,
'watermark' => [
'path' => public_path('images/watermark.png'),
'position' => 'bottom-right',
'opacity' => 50
]
]);
return response()->json(['path' => $path]);
}
Where to Look First:
config/imagekit.php for default settings.ImageKit for quick usage in controllers.upload(), resize(), watermark(), and compress() methods.Basic Upload with Processing:
$path = ImageKit::upload($request->file('image'), [
'disk' => 's3',
'folder' => 'user-avatars',
'resize' => ['width' => 300, 'height' => 300, 'fit' => 'cover'],
'compress' => 75
]);
Dynamic Processing Based on Context:
$options = [
'resize' => ($request->input('thumbnail') ? ['width' => 200, 'height' => 200] : null),
'watermark' => ($request->user()->isPremium() ? ['path' => premiumWatermarkPath()] : null)
];
$path = ImageKit::upload($image, $options);
Batch Processing:
$images = $request->file('images');
$results = [];
foreach ($images as $image) {
$results[] = ImageKit::upload($image, ['folder' => 'batch-uploads']);
}
Integration with Storage Disks:
// Use a custom disk configuration
$path = ImageKit::upload($image, [
'disk' => 'custom-s3',
'folder' => 'processed',
'visibility' => 'private'
]);
Event-Driven Processing:
// Listen for image upload events
event(new \DevMahmoudMustafa\ImageKit\Events\ImageUploaded($path, $options));
Form Request Validation:
public function rules() {
return [
'image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
];
}
Service Layer Abstraction:
class ImageService {
public function processImage($image, array $options) {
return ImageKit::upload($image, $options);
}
}
API Response Handling:
return response()->json([
'success' => true,
'data' => [
'path' => $path,
'url' => Storage::disk('public')->url($path)
]
]);
Disk Configuration Mismatch:
disk option in ImageKit::upload() matches a configured disk in config/filesystems.php.if (!Storage::disks()->has('custom-disk')) {
throw new \Exception('Disk not configured');
}
Watermark Path Issues:
public_path()/storage_path().'watermark' => [
'path' => public_path('images/watermark.png'),
'position' => 'center'
]
Memory Limits:
memory_limit in php.ini or process in chunks.Event Listener Conflicts:
Log Processing Steps:
\Log::info('ImageKit options:', $options);
$path = ImageKit::upload($image, $options);
Check Storage Permissions:
chmod -R 775 storage/app/public
Validate ImageKit Config:
dd(config('imagekit'));
Reuse Configurations:
// config/imagekit.php
'presets' => [
'thumbnail' => ['resize' => ['width' => 200, 'height' => 200]],
'product' => ['resize' => ['width' => 800, 'height' => 800], 'compress' => 85]
];
// Usage
$path = ImageKit::upload($image, ['preset' => 'thumbnail']);
Leverage Events for Notifications:
// Listen for ImageUploaded event
ImageUploaded::subscribe(function ($event) {
Mail::to($event->user)->send(new ImageProcessed($event->path));
});
Optimize for CDN:
$path = ImageKit::upload($image, [
'disk' => 'cdn',
'folder' => 'optimized',
'compress' => 90,
'format' => 'webp'
]);
Fallback for Missing Features:
try {
$path = ImageKit::upload($image, ['watermark' => ['path' => $watermark]]);
} catch (\Exception $e) {
$path = ImageKit::upload($image); // Fallback to no watermark
}
Test with Local Storage First:
local disk during development to avoid cloud costs:
$path = ImageKit::upload($image, ['disk' => 'local']);
Cache Processed Images:
$cacheKey = 'image_' . md5($image->getClientOriginalName());
if (cache()->has($cacheKey)) {
return cache()->get($cacheKey);
}
$path = ImageKit::upload($image, $options);
cache()->put($cacheKey, $path, now()->addHours(1));
return $path;
How can I help you explore Laravel packages today?