diff --git a/data/data.go b/data/data.go index 9c6d609..77fedb2 100644 --- a/data/data.go +++ b/data/data.go @@ -32,6 +32,9 @@ type Options struct { // If true, resize the image to fit in the specified dimensions. Image // will not be cropped, and aspect ratio will be maintained. Fit bool + + // Rotate image the specified degrees counter-clockwise. Valid values are 90, 180, 270. + Rotate int } func (o Options) String() string { @@ -63,6 +66,11 @@ func ParseOptions(str string) *Options { for _, part := range parts[1:] { if part == "fit" { o.Fit = true + continue + } + if len(part) > 2 && strings.HasPrefix(part, "r=") { + o.Rotate, _ = strconv.Atoi(part[2:]) + continue } } diff --git a/readme.md b/readme.md index cac4388..63f3ea6 100644 --- a/readme.md +++ b/readme.md @@ -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` 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 ### 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) 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) +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 ## diff --git a/transform/transform.go b/transform/transform.go index cb01f21..9f40e4d 100644 --- a/transform/transform.go +++ b/transform/transform.go @@ -49,6 +49,7 @@ func Transform(img data.Image, opt *data.Options) (*data.Image, error) { return nil, err } + // convert percentage width and height values to absolute values var h, w int if opt.Width > 0 && opt.Width < 1 { 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 buf := new(bytes.Buffer) switch format {