Coming from Intervention Image
Introduction
Laravel Go Image covers what intervention/image-laravel and Intervention Image 4 do in a Laravel application. The method names stay close to the PHP ones, but the API follows Go conventions:
- Optional arguments become explicit ones. A width or height of
0stands for PHP'snull, and the zeroimage.Alignmentisimage.Center. - Modifiers return the image and keep the first error, instead of throwing exceptions. See Errors.
- Shapes are structs passed to
Draw, instead of factory callbacks. - The config file's
autoOrientation,decodeAnimation, andbackgroundColoroptions come from environment variables, with configure functions instead of a published config file.
Method names
| Intervention Image (PHP) | Laravel Go Image |
|---|---|
Image::decodePath($path) | image.From(r).Open(path) |
Image::decode($request->file('photo')) | image.From(r).Upload(form.Photo) |
Image::decodeBinary($binary) | image.From(r).Decode(data) |
Image::createImage(800, 600) | image.From(r).Canvas(800, 600) |
$image->scale(width: 300) | img.Scale(300, 0) |
$image->cover(300, 300, 'center') | img.Cover(300, 300, image.Center) |
$image->crop(200, 100, 10, 20) | img.Crop(200, 100, image.TopLeft.Offset(10, 20)) |
$image->insert($logo, 10, 10, 'bottom-right') | img.Insert(logo, image.BottomRight.Offset(10, 10)) |
$image->drawRectangle(fn ($rectangle) => ...) | img.Draw(image.Rectangle{X: 10, Y: 10, ...}) |
$image->text('Hi', 10, 10, fn ($f) => ...) | img.Text("Hi", 10, 10, image.Font{...}) |
$image->colorAt(10, 20) | img.ColorAt(10, 20) |
$image->backgroundColor() | img.Background() |
$image->setBackgroundColor('fff') | img.SetBackground("fff") |
$image->encodeUsingFormat(Format::WEBP, quality: 65) | img.Encode(image.WEBP, image.Quality(65)) |
$image->encodeUsingPath('photo.webp') | img.Encode("photo.webp") |
$image->save('photo.webp') | img.Save("photo.webp") |
response()->image($image, Format::WEBP, quality: 65) | image.Response(img, image.WEBP, image.Quality(65)) |
Behavior differences
The package matches Intervention Image with the GD driver, except in these cases:
- BMP files blend transparency over the background color. GD writes black.
- A rectangle covers exactly its width and height. GD fills one more pixel in each direction.
Cropoffsets on right and bottom alignments move inward, away from the edge, as they do forInsert. Intervention Image adds them to the crop's position.Trimtolerance is a percentage of the largest RGBA distance.- Font sizes are capped at 4096 pixels.
- Polygons that cross themselves fill by the nonzero rule.
- Images may hold up to 2^26 pixels. See Size limits.
Missing features
The package has a single pure Go driver, so these Intervention Image features aren't available:
- The GD, Imagick, and libvips driver choice, and the
driversetting. - AVIF and HEIC, which need C libraries. The manager returns
image.ErrNotSupported. - Writing metadata and color profiles, and the
stripsetting. Go's encoders don't write them. - Resolution, colorspace, and color profile methods.
colorsAt,setExif,setOrigin, custom modifiers and analyzers throughmodifyandanalyze, andFractionsizes.