mirror of
https://github.com/willnorris/imageproxy.git
synced 2024-12-30 22:34:18 -05:00
add support for image rotation
This commit is contained in:
parent
ea76f2d0d8
commit
d506fc6881
3 changed files with 29 additions and 0 deletions
|
@ -32,6 +32,9 @@ type Options struct {
|
||||||
// If true, resize the image to fit in the specified dimensions. Image
|
// If true, resize the image to fit in the specified dimensions. Image
|
||||||
// will not be cropped, and aspect ratio will be maintained.
|
// will not be cropped, and aspect ratio will be maintained.
|
||||||
Fit bool
|
Fit bool
|
||||||
|
|
||||||
|
// Rotate image the specified degrees counter-clockwise. Valid values are 90, 180, 270.
|
||||||
|
Rotate int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o Options) String() string {
|
func (o Options) String() string {
|
||||||
|
@ -63,6 +66,11 @@ func ParseOptions(str string) *Options {
|
||||||
for _, part := range parts[1:] {
|
for _, part := range parts[1:] {
|
||||||
if part == "fit" {
|
if part == "fit" {
|
||||||
o.Fit = true
|
o.Fit = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if len(part) > 2 && strings.HasPrefix(part, "r=") {
|
||||||
|
o.Rotate, _ = strconv.Atoi(part[2:])
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,6 +59,12 @@ always, the original aspect ratio will be preserved. Specifying the `fit`
|
||||||
option with only one of either width or height does the same thing as if `fit`
|
option with only one of either width or height does the same thing as if `fit`
|
||||||
had not been specified.
|
had not been specified.
|
||||||
|
|
||||||
|
#### Rotate ####
|
||||||
|
|
||||||
|
The `r={degrees}` option will rotate the image the specified number of degrees,
|
||||||
|
counter-clockwise. Valid degrees values are `90`, `180`, and `270`. Images
|
||||||
|
are rotated **after** being resized.
|
||||||
|
|
||||||
### Remote URL ###
|
### Remote URL ###
|
||||||
|
|
||||||
The URL of the original image to load is specified as the remainder of the
|
The URL of the original image to load is specified as the remainder of the
|
||||||
|
@ -85,6 +91,7 @@ x100 | 100px tall, proportional width | ![x100](https://s.wjn.me/x1
|
||||||
100x150 | 100 by 150 pixels, cropping as needed | ![100x150](https://s.wjn.me/100x150/https://willnorris.com/content/uploads/2013/12/small-things.jpg)
|
100x150 | 100 by 150 pixels, cropping as needed | ![100x150](https://s.wjn.me/100x150/https://willnorris.com/content/uploads/2013/12/small-things.jpg)
|
||||||
100 | 100px square, cropping as needed | ![100](https://s.wjn.me/100/https://willnorris.com/content/uploads/2013/12/small-things.jpg)
|
100 | 100px square, cropping as needed | ![100](https://s.wjn.me/100/https://willnorris.com/content/uploads/2013/12/small-things.jpg)
|
||||||
150,fit | fit to be no more than 150 by 150 pixels | ![150,fit](https://s.wjn.me/150,fit/https://willnorris.com/content/uploads/2013/12/small-things.jpg)
|
150,fit | fit to be no more than 150 by 150 pixels | ![150,fit](https://s.wjn.me/150,fit/https://willnorris.com/content/uploads/2013/12/small-things.jpg)
|
||||||
|
100,r=90| 100px square, rotated 90 degrees | ![100,r=90](https://s.wjn.me/100,r=90/https://willnorris.com/content/uploads/2013/12/small-things.jpg)
|
||||||
|
|
||||||
|
|
||||||
## License ##
|
## License ##
|
||||||
|
|
|
@ -49,6 +49,7 @@ func Transform(img data.Image, opt *data.Options) (*data.Image, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// convert percentage width and height values to absolute values
|
||||||
var h, w int
|
var h, w int
|
||||||
if opt.Width > 0 && opt.Width < 1 {
|
if opt.Width > 0 && opt.Width < 1 {
|
||||||
w = int(float64(m.Bounds().Max.X-m.Bounds().Min.X) * opt.Width)
|
w = int(float64(m.Bounds().Max.X-m.Bounds().Min.X) * opt.Width)
|
||||||
|
@ -72,6 +73,19 @@ func Transform(img data.Image, opt *data.Options) (*data.Image, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// rotate
|
||||||
|
switch opt.Rotate {
|
||||||
|
case 90:
|
||||||
|
m = imaging.Rotate90(m)
|
||||||
|
break
|
||||||
|
case 180:
|
||||||
|
m = imaging.Rotate180(m)
|
||||||
|
break
|
||||||
|
case 270:
|
||||||
|
m = imaging.Rotate270(m)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
// encode image
|
// encode image
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
switch format {
|
switch format {
|
||||||
|
|
Loading…
Reference in a new issue