Skip to content

Commit

Permalink
Adding trim-border operation. WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dooman87 committed Dec 19, 2023
1 parent 78e608d commit ccd8930
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 195 deletions.
11 changes: 7 additions & 4 deletions img/processor/imagemagick.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (p *ImageMagick) Resize(config *img.TransformationConfig) (*img.Image, erro

args := make([]string, 0)
args = append(args, "-") //Input
args = append(args, getBeforeTransformConvertFormatOptions(source, mimeType)...)
args = append(args, getBeforeTransformConvertFormatOptions(config, source, mimeType)...)
args = append(args, beforeResizeConvertOpts...)
args = append(args, "-resize", targetSize)
args = append(args, getQualityOptions(source, config, mimeType)...)
Expand Down Expand Up @@ -199,7 +199,7 @@ func (p *ImageMagick) FitToSize(config *img.TransformationConfig) (*img.Image, e

args := make([]string, 0)
args = append(args, "-") //Input
args = append(args, getBeforeTransformConvertFormatOptions(source, mimeType)...)
args = append(args, getBeforeTransformConvertFormatOptions(config, source, mimeType)...)
args = append(args, beforeResizeConvertOpts...)
args = append(args, "-resize", targetSize+"^")

Expand Down Expand Up @@ -241,7 +241,7 @@ func (p *ImageMagick) Optimise(config *img.TransformationConfig) (*img.Image, er

args := make([]string, 0)
args = append(args, "-") //Input
args = append(args, getBeforeTransformConvertFormatOptions(source, mimeType)...)
args = append(args, getBeforeTransformConvertFormatOptions(config, source, mimeType)...)
args = append(args, beforeResizeConvertOpts...)
args = append(args, getQualityOptions(source, config, mimeType)...)
args = append(args, p.AdditionalArgs...)
Expand Down Expand Up @@ -495,12 +495,15 @@ func getConvertFormatOptions(source *img.Info) []string {
return opts
}

func getBeforeTransformConvertFormatOptions(source *img.Info, outputMimeType string) []string {
func getBeforeTransformConvertFormatOptions(config *img.TransformationConfig, source *img.Info, outputMimeType string) []string {
var opts []string

if outputMimeType == "image/webp" && source.Format == "GIF" {
opts = append(opts, "-coalesce")
}
if config.TrimBorder {
opts = append(opts, "-trim")
}

return opts
}
Expand Down
27 changes: 27 additions & 0 deletions img/processor/imagemagick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,33 @@ func TestImageMagick_IsIllustration(t *testing.T) {
}
}

var trimBorderTestFiles = []string{"logo-1.png", "logo-2.png", "no-border.jpg"}

func TestImageMagick_Resize_TrimBorder(t *testing.T) {
for _, tt := range trimBorderTestFiles {
f := fmt.Sprintf("%s/%s", "./test_files/trim-border", tt)

orig, err := ioutil.ReadFile(f)
if err != nil {
t.Errorf("can't read file %s: %+v", f, err)
}

resultImage, err := proc.Optimise(&img.TransformationConfig{
Src: &img.Image{
Id: "img",
Data: orig,
},
TrimBorder: true,
})

if err != nil {
t.Errorf("couldn't optimise image %s", tt)
}

ioutil.WriteFile(fmt.Sprintf("./test_files/trim-border/expected_optimise_%s", tt), resultImage.Data, 0777)
}
}

func testImages(t *testing.T, fn transform, files []*testTransformation) {
results := make([]*result, 0)
for _, tt := range files {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/processor/test_files/trim-border/logo-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/processor/test_files/trim-border/logo-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 3 additions & 9 deletions img/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ const (
LOWER
)

type OptimiseConfig struct {
// TrimBorder is a flag whether we need to remove border or not
TrimBorder bool
}

type ResizeConfig struct {
OptimiseConfig
// Size is a size of output images in the format WxH.
Size string
}
Expand All @@ -67,6 +61,8 @@ type TransformationConfig struct {
SupportedFormats []string
// Quality defines quality of output image
Quality Quality
// TrimBorder is a flag whether we need to remove border or not
TrimBorder bool
// Config is the configuration for the specific transformation
Config interface{}
}
Expand Down Expand Up @@ -333,9 +329,6 @@ func (r *Service) transformUrl(resp http.ResponseWriter, req *http.Request, tran
}
}
}
if c, ok := config.(*OptimiseConfig); ok {
c.TrimBorder = trimBorder
}

saveDataHeader := req.Header.Get("Save-Data")

Expand Down Expand Up @@ -367,6 +360,7 @@ func (r *Service) transformUrl(resp http.ResponseWriter, req *http.Request, tran
Src: srcImage,
SupportedFormats: supportedFormats,
Quality: getQuality(saveDataHeader, saveDataParam, dppx),
TrimBorder: trimBorder,
Config: config,
},
Resp: resp,
Expand Down
Loading

0 comments on commit ccd8930

Please sign in to comment.