Skip to content

Commit

Permalink
more processing options including raw
Browse files Browse the repository at this point in the history
  • Loading branch information
penyaev committed Jun 8, 2021
1 parent 9829080 commit 46f4903
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 4 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ imgproxyurl is a small library to help you build urls for [imgproxy](https://git
This is a WIP.

### Usage
You can set options globally or per-instance. Create a new instance either by calling `imgproxyurl.New` or derive it from an existing one: `u.WithOptions()`.

Many imgproxy options are supported (see "Supported processing options" section below). Nevertheless, there's also a way to set an arbitrary option manually: `imgproxy.Raw{}`
```go
// Some settings can be set globally (you'll be able to override them for specific instances)

Expand Down Expand Up @@ -37,11 +40,12 @@ u2, err := u.WithOptions(
imgproxyurl.Format{"png"},
imgproxyurl.PlainSourceUrl{true},
imgproxyurl.ResizingType{imgproxyurl.ResizingTypeFill},
imgproxyurl.Raw{OptionKey: "raw", Parameters: []interface{}{1, 2, "test"}},
)
if err != nil {
log.Fatalln(err)
}
fmt.Println(u2) // https://example.com/insecure/h:200/rt:fill/w:200/plain/local%3A%2F%2F%2Fo%2Ft%2FotRO1jl3IUVa.jpg@png
fmt.Println(u2) // https://example.com/insecure/h:200/raw:1:2:test/rt:fill/w:200/plain/local%3A%2F%2F%2Fo%2Ft%2FotRO1jl3IUVa.jpg@png


// playing with gravity
Expand Down Expand Up @@ -71,6 +75,8 @@ fmt.Println(u3) // https://example.com/insecure/ex:true:no:100:200/g:fp:0.3:0.4/
```

### Supported processing options
You can find implementations of these processing options in `options.go`

- [resizing type](https://docs.imgproxy.net/#/generating_the_url_advanced?id=resizing-type)
- [resizing algorithm](https://docs.imgproxy.net/#/generating_the_url_advanced?id=resizing-algorithm)
- [width](https://docs.imgproxy.net/#/generating_the_url_advanced?id=width)
Expand All @@ -81,10 +87,16 @@ fmt.Println(u3) // https://example.com/insecure/ex:true:no:100:200/g:fp:0.3:0.4/
- [gravity](https://docs.imgproxy.net/#/generating_the_url_advanced?id=gravity)
- [crop](https://docs.imgproxy.net/#/generating_the_url_advanced?id=crop)
- [padding](https://docs.imgproxy.net/#/generating_the_url_advanced?id=padding)
- [trim](https://docs.imgproxy.net/#/generating_the_url_advanced?id=trim)
- [rotate](https://docs.imgproxy.net/#/generating_the_url_advanced?id=rotate)
- [quality](https://docs.imgproxy.net/#/generating_the_url_advanced?id=quality)
- [max bytes](https://docs.imgproxy.net/#/generating_the_url_advanced?id=max-bytes)
- [background](https://docs.imgproxy.net/#/generating_the_url_advanced?id=background)
- [background alpha](https://docs.imgproxy.net/#/generating_the_url_advanced?id=background-alpha)
- [blur](https://docs.imgproxy.net/#/generating_the_url_advanced?id=blur)
- [sharpen](https://docs.imgproxy.net/#/generating_the_url_advanced?id=sharpen)
- [preset](https://docs.imgproxy.net/#/generating_the_url_advanced?id=preset)
- [auto_rotate](https://docs.imgproxy.net/#/generating_the_url_advanced?id=auto-rotate)
- [filename](https://docs.imgproxy.net/#/generating_the_url_advanced?id=filename)

Not all options are supported at the moment.
88 changes: 85 additions & 3 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,19 @@ func (o Gravity) String() string {
return format(o.Key(), arguments...)
}

//When set, imgproxy will apply the sharpen filter to the resulting image. sigma the size of a mask imgproxy will use.
//When set, imgproxy will apply the sharpen filter to the resulting image
//
//As an approximate guideline, use 0.5 sigma for 4 pixels/mm (display resolution), 1.0 for 12 pixels/mm and 1.5 for 16 pixels/mm (300 dpi == 12 pixels/mm).
type Sharpen struct {
Sharpen float64
//Sigma is the size of a mask imgproxy will use.
Sigma float64
}

func (Sharpen) Key() string {
return "sh"
}
func (o Sharpen) String() string {
return format(o.Key(), o.Sharpen)
return format(o.Key(), o.Sigma)
}

//Redefines quality of the resulting image, percentage. When 0, quality is assumed based on IMGPROXY_QUALITY and IMGPROXY_FORMAT_QUALITY.
Expand Down Expand Up @@ -351,6 +352,87 @@ func (o Presets) String() string {
return format(o.Key(), strings.Join(o.Presets, ":"))
}

//Removes surrounding background.
type Trim struct {
// Color similarity tolerance.
Threshold int
// Hex-coded value of the color that needs to be cut off
Color string
// When set, imgproxy will cut only equal parts from left and right sides. That means that if 10px of background can be cut off from left and 5px from right then 5px will be cut off from both sides. For example, it can be useful if objects on your images are centered but have non-symmetrical shadow.
EqualHor bool
// Acts like EqualHor but for top/bottom sides.
EqualVer bool
}

func (Trim) Key() string {
return "t"
}
func (o Trim) String() string {
return format(o.Key(), o.Threshold, o.Color, o.EqualHor, o.EqualVer)
}

//Rotates the image on the specified angle. The orientation from the image metadata is applied before the rotation unless autorotation is disabled.
type Rotate struct {
//Only 0/90/180/270/etc degrees angles are supported.
Angle int
}

func (Rotate) Key() string {
return "rot"
}
func (o Rotate) String() string {
return format(o.Key(), o.Angle)
}

//When set, imgproxy will apply the gaussian blur filter to the resulting image
type Blur struct {
//Sigma defines the size of a mask imgproxy will use.
Sigma int
}

func (Blur) Key() string {
return "bl"
}
func (o Blur) String() string {
return format(o.Key(), o.Sigma)
}

//When set, imgproxy will automatically rotate images based onon the EXIF Orientation parameter (if available in the image meta data). The orientation tag will be removed from the image anyway. Normally this is controlled by the IMGPROXY_AUTO_ROTATE configuration but this procesing option allows the configuration to be set for each request.
type AutoRotate struct {
AutoRotate bool
}

func (AutoRotate) Key() string {
return "ar"
}
func (o AutoRotate) String() string {
return format(o.Key(), o.AutoRotate)
}

//Defines a filename for Content-Disposition header. When not specified, imgproxy will get filename from the source url.
type Filename struct {
Filename string
}

func (Filename) Key() string {
return "fn"
}
func (o Filename) String() string {
return format(o.Key(), o.Filename)
}

type Raw struct {
OptionKey string
Parameters []interface{}
}

func (o Raw) Key() string {
return o.OptionKey
}
func (o Raw) String() string {
return format(o.Key(), o.Parameters...)
}

type Format struct {
Format string
}
Expand Down
12 changes: 12 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ func TestUrl_applyOptions(t *testing.T) {
"w": "200",
},
}, wantErr: false},
{name: "raw options", fields: fields{
options: map[string]string{
"z": "50",
"h": "100",
},
}, args: args{options: []Option{Raw{OptionKey: "raw", Parameters: []interface{}{1, 2, "test"}}}}, want: &Url{
options: map[string]string{
"z": "50",
"h": "100",
"raw": "1:2:test",
},
}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 46f4903

Please sign in to comment.