-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose ConvertImageCommand function in imagick.v2 (refs #220)
- Loading branch information
Showing
8 changed files
with
310 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"gopkg.in/gographics/imagick.v3/imagick" | ||
) | ||
|
||
func main() { | ||
imagick.Initialize() | ||
defer imagick.Terminate() | ||
|
||
ret, err := imagick.ConvertImageCommand([]string{ | ||
"convert", "logo:", "-resize", "100x100", "/tmp/out.png", | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("Metadata:\n%s\n", ret.Meta) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ module resizer | |
|
||
require gopkg.in/gographics/imagick.v2 v2.5.0 | ||
|
||
go 1.13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
gopkg.in/gographics/imagick.v2 v2.5.0/go.mod h1:of4TbGX8yMcpgWkWFjha7FsOFr+NjOJ5O1qtKU27Yj0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2013 Herbert G. Fischer. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package imagick | ||
|
||
/* | ||
#include <wand/MagickWand.h> | ||
*/ | ||
import "C" | ||
|
||
import "fmt" | ||
|
||
// ExceptionInfo is an error type returned by certain | ||
// New* API calls. | ||
type ExceptionInfo struct { | ||
kind ExceptionType | ||
errno int | ||
reason string | ||
description string | ||
} | ||
|
||
// Create a new ExceptionInfo wrapper around a C ExceptionInfo ptr | ||
func newExceptionInfo(errInfo *C.ExceptionInfo) *ExceptionInfo { | ||
if errInfo == nil { | ||
return nil | ||
} | ||
|
||
return &ExceptionInfo{ | ||
kind: ExceptionType(errInfo.severity), | ||
errno: int(errInfo.error_number), | ||
reason: C.GoString(errInfo.reason), | ||
description: C.GoString(errInfo.description), | ||
} | ||
} | ||
|
||
// Check if a given C ExceptionInfo ptr is an error. | ||
// Returns a valid ExceptionInfo if there was an error, | ||
// otherwise returns nil | ||
func checkExceptionInfo(errInfo *C.ExceptionInfo) *ExceptionInfo { | ||
if errInfo != nil && errInfo.error_number != 0 { | ||
return newExceptionInfo(errInfo) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (e *ExceptionInfo) Error() string { | ||
if e == nil { | ||
return "" | ||
} | ||
return fmt.Sprintf("%s (%d): %s %s", | ||
e.kind.String(), e.Errno(), e.Reason(), e.Description()) | ||
} | ||
|
||
// Errno returns the ExceptionInfo error number (non-zero if error) | ||
func (e *ExceptionInfo) Errno() int { | ||
if e == nil { | ||
return 0 | ||
} | ||
return e.errno | ||
} | ||
|
||
// Reason returns the string reason for the Exception | ||
func (e *ExceptionInfo) Reason() string { | ||
if e == nil { | ||
return "" | ||
} | ||
return e.reason | ||
} | ||
|
||
// Description returns the string description for the Exception | ||
func (e *ExceptionInfo) Description() string { | ||
if e == nil { | ||
return "" | ||
} | ||
return e.description | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2013 Herbert G. Fischer. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package imagick | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func init() { | ||
Initialize() | ||
} | ||
|
||
// TODO(justinfx): Need to expand test to try and | ||
// produce and catch an error from NewPixelWand | ||
func TestNewMagickImage(t *testing.T) { | ||
info := newImageInfo() | ||
defer info.Destroy() | ||
|
||
pp, err := QueryMagickColor("blue") | ||
if err != nil { | ||
t.Fatal(err.Error()) | ||
} | ||
|
||
img := NewMagickImage(info, 100, 100, pp) | ||
if img == nil { | ||
t.Fatal("nil MagickImage") | ||
} | ||
} | ||
|
||
func ExampleConvertImageCommand() { | ||
ret, err := ConvertImageCommand([]string{ | ||
"convert", "logo:", "-resize", "100x100", "/tmp/out.png", | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("Meta:", ret.Meta) | ||
} | ||
|
||
func TestConvertImageCommand(t *testing.T) { | ||
tmp, err := ioutil.TempFile("", "imagick_test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer os.Remove(tmp.Name()) | ||
|
||
ret, err := ConvertImageCommand([]string{ | ||
"convert", "logo:", "-resize", "100x100", tmp.Name(), | ||
}) | ||
if err != nil { | ||
t.Fatalf("command failed: %v", err) | ||
} | ||
|
||
if ret.Meta == "" { | ||
t.Fatal("got empty metadata string from command result") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters