|
|
|
|
@ -1,16 +1,16 @@
|
|
|
|
|
package bimg
|
|
|
|
|
|
|
|
|
|
// Image encapsulates the whole image buffer
|
|
|
|
|
// Image provides a simple method DSL to transform a given image as byte buffer.
|
|
|
|
|
type Image struct {
|
|
|
|
|
buffer []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Creates a new image
|
|
|
|
|
// NewImage creates a new Image struct with method DSL.
|
|
|
|
|
func NewImage(buf []byte) *Image {
|
|
|
|
|
return &Image{buf}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resize the image to fixed width and height
|
|
|
|
|
// Resize resizes the image to fixed width and height.
|
|
|
|
|
func (i *Image) Resize(width, height int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: width,
|
|
|
|
|
@ -20,7 +20,7 @@ func (i *Image) Resize(width, height int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Force resize with custom size (aspect ratio won't be maintained)
|
|
|
|
|
// ForceResize resizes with custom size (aspect ratio won't be maintained).
|
|
|
|
|
func (i *Image) ForceResize(width, height int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: width,
|
|
|
|
|
@ -30,7 +30,7 @@ func (i *Image) ForceResize(width, height int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Resize the image to fixed width and height with additional crop transformation
|
|
|
|
|
// ResizeAndCrop resizes the image to fixed width and height with additional crop transformation.
|
|
|
|
|
func (i *Image) ResizeAndCrop(width, height int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: width,
|
|
|
|
|
@ -41,7 +41,7 @@ func (i *Image) ResizeAndCrop(width, height int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract area from the by X/Y axis
|
|
|
|
|
// Extract area from the by X/Y axis in the current image.
|
|
|
|
|
func (i *Image) Extract(top, left, width, height int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Top: top,
|
|
|
|
|
@ -57,7 +57,7 @@ func (i *Image) Extract(top, left, width, height int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enlarge the image by width and height. Aspect ratio is maintained
|
|
|
|
|
// Enlarge enlarges the image by width and height. Aspect ratio is maintained.
|
|
|
|
|
func (i *Image) Enlarge(width, height int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: width,
|
|
|
|
|
@ -67,7 +67,7 @@ func (i *Image) Enlarge(width, height int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enlarge the image by width and height with additional crop transformation
|
|
|
|
|
// EnlargeAndCrop enlarges the image by width and height with additional crop transformation.
|
|
|
|
|
func (i *Image) EnlargeAndCrop(width, height int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: width,
|
|
|
|
|
@ -78,7 +78,7 @@ func (i *Image) EnlargeAndCrop(width, height int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crop the image to the exact size specified
|
|
|
|
|
// Crop crops the image to the exact size specified.
|
|
|
|
|
func (i *Image) Crop(width, height int, gravity Gravity) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: width,
|
|
|
|
|
@ -89,7 +89,7 @@ func (i *Image) Crop(width, height int, gravity Gravity) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crop an image by width (auto height)
|
|
|
|
|
// CropByWidth crops an image by width only param (auto height).
|
|
|
|
|
func (i *Image) CropByWidth(width int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: width,
|
|
|
|
|
@ -98,7 +98,7 @@ func (i *Image) CropByWidth(width int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crop an image by height (auto width)
|
|
|
|
|
// CropByHeight crops an image by height (auto width).
|
|
|
|
|
func (i *Image) CropByHeight(height int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Height: height,
|
|
|
|
|
@ -107,7 +107,7 @@ func (i *Image) CropByHeight(height int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Thumbnail the image by the a given width by aspect ratio 4:4
|
|
|
|
|
// Thumbnail creates a thumbnail of the image by the a given width by aspect ratio 4:4.
|
|
|
|
|
func (i *Image) Thumbnail(pixels int) ([]byte, error) {
|
|
|
|
|
options := Options{
|
|
|
|
|
Width: pixels,
|
|
|
|
|
@ -118,50 +118,52 @@ func (i *Image) Thumbnail(pixels int) ([]byte, error) {
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add text as watermark on the given image
|
|
|
|
|
// Watermark adds text as watermark on the given image.
|
|
|
|
|
func (i *Image) Watermark(w Watermark) ([]byte, error) {
|
|
|
|
|
options := Options{Watermark: w}
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Zoom the image by the given factor.
|
|
|
|
|
// You should probably call Extract() before
|
|
|
|
|
// Zoom zooms the image by the given factor.
|
|
|
|
|
// You should probably call Extract() before.
|
|
|
|
|
func (i *Image) Zoom(factor int) ([]byte, error) {
|
|
|
|
|
options := Options{Zoom: factor}
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rotate the image by given angle degrees (0, 90, 180 or 270)
|
|
|
|
|
// Rotate rotates the image by given angle degrees (0, 90, 180 or 270).
|
|
|
|
|
func (i *Image) Rotate(a Angle) ([]byte, error) {
|
|
|
|
|
options := Options{Rotate: a}
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Flip the image about the vertical Y axis
|
|
|
|
|
// Flip flips the image about the vertical Y axis.
|
|
|
|
|
func (i *Image) Flip() ([]byte, error) {
|
|
|
|
|
options := Options{Flip: true}
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Flop the image about the horizontal X axis
|
|
|
|
|
// Flop flops the image about the horizontal X axis.
|
|
|
|
|
func (i *Image) Flop() ([]byte, error) {
|
|
|
|
|
options := Options{Flop: true}
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Convert image to another format
|
|
|
|
|
// Convert converts image to another format.
|
|
|
|
|
func (i *Image) Convert(t ImageType) ([]byte, error) {
|
|
|
|
|
options := Options{Type: t}
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Colour space conversion
|
|
|
|
|
// Colourspace performs a color space conversion bsaed on the given interpretation.
|
|
|
|
|
func (i *Image) Colourspace(c Interpretation) ([]byte, error) {
|
|
|
|
|
options := Options{Interpretation: c}
|
|
|
|
|
return i.Process(options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Transform the image by custom options
|
|
|
|
|
// Process processes the image based on the given transformation options,
|
|
|
|
|
// talking with libvips bindings accordingly and returning the resultant
|
|
|
|
|
// image buffer.
|
|
|
|
|
func (i *Image) Process(o Options) ([]byte, error) {
|
|
|
|
|
image, err := Resize(i.buffer, o)
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -171,33 +173,34 @@ func (i *Image) Process(o Options) ([]byte, error) {
|
|
|
|
|
return image, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get image metadata (size, alpha channel, profile, EXIF rotation)
|
|
|
|
|
// Metadata returns the image metadata (size, alpha channel, profile, EXIF rotation).
|
|
|
|
|
func (i *Image) Metadata() (ImageMetadata, error) {
|
|
|
|
|
return Metadata(i.buffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the image interpretation type
|
|
|
|
|
// Interpretation gets the image interpretation type.
|
|
|
|
|
// See: http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/VipsImage.html#VipsInterpretation
|
|
|
|
|
func (i *Image) Interpretation() (Interpretation, error) {
|
|
|
|
|
return ImageInterpretation(i.buffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the current image has a valid colourspace
|
|
|
|
|
// ColourspaceIsSupported checks if the current image
|
|
|
|
|
// color space is supported.
|
|
|
|
|
func (i *Image) ColourspaceIsSupported() (bool, error) {
|
|
|
|
|
return ColourspaceIsSupported(i.buffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get image type format (jpeg, png, webp, tiff)
|
|
|
|
|
// Type returns the image type format (jpeg, png, webp, tiff).
|
|
|
|
|
func (i *Image) Type() string {
|
|
|
|
|
return DetermineImageTypeName(i.buffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get image size
|
|
|
|
|
// Size returns the image size as form of width and height pixels.
|
|
|
|
|
func (i *Image) Size() (ImageSize, error) {
|
|
|
|
|
return Size(i.buffer)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get image buffer
|
|
|
|
|
// Image returns the current resultant image image buffer.
|
|
|
|
|
func (i *Image) Image() []byte {
|
|
|
|
|
return i.buffer
|
|
|
|
|
}
|
|
|
|
|
|