Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Imagekit Laravel Package

devmahmoudmustafa/laravel-imagekit

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require devmahmoudmustafa/laravel-imagekit
    php artisan vendor:publish --provider="DevMahmoudMustafa\ImageKit\ImageKitServiceProvider"
    

    Publish the config file to config/imagekit.php.

  2. 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]);
    }
    
  3. Where to Look First:

    • Config File: config/imagekit.php for default settings.
    • Facade: ImageKit for quick usage in controllers.
    • Methods Reference: Focus on upload(), resize(), watermark(), and compress() methods.

Implementation Patterns

Core Workflows

  1. Basic Upload with Processing:

    $path = ImageKit::upload($request->file('image'), [
        'disk' => 's3',
        'folder' => 'user-avatars',
        'resize' => ['width' => 300, 'height' => 300, 'fit' => 'cover'],
        'compress' => 75
    ]);
    
  2. 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);
    
  3. Batch Processing:

    $images = $request->file('images');
    $results = [];
    foreach ($images as $image) {
        $results[] = ImageKit::upload($image, ['folder' => 'batch-uploads']);
    }
    
  4. Integration with Storage Disks:

    // Use a custom disk configuration
    $path = ImageKit::upload($image, [
        'disk' => 'custom-s3',
        'folder' => 'processed',
        'visibility' => 'private'
    ]);
    
  5. Event-Driven Processing:

    // Listen for image upload events
    event(new \DevMahmoudMustafa\ImageKit\Events\ImageUploaded($path, $options));
    

Integration Tips

  • 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)
        ]
    ]);
    

Gotchas and Tips

Pitfalls

  1. Disk Configuration Mismatch:

    • Ensure the disk option in ImageKit::upload() matches a configured disk in config/filesystems.php.
    • Fix: Validate disk existence before upload:
      if (!Storage::disks()->has('custom-disk')) {
          throw new \Exception('Disk not configured');
      }
      
  2. Watermark Path Issues:

    • Watermark paths must be absolute or resolvable via public_path()/storage_path().
    • Fix: Use helper methods:
      'watermark' => [
          'path' => public_path('images/watermark.png'),
          'position' => 'center'
      ]
      
  3. Memory Limits:

    • Large image resizing may hit PHP memory limits.
    • Fix: Increase memory_limit in php.ini or process in chunks.
  4. Event Listener Conflicts:

    • Custom events may conflict with package events.
    • Fix: Use unique event names or namespace your listeners.

Debugging

  • 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'));
    

Tips

  1. 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']);
    
  2. Leverage Events for Notifications:

    // Listen for ImageUploaded event
    ImageUploaded::subscribe(function ($event) {
        Mail::to($event->user)->send(new ImageProcessed($event->path));
    });
    
  3. Optimize for CDN:

    $path = ImageKit::upload($image, [
        'disk' => 'cdn',
        'folder' => 'optimized',
        'compress' => 90,
        'format' => 'webp'
    ]);
    
  4. Fallback for Missing Features:

    try {
        $path = ImageKit::upload($image, ['watermark' => ['path' => $watermark]]);
    } catch (\Exception $e) {
        $path = ImageKit::upload($image); // Fallback to no watermark
    }
    
  5. Test with Local Storage First:

    • Use local disk during development to avoid cloud costs:
      $path = ImageKit::upload($image, ['disk' => 'local']);
      
  6. 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;
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui