imgproxy

package module
v0.0.0-...-74d54ee Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 10 Imported by: 0

README

imgproxy-go

NOTE: A clone of the unitedwardrobe library to test the base64 URL-safe encoding method (base64.RawURLEncoding). The current version failed with encoding the path "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQ2F3M1nb8b_wuN94uxrdJ88eacQtp3d8KEB1OZ68DA13Z9iQzKSaif84Y&s" as example.

A Go client library to generate urls for imgproxy services.

Based on https://docs.imgproxy.net/

Note: This library is not feature complete.

Usage

  package main

  import (
    "fmt"

    "github.com/unitedwardrobe/imgproxy-go"
  )

  func main() {
    ip, err := imgproxy.NewImgproxy(imgproxy.Config{
      BaseURL:       "http://localhost",
      SignatureSize: 15,
      Key:           hex.EncodeToString([]byte("key")),
      Salt:          hex.EncodeToString([]byte("salt")),
      EncodePath:    false,
    })
    if err != nil {
      panic(err)
    }

    url, err := ip.
      Builder().
      Resize(imgproxy.ResizingTypeFill, 123, 456, true, false).
      DPR(10).
      Format("png").
      Generate("path/to/my/image.jpg")
    if err != nil {
      panic(err)
    }

    fmt.Println(url) // http://localhost/448bHumukUmn0qpKBY2z/dpr:10/f:png/rs:fill:123:456:1:0/plain/path/to/my/image.jpg
  }

Tests

  $ go test

License

This project is licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	// Resizes the image while keeping aspect ratio to fit a given size.
	ResizingTypeFit = ResizingType("fit")

	// Resizes the image while keeping aspect ratio to fill a given size and crops projecting parts.
	ResizingTypeFill = ResizingType("fill")

	// The same as fill, but if the resized image is smaller than the requested size, imgproxy will crop the result to keep the requested aspect ratio.
	ResizingTypeFillDown = ResizingType("fill-down")

	// Resizes the image without keeping the aspect ratio.
	ResizingTypeForce = ResizingType("force")

	// If both source and resulting dimensions have the same orientation (portrait or landscape), imgproxy will use fill. Otherwise, it will use fit.
	ResizingTypeAuto = ResizingType("auto")
)

ResizingType enum.

View Source
const (
	// Default gravity position.
	GravityEnumCenter = GravityEnum("ce")
	// Top edge.
	GravityEnumNorth = GravityEnum("no")
	// Bottom edge.
	GravityEnumSouth = GravityEnum("so")
	// Right edge.
	GravityEnumEast = GravityEnum("ea")
	// Left edge.
	GravityEnumWest = GravityEnum("we")
	// Top-right corner.
	GravityEnumNorthEast = GravityEnum("noea")
	// Top-left corner.
	GravityEnumNorthWest = GravityEnum("nowe")
	// Bottom-right corner.
	GravityEnumSouthEast = GravityEnum("soea")
	// Bottom-left corner.
	GravityEnumSouthWest = GravityEnum("sowe")
	// Libvips detects the most "interesting" section of the image and considers it as the center of the resulting image.
	GravityEnumSmart = GravityEnum("sm")
)

GravityEnum constants.

View Source
const (
	WatermarkPositionCenter = WatermarkPosition("ce")

	WatermarkPositionNorth = WatermarkPosition("no")

	WatermarkPositionSouth = WatermarkPosition("so")

	WatermarkPositionEast = WatermarkPosition("ea")

	WatermarkPositionWest = WatermarkPosition("we")

	WatermarkPositionNorthEast = WatermarkPosition("noea")

	WatermarkPositionNorthWest = WatermarkPosition("nowe")

	WatermarkPositionSouthEast = WatermarkPosition("soea")

	WatermarkPositionSouthWest = WatermarkPosition("sowe")

	WatermarkPositionReplicate = WatermarkPosition("re")
)

WatermarkPosition constants.

Variables

View Source
var ErrInvalidSignature = stdErrs.New("invalid signature size")

ErrInvalidSignature error.

Functions

This section is empty.

Types

type BackgroundSetter

type BackgroundSetter interface {
	SetBgOption(*ImgproxyURLData) *ImgproxyURLData
}

BackgroundSetter interface to set the background option.

type Config

type Config struct {
	BaseURL       string
	SignatureSize int
	Key           string
	Salt          string
	EncodePath    bool
}

Config holds the parameters for constructing an imgproxy URL builder.

type FocusPoint

type FocusPoint struct {
	X int64
	Y int64
}

FocusPoint holds the coordinates of the focus point.

func (FocusPoint) GetStringOption

func (f FocusPoint) GetStringOption() string

GetStringOption gets the focus point value as string.

func (FocusPoint) SetGravityOption

func (f FocusPoint) SetGravityOption(i *ImgproxyURLData) *ImgproxyURLData

SetGravityOption sets gravity option.

type GravityEnum

type GravityEnum string

GravityEnum holds a gravity option value.

func (GravityEnum) GetStringOption

func (g GravityEnum) GetStringOption() string

GetStringOption gets the gravity value as string.

func (GravityEnum) SetGravityOption

func (g GravityEnum) SetGravityOption(i *ImgproxyURLData) *ImgproxyURLData

SetGravityOption sets the gravity option.

type GravitySetter

type GravitySetter interface {
	SetGravityOption(i *ImgproxyURLData) *ImgproxyURLData
	GetStringOption() string
}

GravitySetter interface to set and get a gravity option.

type HexColor

type HexColor string

HexColor holds an hexadecimal format color.

func (HexColor) SetBgOption

func (h HexColor) SetBgOption(i *ImgproxyURLData) *ImgproxyURLData

SetBgOption sets the background option.

type Imgproxy

type Imgproxy struct {
	// contains filtered or unexported fields
}

Imgproxy is a URL builder helper for imgproxy.

func NewImgproxy

func NewImgproxy(cfg Config) (*Imgproxy, error)

NewImgproxy returns a new *Imgproxy.

func (*Imgproxy) Builder

func (i *Imgproxy) Builder() *ImgproxyURLData

Builder returns a *ImgproxyURLData that can be used to construct an imgproxy URL.

type ImgproxyURLData

type ImgproxyURLData struct {
	*Imgproxy
	Options map[string]string
}

ImgproxyURLData is a struct that contains the data required for generating an imgproxy URL.

func (*ImgproxyURLData) Background

Background fills the resulting image background with the specified color. RGBColor are the red, green and blue channel values of the background color (0-255). HexColor is a hex-coded value of the color. Useful when you convert an image with alpha-channel to JPEG.

func (*ImgproxyURLData) Blur

func (i *ImgproxyURLData) Blur(sigma int) *ImgproxyURLData

Blur applies a gaussian blur filter to the resulting image. The value of sigma defines the size of the mask imgproxy will use.

func (*ImgproxyURLData) CacheBuster

func (i *ImgproxyURLData) CacheBuster(buster string) *ImgproxyURLData

CacheBuster doesn’t affect image processing but its changing allows for bypassing the CDN, proxy server and browser cache. Useful when you have changed some things that are not reflected in the URL, like image quality settings, presets, or watermark data. It’s highly recommended to prefer the cachebuster option over a URL query string because that option can be properly signed.

func (*ImgproxyURLData) Crop

func (i *ImgproxyURLData) Crop(width int, height int, gravity GravitySetter) *ImgproxyURLData

Crop sets the crop option.

func (*ImgproxyURLData) DPR

func (i *ImgproxyURLData) DPR(dpr int) *ImgproxyURLData

DPR controls the output density of your image.

func (*ImgproxyURLData) Enlarge

func (i *ImgproxyURLData) Enlarge(enlarge int) *ImgproxyURLData

Enlarge enlarges the image.

func (*ImgproxyURLData) Format

func (i *ImgproxyURLData) Format(extension string) *ImgproxyURLData

Format specifies the resulting image format. Alias for the extension part of the URL.

func (*ImgproxyURLData) Generate

func (i *ImgproxyURLData) Generate(uri string) (string, error)

Generate generates the imgproxy URL.

func (*ImgproxyURLData) Gravity

Gravity guides imgproxy when needs to cut some parts of the image.

func (*ImgproxyURLData) Height

func (i *ImgproxyURLData) Height(height int) *ImgproxyURLData

Height defines the height of the resulting image. When set to 0, imgproxy will calculate resulting height using the defined width and source aspect ratio. When set to 0 and resizing type is force, imgproxy will keep the original height.

func (*ImgproxyURLData) Preset

func (i *ImgproxyURLData) Preset(presets ...string) *ImgproxyURLData

Preset defines a list of presets to be used by imgproxy.

func (*ImgproxyURLData) Quality

func (i *ImgproxyURLData) Quality(quality int) *ImgproxyURLData

Quality redefines quality of the resulting image, as a percentage.

func (*ImgproxyURLData) Resize

func (i *ImgproxyURLData) Resize(
	resizingType ResizingType, width int, height int, enlarge bool, extend bool,
) *ImgproxyURLData

Resize resizes the image.

func (*ImgproxyURLData) ResizingType

func (i *ImgproxyURLData) ResizingType(resizingType ResizingType) *ImgproxyURLData

ResizingType sets the resizing type.

func (*ImgproxyURLData) SetOption

func (i *ImgproxyURLData) SetOption(key, value string) *ImgproxyURLData

SetOption sets an option on the URL.

func (*ImgproxyURLData) Sharpen

func (i *ImgproxyURLData) Sharpen(sigma int) *ImgproxyURLData

Sharpen applies the sharpen filter to the resulting image. The value of sigma defines the size of the mask imgproxy will use.

func (*ImgproxyURLData) Size

func (i *ImgproxyURLData) Size(width int, height int, enlarge bool) *ImgproxyURLData

Size sets size option.

func (*ImgproxyURLData) Watermark

func (i *ImgproxyURLData) Watermark(
	opacity int, position WatermarkPosition, offset *WatermarkOffset, scale int,
) *ImgproxyURLData

Watermark places a watermark on the processed image.

func (*ImgproxyURLData) Width

func (i *ImgproxyURLData) Width(width int) *ImgproxyURLData

Width defines the width of the resulting image. When set to 0, imgproxy will calculate width using the defined height and source aspect ratio. When set to 0 and resizing type is force, imgproxy will keep the original width.

type OffsetGravity

type OffsetGravity struct {
	Type    GravityEnum
	XOffset int
	YOffset int
}

OffsetGravity holds a gravity type and offsets coordinates.

func (OffsetGravity) GetStringOption

func (o OffsetGravity) GetStringOption() string

GetStringOption gets the gravity offset value as string.

func (OffsetGravity) SetGravityOption

func (o OffsetGravity) SetGravityOption(i *ImgproxyURLData) *ImgproxyURLData

SetGravityOption sets the gravity option.

type RGBColor

type RGBColor struct {
	R int
	G int
	B int
}

RGBColor holds an RGB color.

func (RGBColor) SetBgOption

func (rgb RGBColor) SetBgOption(i *ImgproxyURLData) *ImgproxyURLData

SetBgOption sets the background option.

type ResizingType

type ResizingType string

ResizingType enum.

type WatermarkOffset

type WatermarkOffset struct {
	X int
	Y int
}

WatermarkOffset holds the watermark coordinates.

type WatermarkPosition

type WatermarkPosition string

WatermarkPosition holds a watermark position option.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL