Imagic is a Laravel Nova field package that allows for image manipulation capabilities, such as cropping, resizing, quality adjustment, and WebP conversion. It utilizes the powerful Intervention Image class for image manipulation. The purpose of this package is to optimize images for web usage by converting them to the WebP format, which provides superior compression and faster load times.
Advanced Image Manipulation Made Easy with Images Magic
✅ Single/Multiple Uploads
✅ Cropping
✅ Resizing
✅ Fitting
✅ Quality Control
✅ WebP Conversion
✅ Watermarking
✅ Custom Directories
- PHP (^7.1 or higher)
- Laravel Nova (^4.0 or higher)
composer require ayvazyan10/nova-imagic
Here is an example of how to use Imagic in your Laravel Nova application: In your Laravel Nova resources, use the Imagic field:
use Ayvazyan10\Imagic\Imagic;
public function fields(Request $request)
{
return [
// ...
Imagic::make('Image', 'image'),
// ...
];
}
Imagic::make('Image')
->multiple()
->crop($width, $height, $left = 0, $top = 0)
->resize($width, $height)
->fit($width, $height)
->widen($width)
->quality($quality)
->disk($path)
->directory($path)
->convert($convert = true)
->watermark($path, $position = 'bottom-right', $x = 0, $y = 0);
Below are some examples in different scenarios.
To enable multiple image uploads, use the multiple() method. Note that when you use the multiple() method, your database column should be of type text, longtext, or json to store all images in a JSON format. Additionally, you will have the ability to sort uploaded images by drag and drop.
Imagic::make('Images')->multiple(),
To crop images, use the crop() method:
- x (optional) X-Coordinate of the top-left corner if the rectangular cutout. By default the rectangular part will be centered on the current image.
- y (optional) Y-Coordinate of the top-left corner if the rectangular cutout. By default the rectangular part will be centered on the current image.
Imagic::make('Image')->crop($width, $height, $x, $y),
To resize images, use the resize() method:
Imagic::make('Image')->resize($width = int|null, $height = int|null),
Specify the desired (only - width) for image resizing.
- The height will be automatically adjusted to maintain the aspect ratio.
Imagic::make('Image')->widen($width = int),
To adjust the image quality, use the quality() method: default is 90
Imagic::make('Image')->quality(90),
To convert images to WebP format, use the convert() method:
By default, the images will be converted to WebP format. To disable conversion, pass false to the convert() method:
Imagic::make('Image')->convert(false),
You can use the fit() method when defining the Imagic field in your Nova resource:
Imagic::make('Image')->fit($width, $height),
Replace the /path/to/watermark.png with the actual path to your watermark image.
This will add the watermark to the image with the specified path, position, and offset (15x15 pixels from the bottom-right corner in this example).
Remember to import the Imagic class at the top of your Nova resource file:
Imagic::make('Image')->watermark('/path/to/watermark.png', 'bottom-right', 15, 15),
Here is an example of how to use it:
Imagic::make('Image')->disk('public')
Caution: The disk is not working with custom directories and will throw an error.
BY DEFAULT - Imagic uses this structure: /storage/imagic/year/month/day/image_name.webp
The Imagic class includes a directory() method that allows you to specify a custom directory path for your image uploads. This allows for more flexibility in managing the location of your image files.
To use this feature, call the directory() method when creating an Imagic field and provide it with your custom directory path as an argument. This path should be a string, and should not start or end with a /.
Here is an example of how to use it:
Imagic::make('Image')->directory('your/custom/directory')
In this example, any images uploaded through this field will be saved in your/custom/directory.
Caution: The provided directory path should not start or end with a /. If it does, an InvalidArgumentException will be thrown. Make sure your directory path is correctly formatted when using this feature.
For example, the following code would throw an exception:
// This will throw an exception because the directory path starts with a '/'
// Directory structure should not start or end with a slash. Only in the middle.
Imagic::make('Image')
->directory('/invalid/directory/path')
Please see contributing.md for details and a todolist.
If you discover any security related issues, please email ayvazyan403@gmail.com instead of using the issue tracker.
MIT. Please see the license file for more information.