You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
caddy-hugo2/media/image.go

50 lines
857 B

package media
import (
"image"
"image/jpeg"
"os"
"path"
"github.com/nfnt/resize"
)
func imageSize(name string) (image.Rectangle, error) {
f, err := os.Open(name)
if err != nil {
return image.ZR, err
}
defer f.Close()
cfg, _, err := image.DecodeConfig(f)
if err != nil {
return image.ZR, err
}
width := cfg.Width
height := cfg.Height
return image.Rect(0, 0, width, height), nil
}
func ThumbImage(filename string, img image.Image, size image.Rectangle) error {
os.MkdirAll(path.Dir(filename), 0755)
fthumb, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0655)
if err != nil {
return err
}
img = resize.Resize(uint(size.Dx()), uint(size.Dy()), img, resize.Bilinear)
err = jpeg.Encode(fthumb, img, nil)
if err != nil {
return err
}
err = fthumb.Close()
if err != nil {
return err
}
return nil
}