2018-01-23 10:48:44 +00:00
|
|
|
package common
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
_ "image/gif"
|
|
|
|
"image/jpeg"
|
|
|
|
_ "image/png"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2018-01-23 10:48:44 +00:00
|
|
|
var Thumbnailer ThumbnailerInt
|
|
|
|
|
|
|
|
type ThumbnailerInt interface {
|
2018-07-28 12:52:23 +00:00
|
|
|
Resize(inPath string, tmpPath string, outPath string, width int) error
|
2018-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type RezThumbnailer struct {
|
|
|
|
}
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
func (thumb *RezThumbnailer) Resize(inPath string, tmpPath string, outPath string, width int) error {
|
2018-01-23 10:48:44 +00:00
|
|
|
// TODO: Sniff the aspect ratio of the image and calculate the dest height accordingly, bug make sure it isn't excessively high
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
func (thumb *RezThumbnailer) resize(inPath string, outPath string, width int, height int) error {
|
2018-01-23 10:48:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
// ! Note: CaireThumbnailer can't handle gifs, so we'll have to either cap their sizes or have another resizer deal with them
|
|
|
|
type CaireThumbnailer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCaireThumbnailer() *CaireThumbnailer {
|
|
|
|
return &CaireThumbnailer{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func precodeImage(inPath string, tmpPath string) error {
|
|
|
|
imageFile, err := os.Open(inPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer imageFile.Close()
|
|
|
|
|
|
|
|
img, _, err := image.Decode(imageFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile, err := os.Create(tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
2018-01-23 10:48:44 +00:00
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
return jpeg.Encode(outFile, img, nil)
|
2018-01-23 10:48:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-28 12:52:23 +00:00
|
|
|
func (thumb *CaireThumbnailer) Resize(inPath string, tmpPath string, outPath string, width int) error {
|
|
|
|
err := precodeImage(inPath, tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
// TODO: Caire doesn't work. Try something else. Or get them to fix the index out of range. We get enough wins from re-encoding as jpeg anyway
|
|
|
|
/*imageFile, err := os.Open(tmpPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer imageFile.Close()
|
|
|
|
|
|
|
|
outFile, err := os.Create(outPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer outFile.Close()
|
|
|
|
|
|
|
|
p := &caire.Processor{NewWidth: width, Scale: true}
|
|
|
|
return p.Process(imageFile, outFile)*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
type LilliputThumbnailer struct {
|
2018-01-23 10:48:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
*/
|