44 lines
917 B
Go
44 lines
917 B
Go
package main
|
|
|
|
import (
|
|
lib "git.makerforce.io/photos/photos/pkg/bucket"
|
|
"github.com/davidbyttow/govips/pkg/vips"
|
|
)
|
|
|
|
func readsize(buf []byte) (int, int, error) {
|
|
i, err := vips.NewImageFromBuffer(buf)
|
|
if err != nil {
|
|
return 0, 0, err
|
|
}
|
|
return i.Width(), i.Height(), nil
|
|
}
|
|
|
|
func resize(buf []byte, height int, format vips.ImageType, quality int) ([]byte, error) {
|
|
i, err := vips.NewImageFromBuffer(buf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer i.Close()
|
|
|
|
scale := float64(height) / float64(i.Height())
|
|
err = i.Resize(scale)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
outBuf, _, err := i.Export(vips.ExportParams{
|
|
Format: format,
|
|
Quality: quality,
|
|
})
|
|
return outBuf, err
|
|
}
|
|
|
|
func toVipsImageType(format lib.PhotoFormat) vips.ImageType {
|
|
if format == lib.PhotoFormatWEBP {
|
|
return vips.ImageTypeWEBP
|
|
}
|
|
if format == lib.PhotoFormatJPEG {
|
|
return vips.ImageTypeJPEG
|
|
}
|
|
return vips.ImageTypeUnknown
|
|
}
|