Use image hooks for customization
masterHooks allow you to run logic before or after an image is saved. You can define hooks using either a Class or a Callback.
Class-based Hooks
Specify a class name in your $imageFields definition. The class must implement a handle($image) method, where $image is an instance of Intervention Image. These are resolved via the Laravel IoC container.
Callback Hooks
Pass an anonymous function directly into the field options.
Hook Types
before_save: Called before the image is written to disk. Changes made to the$imageinstance here will be applied to the final file.after_save: Called after the image is saved to disk. Useful for post-processing like watermarking.
Note: If you have defined width or height in your field options, the $image instance passed to the hook will already be resized.
// Class-based hook
class BlurFilter {
public function handle($image) {
$image->blur(10);
}
}
// Definition in model
protected static $imageFields = [
'avatar' => [
'before_save' => BlurFilter::class,
],
];
// Callback-based hook
$user->setImagesField([
'avatar' => [
'before_save' => function($image) {
$image->blur(10);
},
]
]);