Skip to content

Commit

Permalink
Remove Metadata on RAW image
Browse files Browse the repository at this point in the history
  • Loading branch information
n0vad3v committed Mar 13, 2024
1 parent 5dcc4b7 commit a1f05b3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
"IMG_MAP": {},
"ALLOWED_TYPES": ["jpg","png","jpeg","bmp","svg","heic","nef"],
"CONVERT_TYPES": ["webp","avif","jxl"],
"STRIP_METADATA": true,
"ENABLE_EXTRA_PARAMS": false
"READ_BUFFER_SIZE": 4096,
"CONCURRENCY": 262144,
Expand Down Expand Up @@ -78,6 +79,7 @@ type WebpConfig struct {
EnableJXL bool `json:"ENABLE_JXL"`

EnableExtraParams bool `json:"ENABLE_EXTRA_PARAMS"`
StripMetadata bool `json:"STRIP_METADATA"`
ReadBufferSize int `json:"READ_BUFFER_SIZE"`
Concurrency int `json:"CONCURRENCY"`
DisableKeepalive bool `json:"DISABLE_KEEPALIVE"`
Expand All @@ -100,6 +102,7 @@ func NewWebPConfig() *WebpConfig {
EnableJXL: false,

EnableExtraParams: false,
StripMetadata: true,
ReadBufferSize: 4096,
Concurrency: 262144,
DisableKeepalive: false,
Expand Down Expand Up @@ -188,6 +191,16 @@ func LoadConfig() {
log.Warnf("WEBP_ENABLE_EXTRA_PARAMS is not a valid boolean, using value in config.json %t", Config.EnableExtraParams)
}
}
if os.Getenv("WEBP_STRIP_METADATA") != "" {
stripMetadata := os.Getenv("WEBP_STRIP_METADATA")
if stripMetadata == "true" {
Config.StripMetadata = true
} else if stripMetadata == "false" {
Config.StripMetadata = false
} else {
log.Warnf("WEBP_STRIP_METADATA is not a valid boolean, using value in config.json %t", Config.StripMetadata)
}
}
if os.Getenv("WEBP_IMG_MAP") != "" {
// TODO
}
Expand Down
8 changes: 4 additions & 4 deletions encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,13 @@ func avifEncoder(img *vips.ImageRef, rawPath string, optimizedPath string) error
if quality >= 100 {
buf, _, err = img.ExportAvif(&vips.AvifExportParams{
Lossless: true,
StripMetadata: true,
StripMetadata: config.Config.StripMetadata,
})
} else {
buf, _, err = img.ExportAvif(&vips.AvifExportParams{
Quality: quality,
Lossless: false,
StripMetadata: true,
StripMetadata: config.Config.StripMetadata,
})
}

Expand Down Expand Up @@ -227,15 +227,15 @@ func webpEncoder(img *vips.ImageRef, rawPath string, optimizedPath string) error
// use_lossless_preset = 0; // disable -z option
buf, _, err = img.ExportWebp(&vips.WebpExportParams{
Lossless: true,
StripMetadata: true,
StripMetadata: config.Config.StripMetadata,
})
} else {
// If some special images cannot encode with default ReductionEffort(0), then retry from 0 to 6
// Example: https://github.com/webp-sh/webp_server_go/issues/234
ep := vips.WebpExportParams{
Quality: quality,
Lossless: false,
StripMetadata: true,
StripMetadata: config.Config.StripMetadata,
}
for i := range 7 {
ep.ReductionEffort = i
Expand Down
3 changes: 3 additions & 0 deletions encoder/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func ResizeItself(raw, dest string, extraParams config.ExtraParams) {
return
}
_ = resizeImage(img, extraParams)
if config.Config.StripMetadata {
img.RemoveMetadata()
}
buf, _, _ := img.ExportNative()
_ = os.WriteFile(dest, buf, 0600)
img.Close()
Expand Down

0 comments on commit a1f05b3

Please sign in to comment.