mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-05-23 10:55:12 +02:00
restructure transform code for easier testing
This commit is contained in:
parent
2e68caff67
commit
882c2b3782
1 changed files with 23 additions and 13 deletions
36
transform.go
36
transform.go
|
|
@ -27,7 +27,9 @@ import (
|
||||||
// compression quality of resized jpegs
|
// compression quality of resized jpegs
|
||||||
const jpegQuality = 95
|
const jpegQuality = 95
|
||||||
|
|
||||||
// Transform the provided image.
|
// Transform the provided image. img should contain the raw bytes of an
|
||||||
|
// encoded image in one of the supported formats (gif, jpeg, or png). The
|
||||||
|
// bytes of a similarly encoded image is returned.
|
||||||
func Transform(img []byte, opt Options) ([]byte, error) {
|
func Transform(img []byte, opt Options) ([]byte, error) {
|
||||||
if opt == emptyOptions {
|
if opt == emptyOptions {
|
||||||
// bail if no transformation was requested
|
// bail if no transformation was requested
|
||||||
|
|
@ -40,6 +42,25 @@ func Transform(img []byte, opt Options) ([]byte, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m = transformImage(m, opt)
|
||||||
|
|
||||||
|
// encode image
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
switch format {
|
||||||
|
case "gif":
|
||||||
|
gif.Encode(buf, m, nil)
|
||||||
|
case "jpeg":
|
||||||
|
jpeg.Encode(buf, m, &jpeg.Options{Quality: jpegQuality})
|
||||||
|
case "png":
|
||||||
|
png.Encode(buf, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// transformImage modifies the image m based on the transformations specified
|
||||||
|
// in opt.
|
||||||
|
func transformImage(m image.Image, opt Options) image.Image {
|
||||||
// convert percentage width and height values to absolute values
|
// convert percentage width and height values to absolute values
|
||||||
imgW := m.Bounds().Max.X - m.Bounds().Min.X
|
imgW := m.Bounds().Max.X - m.Bounds().Min.X
|
||||||
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
|
imgH := m.Bounds().Max.Y - m.Bounds().Min.Y
|
||||||
|
|
@ -98,16 +119,5 @@ func Transform(img []byte, opt Options) ([]byte, error) {
|
||||||
m = imaging.Rotate270(m)
|
m = imaging.Rotate270(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
// encode image
|
return m
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
switch format {
|
|
||||||
case "gif":
|
|
||||||
gif.Encode(buf, m, nil)
|
|
||||||
case "jpeg":
|
|
||||||
jpeg.Encode(buf, m, &jpeg.Options{Quality: jpegQuality})
|
|
||||||
case "png":
|
|
||||||
png.Encode(buf, m)
|
|
||||||
}
|
|
||||||
|
|
||||||
return buf.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue