- How do I install spatie/image in a Laravel project?
- Run `composer require spatie/image` in your project directory. The package requires PHP 8.0+ and one of the extensions: GD, Imagick, or VIPS. No additional Laravel-specific setup is needed beyond requiring the package.
- Which Laravel versions does spatie/image support?
- The package is framework-agnostic but works seamlessly with Laravel 8.x, 9.x, and 10.x. It doesn’t rely on Laravel’s core, so it won’t conflict with newer versions unless you’re using Laravel-specific integrations like Storage facade.
- Can I use spatie/image for async image resizing in Laravel?
- Yes. Chain operations with `save()` to a temporary path, then dispatch a job (e.g., `ImageResizeJob`) to process and store the final image. Use Laravel’s queue system (`@queue` or `dispatch()`) for background processing.
- What’s the difference between GD, Imagick, and VIPS drivers?
- GD is lightweight but lacks advanced features like AVIF/HEIC support. Imagick offers superior performance and format support but requires system dependencies. VIPS is memory-efficient for large images but less common. Choose based on your needs: GD for simplicity, Imagick for power, VIPS for scalability.
- How do I handle image orientation (e.g., EXIF rotation) with this package?
- Use the `orientation()` method to auto-correct rotation based on EXIF data. Ensure the `exif` PHP extension is enabled in your environment. For shared hosting, you may need to manually rotate images or use a fallback like `->rotate(90)` if EXIF isn’t available.
- Can I integrate spatie/image with Laravel’s Storage facade?
- Absolutely. Load images from `Storage::disk('public')->path($file)` and save processed files back to the same or a different disk. Example: `Image::load(storage_path('app/public/'.$file))->resize()->save(storage_path('app/public/thumbs/'.$file)).`
- What’s the best way to validate images before processing?
- Use Laravel’s validation rules like `mimes:jpeg,png,gif` and `max:5000` to check file types and sizes. For advanced checks (e.g., dimensions), use `getimagesize()` or libraries like `spatie/laravel-medialibrary` for model-based validation.
- How do I migrate from Intervention Image to spatie/image?
- Replace `Image::make($path)` with `Image::load($path)` and update method names (e.g., `resize()` → `width($width)->height($height)`). The API is similar but more fluent. Test edge cases like filters (`sepia()` vs. `filter(Filter::Sepia)`) and fallbacks.
- What should I do if Imagick/GD isn’t available in production?
- Configure a fallback driver in your `config/filesystems.php` or use a try-catch block to degrade gracefully. For example, log a warning and serve a placeholder if the primary driver fails. Alternatively, use `->save()` with a lower-quality GD fallback.
- How can I test image manipulations in Laravel?
- Mock the `Image` facade in PHPUnit or use temporary files in `storage/app/test-images`. Compare outputs with `file_get_contents()` or visual regression tools like `php-image-magick-comparison`. Test edge cases like corrupt files, transparency, and extreme dimensions.