mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 02:15:54 -07:00
fix(crop): tests
This commit is contained in:
parent
2e90c11e5b
commit
03f08422d8
8 changed files with 86 additions and 17 deletions
|
|
@ -35,8 +35,10 @@ The [install script](https://github.com/lovell/sharp/blob/master/preinstall.sh)
|
||||||
- Crop
|
- Crop
|
||||||
- Enlarge
|
- Enlarge
|
||||||
- Zoom
|
- Zoom
|
||||||
- Extract
|
- Rotate by degrees
|
||||||
- Image metadata
|
- Flip/Flop
|
||||||
|
- Extract area
|
||||||
|
- Extract image metadata
|
||||||
- Image conversion to multiple formats
|
- Image conversion to multiple formats
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
|
||||||
8
image.go
8
image.go
|
|
@ -64,6 +64,14 @@ func (i *Image) Type() string {
|
||||||
return DetermineImageTypeName(i.buffer)
|
return DetermineImageTypeName(i.buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Image) Metadata() (ImageMetadata, error) {
|
||||||
|
return Metadata(i.buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Image) Size() (ImageSize, error) {
|
||||||
|
return Size(i.buffer)
|
||||||
|
}
|
||||||
|
|
||||||
func NewImage(buf []byte) *Image {
|
func NewImage(buf []byte) *Image {
|
||||||
return &Image{buf}
|
return &Image{buf}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ func TestImageResize(t *testing.T) {
|
||||||
|
|
||||||
func TestImageCrop(t *testing.T) {
|
func TestImageCrop(t *testing.T) {
|
||||||
image := readImage()
|
image := readImage()
|
||||||
_, err := image.Crop(300, 240)
|
_, err := image.Crop(800, 600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Cannot process the image: %#v", err)
|
t.Errorf("Cannot process the image: %#v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
55
metadata.go
Normal file
55
metadata.go
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
package bimg
|
||||||
|
|
||||||
|
/*
|
||||||
|
#cgo pkg-config: vips
|
||||||
|
#include "vips/vips.h"
|
||||||
|
*/
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
import ()
|
||||||
|
|
||||||
|
type ImageSize struct {
|
||||||
|
Width int
|
||||||
|
Height int
|
||||||
|
}
|
||||||
|
|
||||||
|
type ImageMetadata struct {
|
||||||
|
Orientation int
|
||||||
|
Alpha bool
|
||||||
|
Profile bool
|
||||||
|
Space int
|
||||||
|
Type string
|
||||||
|
Size ImageSize
|
||||||
|
}
|
||||||
|
|
||||||
|
func Size(buf []byte) (ImageSize, error) {
|
||||||
|
metadata, err := Metadata(buf)
|
||||||
|
if err != nil {
|
||||||
|
return ImageSize{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ImageSize{
|
||||||
|
Width: int(metadata.Size.Width),
|
||||||
|
Height: int(metadata.Size.Height),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Metadata(buf []byte) (ImageMetadata, error) {
|
||||||
|
defer C.vips_thread_shutdown()
|
||||||
|
|
||||||
|
image, imageType, err := vipsRead(buf)
|
||||||
|
if err != nil {
|
||||||
|
return ImageMetadata{}, err
|
||||||
|
}
|
||||||
|
defer C.g_object_unref(C.gpointer(image))
|
||||||
|
|
||||||
|
metadata := ImageMetadata{
|
||||||
|
Type: getImageTypeName(imageType),
|
||||||
|
Size: ImageSize{
|
||||||
|
Width: int(image.Xsize),
|
||||||
|
Height: int(image.Ysize),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return metadata, nil
|
||||||
|
}
|
||||||
|
|
@ -12,6 +12,12 @@ type Gravity int
|
||||||
|
|
||||||
type Interpolator int
|
type Interpolator int
|
||||||
|
|
||||||
|
const (
|
||||||
|
BICUBIC Interpolator = iota
|
||||||
|
BILINEAR
|
||||||
|
NOHALO
|
||||||
|
)
|
||||||
|
|
||||||
var interpolations = map[Interpolator]string{
|
var interpolations = map[Interpolator]string{
|
||||||
BICUBIC: "bicubic",
|
BICUBIC: "bicubic",
|
||||||
BILINEAR: "bilinear",
|
BILINEAR: "bilinear",
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,6 @@ import (
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
BICUBIC Interpolator = iota
|
|
||||||
BILINEAR
|
|
||||||
NOHALO
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
CENTRE Gravity = iota
|
CENTRE Gravity = iota
|
||||||
NORTH
|
NORTH
|
||||||
|
|
@ -163,9 +157,10 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
||||||
|
|
||||||
if affinedWidth != o.Width || affinedHeight != o.Height {
|
if affinedWidth != o.Width || affinedHeight != o.Height {
|
||||||
if o.Crop {
|
if o.Crop {
|
||||||
left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity)
|
left, top := calculateCrop(affinedWidth, affinedHeight, o.Width, o.Height, o.Gravity)
|
||||||
o.Width = int(math.Min(float64(inWidth), float64(o.Width)))
|
o.Width = int(math.Min(float64(inWidth), float64(o.Width)))
|
||||||
o.Height = int(math.Min(float64(inHeight), float64(o.Height)))
|
o.Height = int(math.Min(float64(inHeight), float64(o.Height)))
|
||||||
|
debug("crop image to %dx%d to %dx%d", left, top, o.Width, o.Height)
|
||||||
image, err = vipsExtract(image, left, top, o.Width, o.Height)
|
image, err = vipsExtract(image, left, top, o.Width, o.Height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ func TestResize(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConvert(t *testing.T) {
|
func TestConvert(t *testing.T) {
|
||||||
options := Options{Width: 640, Height: 480, Crop: false, Type: PNG}
|
options := Options{Width: 640, Height: 480, Crop: true, Type: PNG}
|
||||||
img, err := os.Open("fixtures/test.jpg")
|
img, err := os.Open("fixtures/test.jpg")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|
|
||||||
15
type.go
15
type.go
|
|
@ -16,23 +16,26 @@ func DetermineImageType(buf []byte) ImageType {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DetermineImageTypeName(buf []byte) string {
|
func DetermineImageTypeName(buf []byte) string {
|
||||||
imageCode := vipsImageType(buf)
|
return getImageTypeName(vipsImageType(buf))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getImageTypeName(code ImageType) string {
|
||||||
imageType := "unknown"
|
imageType := "unknown"
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case imageCode == JPEG:
|
case code == JPEG:
|
||||||
imageType = "jpeg"
|
imageType = "jpeg"
|
||||||
break
|
break
|
||||||
case imageCode == WEBP:
|
case code == WEBP:
|
||||||
imageType = "webp"
|
imageType = "webp"
|
||||||
break
|
break
|
||||||
case imageCode == PNG:
|
case code == PNG:
|
||||||
imageType = "png"
|
imageType = "png"
|
||||||
break
|
break
|
||||||
case imageCode == TIFF:
|
case code == TIFF:
|
||||||
imageType = "tiff"
|
imageType = "tiff"
|
||||||
break
|
break
|
||||||
case imageCode == MAGICK:
|
case code == MAGICK:
|
||||||
imageType = "magick"
|
imageType = "magick"
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue