From aceb90949af56baa1f3343bf9f66c87d14c62b6d Mon Sep 17 00:00:00 2001 From: gytisrepecka Date: Wed, 4 Mar 2020 12:31:08 +0200 Subject: [PATCH] Added: resize functionality using github.com/disintegration/imaging, dependencies are now managed using Go Modules, initial testing script. --- cmd/webimg/main.go | 7 +++++++ go.mod | 5 +++++ go.sum | 5 +++++ resize.go | 35 +++++++++++++++++++++++++++++++++++ webimg_test.go | 11 +++++++++++ 5 files changed, 63 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 resize.go create mode 100644 webimg_test.go diff --git a/cmd/webimg/main.go b/cmd/webimg/main.go index f167ba7..fb11b26 100644 --- a/cmd/webimg/main.go +++ b/cmd/webimg/main.go @@ -84,6 +84,13 @@ func main() { } else { fmt.Println("No image given to watermark!") } + case "resize": + doResize := webimg.Resize("smplayer_preferences.jpg", "smplayer_preferences_resize.jpg", 200, 0) + if doResize != nil { + fmt.Println("There was an error watermarking image...") + } else { + fmt.Println("Image resized.") + } default: fmt.Println("Wrong subcommand provided!") } diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ba1a999 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module code.gyt.is/webimg + +go 1.14 + +require github.com/disintegration/imaging v1.6.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..6e27fcd --- /dev/null +++ b/go.sum @@ -0,0 +1,5 @@ +github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c= +github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U= +golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/resize.go b/resize.go new file mode 100644 index 0000000..dc1940e --- /dev/null +++ b/resize.go @@ -0,0 +1,35 @@ +/* + * Copyright © 2020 Gytis Repečka (gytis@repecka.com) + * + * This file is part of webimg. + * + * webimg is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, included + * in the LICENSE file in this source code package. + */ + + package webimg + + import ( + "log" + "github.com/disintegration/imaging" + ) + + func Resize(imagePath, resultPath string, resultWidth, resultHeight int) (err error) { + // Open a test image. + src, err := imaging.Open(imagePath) + if err != nil { + log.Fatalf("failed to open image: %v", err) + } + + // Resize the cropped image to width = 200px preserving the aspect ratio. + src = imaging.Resize(src, resultWidth, resultHeight, imaging.Lanczos) + + // Save the resulting image as JPEG. + err = imaging.Save(src, resultPath) + if err != nil { + log.Fatalf("failed to save image: %v", err) + } + + return err + } diff --git a/webimg_test.go b/webimg_test.go new file mode 100644 index 0000000..26eb93a --- /dev/null +++ b/webimg_test.go @@ -0,0 +1,11 @@ +package webimg + +import "testing" + +func TestWatermark(t *testing.T) { + // want := nil + var want error = nil + if got := Watermark("cmd/webimg/smplayer_preferences.jpg", "cmd/webimg/watermark_inretio-logo.png", "cmd/webimg/test_result_img.jpg", 30, 30, 70); got != want { + t.Errorf("Watermark = %q, want %q", got, want) + } +}