mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 10:25:55 -07:00
fix(image): tests
This commit is contained in:
parent
4330593138
commit
f525611b29
3 changed files with 64 additions and 0 deletions
27
README.md
27
README.md
|
|
@ -163,6 +163,33 @@ if err != nil {
|
||||||
bimg.Write("new.jpg", newImage)
|
bimg.Write("new.jpg", newImage)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Fluent interface
|
||||||
|
|
||||||
|
```go
|
||||||
|
buffer, err := bimg.Read("image.jpg")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
image := bimg.NewImage(buffer)
|
||||||
|
|
||||||
|
// first crop image
|
||||||
|
_, err := image.CropByWidth(300)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// then flip it
|
||||||
|
newImage, err := image.Flip()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the cropped and flipped image
|
||||||
|
bimg.Write("new.jpg", newImage)
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
#### func DetermineImageTypeName
|
#### func DetermineImageTypeName
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
|
||||||
6
image.go
6
image.go
|
|
@ -87,6 +87,12 @@ func (i *Image) Flip() ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flop the image about the horizontal X axis
|
// Flop the image about the horizontal X axis
|
||||||
|
func (i *Image) Flop() ([]byte, error) {
|
||||||
|
options := Options{Flip: VERTICAL}
|
||||||
|
return i.Process(options)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert image to another format
|
||||||
func (i *Image) Convert(t ImageType) ([]byte, error) {
|
func (i *Image) Convert(t ImageType) ([]byte, error) {
|
||||||
options := Options{Type: t}
|
options := Options{Type: t}
|
||||||
return i.Process(options)
|
return i.Process(options)
|
||||||
|
|
|
||||||
|
|
@ -144,6 +144,37 @@ func TestImageMetadata(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFluentInterface(t *testing.T) {
|
||||||
|
image := initImage("test.jpg")
|
||||||
|
_, err := image.CropByWidth(300)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = image.Flip()
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
buf, err := image.Convert(PNG)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, _ := image.Metadata()
|
||||||
|
if data.Alpha != true {
|
||||||
|
t.Fatal("Invalid alpha channel")
|
||||||
|
}
|
||||||
|
if data.Size.Width != 300 {
|
||||||
|
t.Fatal("Invalid width size")
|
||||||
|
}
|
||||||
|
if data.Type != "png" {
|
||||||
|
t.Fatal("Invalid image type")
|
||||||
|
}
|
||||||
|
|
||||||
|
Write("fixtures/test_image_fluent_out.png", buf)
|
||||||
|
}
|
||||||
|
|
||||||
func initImage(file string) *Image {
|
func initImage(file string) *Image {
|
||||||
buf, _ := Read(path.Join("fixtures", file))
|
buf, _ := Read(path.Join("fixtures", file))
|
||||||
return NewImage(buf)
|
return NewImage(buf)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue