bimg Go Package

repository·master·Indexed 25 days ago

https://github.com/h2non/bimg

A high-performance Go package for image processing using C bindings to libvips. It provides functionality for resizing, rotating, converting image formats, adding watermarks, and batch processing via bimg.Options. Requires libvips 8.8 or higher for installation, with specific version requirements for GIF, PDF, SVG, and AVIF support.

Tokens
1.4K
Snippets
6
Records
7
Agent score
35%

What's inside bimg

  1. Install bimg

    master

    To install the bimg Go package, use the following command:

    go get -u github.com/h2non/bimg

    libvips Prerequisites

    bimg requires libvips to be installed on your system. It is recommended to use version 8.8 or higher.

    Follow the official installation instructions for your platform: https://libvips.github.io/libvips/install.html

    Note on format support:

    • libvips 8.3+ is required for GIF, PDF, and SVG support.
    • libvips 8.9+ is required for AVIF support (requires libheif with an AVIF en-/decoder).
    go get -u github.com/h2non/bimg
  2. Debug bimg and libvips

    master

    To debug issues with bimg or the underlying libvips library:

    1. Enable bimg debugging: Set the DEBUG environment variable.

      DEBUG=bimg ./app
    2. Enable libvips traces: Set the VIPS_TRACE environment variable (this will output significant data to stdout).

      VIPS_TRACE=1 ./app
    3. Force fatal errors: Set the G_DEBUG environment variable to catch fatal warnings or critical errors.

      export G_DEBUG=fatal-warnings,fatal-criticals
  3. Convert image format

    master

    Convert an image to a different format (e.g., PNG, JPEG, WEBP) using bimg.NewImage(buffer).Convert(format).

    buffer, err := bimg.Read("image.jpg")
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    newImage, err := bimg.NewImage(buffer).Convert(bimg.PNG)
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    if bimg.NewImage(newImage).Type() == "png" {
      fmt.Fprintln(os.Stderr, "The image was converted into png")
    }
  4. Resize an image

    master

    You can resize an image using bimg.NewImage(buffer).Resize(width, height). This method preserves the aspect ratio by default. To resize without preserving the aspect ratio, use ForceResize(width, height).

    buffer, err := bimg.Read("image.jpg")
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    newImage, err := bimg.NewImage(buffer).Resize(800, 600)
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    size, err := bimg.NewImage(newImage).Size()
    if size.Width == 800 && size.Height == 600 {
      fmt.Println("The image size is valid")
    }
    
    bimg.Write("new.jpg", newImage)
  5. Add a watermark to an image

    master

    Use the Watermark(watermark) method with a bimg.Watermark struct to add text or image watermarks.

    bimg.Watermark fields:

    • Text (string)
    • Opacity (float64)
    • Width (int)
    • DPI (int)
    • Margin (int)
    • Font (string)
    • Background (bimg.Color)

    bimg.Color is defined as {R, G, B}.

    buffer, err := bimg.Read("image.jpg")
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    watermark := bimg.Watermark{
      Text:       "Chuck Norris (c) 2315",
      Opacity:    0.25,
      Width:      200,
      DPI:        100,
      Margin:     150,
      Font:       "sans bold 12",
      Background: bimg.Color{255, 255, 255},
    }
    
    newImage, err := bimg.NewImage(buffer).Watermark(watermark)
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    bimg.Write("new.jpg", newImage)
  6. Rotate an image

    master

    Rotate an image by a specific degree using bimg.NewImage(buffer).Rotate(angle).

    buffer, err := bimg.Read("image.jpg")
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    newImage, err := bimg.NewImage(buffer).Rotate(90)
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    bimg.Write("new.jpg", newImage)
  7. Apply custom image processing options

    master

    Use the Process(options) method with a bimg.Options struct to perform multiple operations (resize, crop, quality, rotation, etc.) in a single call.

    Available fields in bimg.Options include:

    • Width (int)
    • Height (int)
    • Crop (bool)
    • Quality (int)
    • Rotate (int)
    • Interlace (bool)
    options := bimg.Options{
      Width:        800,
      Height:       600,
      Crop:         true,
      Quality:      95,
      Rotate:       180,
      Interlace:    true,
    }
    
    buffer, err := bimg.Read("image.jpg")
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    newImage, err := bimg.NewImage(buffer).Process(options)
    if err != nil {
      fmt.Fprintln(os.Stderr, err)
    }
    
    bimg.Write("new.jpg", newImage)