Merge pull request #533 from buttercannfly/main

better way to decode images
This commit is contained in:
LinkinStars 2023-09-20 10:16:25 +08:00 committed by GitHub
commit 6c2a288abe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 9 deletions

View File

@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright 2022 joyqi
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@ -34,7 +34,7 @@ Answer provides a plugin system for developers to create custom plugins and expa
We value your feedback and suggestions to improve our documentation. If you have any comments or questions, please feel free to contact us. Were excited to see what you can create using our plugin system!
You can also check out the [officially supported plugins here](https://github.com/answerdev/plugins).
You can also check out the [plugins here](https://github.com/answerdev/plugins).
## Contributing

View File

@ -1,21 +1,22 @@
package checker
import (
"image/jpeg"
"image/png"
"image"
_ "image/gif" // use init to support decode jpeg,jpg,png,gif
_ "image/jpeg"
_ "image/png"
"io"
"strings"
)
// IsSupportedImageFile currently answers support image type is `image/jpeg,image/jpg,image/png`
// IsSupportedImageFile currently answers support image type is `image/jpeg,image/jpg,image/png, image/gif`
func IsSupportedImageFile(file io.Reader, ext string) bool {
ext = strings.TrimPrefix(ext, ".")
var err error
switch strings.ToUpper(ext) {
case "JPG", "JPEG":
_, err = jpeg.Decode(file)
case "PNG":
_, err = png.Decode(file)
case "JPG", "JPEG", "PNG", "GIF": // only allow for `image/jpeg,image/jpg,image/png, image/gif`
_, _, err = image.Decode(file)
case "ICO":
// TODO: There is currently no good Golang library to parse whether the image is in ico format.
return true