|
|
|
|
@ -1,7 +1,10 @@
|
|
|
|
|
package bimg
|
|
|
|
|
|
|
|
|
|
// ImageType represents an image type value.
|
|
|
|
|
type ImageType int
|
|
|
|
|
import (
|
|
|
|
|
"regexp"
|
|
|
|
|
"sync"
|
|
|
|
|
"unicode/utf8"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// UNKNOWN represents an unknow image type value.
|
|
|
|
|
@ -24,6 +27,14 @@ const (
|
|
|
|
|
MAGICK
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ImageType represents an image type value.
|
|
|
|
|
type ImageType int
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
htmlCommentRegex = regexp.MustCompile("(?i)<!--([\\s\\S]*?)-->")
|
|
|
|
|
svgRegex = regexp.MustCompile(`(?i)^\s*(?:<\?xml[^>]*>\s*)?(?:<!doctype svg[^>]*>\s*)?<svg[^>]*>[^*]*<\/svg>\s*$`)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ImageTypes stores as pairs of image types supported and its alias names.
|
|
|
|
|
var ImageTypes = map[ImageType]string{
|
|
|
|
|
JPEG: "jpeg",
|
|
|
|
|
@ -36,6 +47,49 @@ var ImageTypes = map[ImageType]string{
|
|
|
|
|
MAGICK: "magick",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// imageMutex is used to provide thread-safe synchronization
|
|
|
|
|
// for SupportedImageTypes map.
|
|
|
|
|
var imageMutex = &sync.RWMutex{}
|
|
|
|
|
|
|
|
|
|
// SupportedImageTypes stores the optional image type supported
|
|
|
|
|
// by the current libvips compilation.
|
|
|
|
|
// Note: lazy evaluation as demand is required due
|
|
|
|
|
// to bootstrap runtime limitation with C/libvips world.
|
|
|
|
|
var SupportedImageTypes = map[ImageType]bool{}
|
|
|
|
|
|
|
|
|
|
// discoverSupportedImageTypes is used to fill SupportedImageTypes map.
|
|
|
|
|
func discoverSupportedImageTypes() {
|
|
|
|
|
imageMutex.Lock()
|
|
|
|
|
SupportedImageTypes[JPEG] = VipsIsTypeSupported(JPEG)
|
|
|
|
|
SupportedImageTypes[PNG] = VipsIsTypeSupported(PNG)
|
|
|
|
|
SupportedImageTypes[GIF] = VipsIsTypeSupported(GIF)
|
|
|
|
|
SupportedImageTypes[WEBP] = VipsIsTypeSupported(WEBP)
|
|
|
|
|
SupportedImageTypes[SVG] = VipsIsTypeSupported(SVG)
|
|
|
|
|
SupportedImageTypes[TIFF] = VipsIsTypeSupported(TIFF)
|
|
|
|
|
SupportedImageTypes[PDF] = VipsIsTypeSupported(PDF)
|
|
|
|
|
SupportedImageTypes[MAGICK] = VipsIsTypeSupported(MAGICK)
|
|
|
|
|
imageMutex.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// isBinary checks if the given buffer is a binary file.
|
|
|
|
|
func isBinary(buf []byte) bool {
|
|
|
|
|
if len(buf) < 24 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for i := 0; i < 24; i++ {
|
|
|
|
|
charCode, _ := utf8.DecodeRuneInString(string(buf[i]))
|
|
|
|
|
if charCode == 65533 || charCode <= 8 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsSVGImage returns true if the given buffer is a valid SVG image.
|
|
|
|
|
func IsSVGImage(buf []byte) bool {
|
|
|
|
|
return !isBinary(buf) && svgRegex.Match(htmlCommentRegex.ReplaceAll(buf, []byte{}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DetermineImageType determines the image type format (jpeg, png, webp or tiff)
|
|
|
|
|
func DetermineImageType(buf []byte) ImageType {
|
|
|
|
|
return vipsImageType(buf)
|
|
|
|
|
@ -46,16 +100,38 @@ func DetermineImageTypeName(buf []byte) string {
|
|
|
|
|
return ImageTypeName(vipsImageType(buf))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsImageTypeSupportedByVips returns true if the given image type
|
|
|
|
|
// is supported by current libvips compilation.
|
|
|
|
|
func IsImageTypeSupportedByVips(t ImageType) bool {
|
|
|
|
|
imageMutex.RLock()
|
|
|
|
|
|
|
|
|
|
// Discover supported image types and cache the result
|
|
|
|
|
itShouldDiscovery := len(SupportedImageTypes) == 0
|
|
|
|
|
if itShouldDiscovery {
|
|
|
|
|
imageMutex.RUnlock()
|
|
|
|
|
discoverSupportedImageTypes()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if image type is actually supported
|
|
|
|
|
isSupported, ok := SupportedImageTypes[t]
|
|
|
|
|
if !itShouldDiscovery {
|
|
|
|
|
imageMutex.RUnlock()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ok && isSupported
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsTypeSupported checks if a given image type is supported
|
|
|
|
|
func IsTypeSupported(t ImageType) bool {
|
|
|
|
|
return ImageTypes[t] != ""
|
|
|
|
|
_, ok := ImageTypes[t]
|
|
|
|
|
return ok && IsImageTypeSupportedByVips(t)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IsTypeNameSupported checks if a given image type name is supported
|
|
|
|
|
func IsTypeNameSupported(t string) bool {
|
|
|
|
|
for _, name := range ImageTypes {
|
|
|
|
|
for imageType, name := range ImageTypes {
|
|
|
|
|
if name == t {
|
|
|
|
|
return true
|
|
|
|
|
return IsImageTypeSupportedByVips(imageType)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
|