2020-09-29 16:09:42 +00:00
|
|
|
package images
|
|
|
|
|
|
|
|
import (
|
2020-10-01 16:47:34 +00:00
|
|
|
"errors"
|
2020-09-29 16:09:42 +00:00
|
|
|
"image"
|
2020-10-01 16:47:34 +00:00
|
|
|
"image/gif"
|
2020-09-29 16:09:42 +00:00
|
|
|
"image/jpeg"
|
2020-10-01 16:47:34 +00:00
|
|
|
"image/png"
|
2020-09-30 22:33:47 +00:00
|
|
|
"io"
|
2020-09-29 16:09:42 +00:00
|
|
|
"os"
|
2020-10-01 16:47:34 +00:00
|
|
|
|
|
|
|
"golang.org/x/image/webp"
|
2020-09-29 16:09:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Get(fileName string) (image.Image, error) {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-30 22:33:47 +00:00
|
|
|
defer file.Close()
|
|
|
|
|
2020-10-08 13:59:03 +00:00
|
|
|
fb, err := prepareFileForDecode(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeImageData(fb, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareFileForDecode(file *os.File) ([]byte, error) {
|
|
|
|
// Read the first 14 bytes, used for performing image type checks before parsing the image data
|
|
|
|
fb := make([]byte, 14)
|
|
|
|
_, err := file.Read(fb)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the read cursor
|
|
|
|
_, err = file.Seek(0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-01 16:47:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 13:59:03 +00:00
|
|
|
return fb, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeImageData(buf []byte, r io.Reader) (img image.Image, err error) {
|
|
|
|
switch GetFileType(buf) {
|
2020-09-30 22:33:47 +00:00
|
|
|
case JPEG:
|
2020-10-08 13:59:03 +00:00
|
|
|
img, err = jpeg.Decode(r)
|
2020-09-30 22:33:47 +00:00
|
|
|
case PNG:
|
2020-10-08 13:59:03 +00:00
|
|
|
img, err = png.Decode(r)
|
2020-10-01 16:47:34 +00:00
|
|
|
case GIF:
|
2020-10-08 13:59:03 +00:00
|
|
|
img, err = gif.Decode(r)
|
2020-09-30 22:33:47 +00:00
|
|
|
case WEBP:
|
2020-10-08 13:59:03 +00:00
|
|
|
img, err = webp.Decode(r)
|
|
|
|
case UNKNOWN:
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
return nil, errors.New("unsupported file type")
|
2020-09-30 22:33:47 +00:00
|
|
|
}
|
2020-09-29 16:09:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2020-10-01 16:47:34 +00:00
|
|
|
func GetFileType(buf []byte) FileType {
|
|
|
|
switch {
|
|
|
|
case isJpeg(buf):
|
|
|
|
return JPEG
|
|
|
|
case isPng(buf):
|
|
|
|
return PNG
|
|
|
|
case isGif(buf):
|
|
|
|
return GIF
|
|
|
|
case isWebp(buf):
|
|
|
|
return WEBP
|
|
|
|
default:
|
|
|
|
return UNKNOWN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func isJpeg(buf []byte) bool {
|
|
|
|
return len(buf) > 2 &&
|
|
|
|
buf[0] == 0xFF &&
|
|
|
|
buf[1] == 0xD8 &&
|
|
|
|
buf[2] == 0xFF
|
|
|
|
}
|
|
|
|
|
|
|
|
func isPng(buf []byte) bool {
|
|
|
|
return len(buf) > 3 &&
|
|
|
|
buf[0] == 0x89 && buf[1] == 0x50 &&
|
|
|
|
buf[2] == 0x4E && buf[3] == 0x47
|
|
|
|
}
|
|
|
|
|
|
|
|
func isGif(buf []byte) bool {
|
|
|
|
return len(buf) > 2 &&
|
|
|
|
buf[0] == 0x47 && buf[1] == 0x49 && buf[2] == 0x46
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWebp(buf []byte) bool {
|
|
|
|
return len(buf) > 11 &&
|
|
|
|
buf[8] == 0x57 && buf[9] == 0x45 &&
|
|
|
|
buf[10] == 0x42 && buf[11] == 0x50
|
|
|
|
}
|
|
|
|
|
2020-10-09 11:04:55 +00:00
|
|
|
func RenderAndMakeFile(img image.Image, imgDetail *Details) error {
|
2020-09-29 16:09:42 +00:00
|
|
|
out, err := os.Create(imgDetail.FileName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
2020-10-08 15:30:18 +00:00
|
|
|
err = Render(out, img, imgDetail)
|
2020-09-30 22:33:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-29 16:09:42 +00:00
|
|
|
|
|
|
|
fi, _ := out.Stat()
|
|
|
|
imgDetail.SizeFile = fi.Size()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-09-30 22:33:47 +00:00
|
|
|
|
2020-10-08 15:30:18 +00:00
|
|
|
func Render(w io.Writer, img image.Image, imgDetail *Details) error {
|
|
|
|
// Currently a wrapper for renderJpeg, but this function is useful if multiple render formats are needed
|
|
|
|
return renderJpeg(w, img, imgDetail)
|
|
|
|
}
|
|
|
|
|
2020-09-30 22:33:47 +00:00
|
|
|
func renderJpeg(w io.Writer, m image.Image, imgDetail *Details) error {
|
|
|
|
o := new(jpeg.Options)
|
|
|
|
o.Quality = imgDetail.Quality
|
|
|
|
|
|
|
|
return jpeg.Encode(w, m, o)
|
|
|
|
}
|