Compare commits
1 commit
master
...
image-resi
Author | SHA1 | Date | |
---|---|---|---|
aceb90949a |
5 changed files with 63 additions and 0 deletions
|
@ -84,6 +84,13 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("No image given to watermark!")
|
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:
|
default:
|
||||||
fmt.Println("Wrong subcommand provided!")
|
fmt.Println("Wrong subcommand provided!")
|
||||||
}
|
}
|
||||||
|
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module code.gyt.is/webimg
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
|
||||||
|
require github.com/disintegration/imaging v1.6.2
|
5
go.sum
Normal file
5
go.sum
Normal file
|
@ -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=
|
35
resize.go
Normal file
35
resize.go
Normal file
|
@ -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
|
||||||
|
}
|
11
webimg_test.go
Normal file
11
webimg_test.go
Normal file
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue