init
This commit is contained in:
89
webp.go
Normal file
89
webp.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package webp
|
||||
|
||||
import (
|
||||
"image"
|
||||
"os"
|
||||
|
||||
"github.com/chai2010/webp"
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
// EncodeFile encodes the specified image file to WebP format with the given width and height.
|
||||
// The output file name is constructed by adding the .webp extension to the input file name.
|
||||
func EncodeFile(filename string, width, height int) error {
|
||||
// Open the input file
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Decode the input file into an image.Image
|
||||
img, format, err := image.Decode(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Convert the image to RGBA format if it's not already in that format
|
||||
if _, ok := img.(*image.RGBA); !ok {
|
||||
img = imaging.Clone(img)
|
||||
}
|
||||
|
||||
// Resize the image to the specified dimensions
|
||||
resizedImg := imaging.Resize(img, width, height, imaging.Lanczos)
|
||||
|
||||
// Create a new file for writing the output
|
||||
outFilename := filename[:len(filename)-len(format)] + "webp"
|
||||
outFile, err := os.Create(outFilename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer outFile.Close()
|
||||
|
||||
// Encode the resized image to WebP format and write it to the output file
|
||||
err = webp.Encode(outFile, resizedImg, &webp.Options{Quality: 80})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//// Encode encodes the specified image to WebP format with the given width and height.
|
||||
//// The output data is returned as a byte slice.
|
||||
//func Encode(data []byte, width, height int) ([]byte, error) {
|
||||
// // Decode the input data into an image.Image
|
||||
// img, format, err := image.Decode(ioutil.NopCloser(bytes.NewReader(data)))
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
//
|
||||
// // Convert the image to RGBA format if it's not already in that format
|
||||
// if _, ok := img.(*image.RGBA); !ok {
|
||||
// img = imaging.Clone(img)
|
||||
// }
|
||||
//
|
||||
// // Resize the image to the specified dimensions
|
||||
// resizedImg := imaging.Resize(img, width, height, imaging.Lanczos)
|
||||
//
|
||||
// // Encode the resized image to WebP format and return it as a byte slice
|
||||
// var buf bytes.Buffer
|
||||
// err = webp.Encode(&buf, resizedImg, &webp.Options{Quality: 80})
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
//
|
||||
// return buf.Bytes(), nil
|
||||
//}
|
||||
//
|
||||
//// Decode decodes the specified WebP data into an image.Image.
|
||||
//func Decode(data []byte) (image.Image, error) {
|
||||
// return webp.Decode(bytes.NewReader(data))
|
||||
//}
|
||||
//
|
||||
//// IsWebP returns true if the specified data is a WebP image.
|
||||
//func IsWebP(data []byte) bool {
|
||||
// return webp.IsWebP(data)
|
||||
//}
|
||||
//
|
||||
//// IsPNG returns true if the specified
|
Reference in New Issue
Block a user