Skip to content

Commit

Permalink
api update
Browse files Browse the repository at this point in the history
  • Loading branch information
penyaev committed May 22, 2021
1 parent 82263bd commit 66f9cb2
Show file tree
Hide file tree
Showing 6 changed files with 808 additions and 471 deletions.
87 changes: 61 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,68 @@ imgproxyurl is a small library to help you build urls for [imgproxy](https://git
This is a WIP.

### Usage
Specify the key and the salt, set the width, height and the resizing type, and get the absolute resulting url:
```go
url, err := imgproxyurl.
New("/path/to/image.jpg").
SetKey("e99bd6542067de7dac460558ecada3987dd2d18b066180eaa1c3abc66fb22e463d177ac8f64c93c44d0d78c35adcdda7e0b5f5a116b23ac3d1fa7a305d0727c4").
SetSalt("a997d51b78d28ba8c05f39b6e634a044b9551352b105f70a4c0fc4c0eca5982719a33527d0253810273bf4d8b747a261cd4898d3e46916cc57d1de8aac132870").
SetWidth(400).
SetHeight(300).
SetResizingType(imgproxyurl.ResizingTypeFit).
GetAbsolute("http://localhost:8080")

// url = "http://localhost:8080/a3eK6TO-pMwXvXtakEZjTov3qDrUoDeGL1Xb_1p-Ue4/w:400/h:300/rt:fit/L3BhdGgvdG8vaW1hZ2UuanBn"
```
// Some settings can be set globally (you'll be able to override them for specific instances)

Load key and salt from environment variables `IMGPROXY_KEY` and `IMGPROXY_SALT`; specify a `png` resulting format, specify gravity with floating-point offsets, set quality to 60 and get a relative url:
```go
url, err := imgproxyurl.
NewFromEnvironment("/path/to/image.jpg").
SetExtension("png").
SetGravity(imgproxyurl.GravityTypeFocusPoint, imgproxyurl.GravityFloatOffsets{
X: 0.1,
Y: 0.5,
}).
SetQuality(60).
Get()

// url = "/jPfU6erScy_cRLYP_pSnwGQ7cGpZCrtuWUSiAWy69mY/g:fp:0.100:0.500/q:60/L3BhdGgvdG8vaW1hZ2UuanBn.png"
// SetKeySalt can return an error in case key/salt is not in hex format
if err := imgproxyurl.SetKeySalt(
"e99bd6542067de7dac460558ecada3987dd2d18b066180eaa1c3abc66fb22e463d177ac8f64c93c44d0d78c35adcdda7e0b5f5a116b23ac3d1fa7a305d0727c4",
"a997d51b78d28ba8c05f39b6e634a044b9551352b105f70a4c0fc4c0eca5982719a33527d0253810273bf4d8b747a261cd4898d3e46916cc57d1de8aac132870",
); err != nil {
log.Fatalln(err)
}
imgproxyurl.SetEndpoint("https://example.com/")

// create a url
u, err := imgproxyurl.New(
"local:///o/t/otRO1jl3IUVa.jpg",
imgproxyurl.Width{200},
imgproxyurl.Height{200},
)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%+v\n", u) // https://example.com/vBTOFF_QqWqQPVCdQdjiTac8sn7EEVIh3c1UidkcvAM/h:200/w:200/bG9jYWw6Ly8vby90L290Uk8xamwzSVVWYS5qcGc


// create a copy with some options changed
u2, err := u.WithOptions(
imgproxyurl.KeyRaw{nil},
imgproxyurl.SaltRaw{nil},
imgproxyurl.Format{"png"},
imgproxyurl.PlainSourceUrl{true},
imgproxyurl.ResizingType{imgproxyurl.ResizingTypeFill},
)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%+v\n", u2) // https://example.com/insecure/h:200/rt:fill/w:200/plain/local%3A%2F%2F%2Fo%2Ft%2FotRO1jl3IUVa.jpg@png


// playing with gravity
u3, err := u2.WithOptions(
imgproxyurl.Gravity{
Type: imgproxyurl.GravityTypeFocusPoint,
Offsets: imgproxyurl.GravityFloatOffsets{
X: 0.3,
Y: 0.4,
},
},
imgproxyurl.Extend{
Extend: true,
Gravity: &imgproxyurl.Gravity{
Type: imgproxyurl.GravityTypeNorth,
Offsets: imgproxyurl.GravityIntegerOffsets{
X: 100,
Y: 200,
},
},
},
)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%+v\n", u3) // https://example.com/insecure/ex:true:no:100:200/g:fp:0.3:0.4/h:200/rt:fill/w:200/plain/local%3A%2F%2F%2Fo%2Ft%2FotRO1jl3IUVa.jpg@png
```

### Supported processing options
Expand All @@ -45,10 +80,10 @@ url, err := imgproxyurl.
- [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)
- [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)
- [preset](https://docs.imgproxy.net/#/generating_the_url_advanced?id=preset)

Not all options are supported at the moment.
Loading

0 comments on commit 66f9cb2

Please sign in to comment.