mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-07 21:48:13 -08: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
|
||||
- Enlarge
|
||||
- Zoom
|
||||
- Extract
|
||||
- Image metadata
|
||||
- Rotate by degrees
|
||||
- Flip/Flop
|
||||
- Extract area
|
||||
- Extract image metadata
|
||||
- Image conversion to multiple formats
|
||||
|
||||
## API
|
||||
|
|
|
|||
8
image.go
8
image.go
|
|
@ -64,6 +64,14 @@ func (i *Image) Type() string {
|
|||
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 {
|
||||
return &Image{buf}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func TestImageResize(t *testing.T) {
|
|||
|
||||
func TestImageCrop(t *testing.T) {
|
||||
image := readImage()
|
||||
_, err := image.Crop(300, 240)
|
||||
_, err := image.Crop(800, 600)
|
||||
if err != nil {
|
||||
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
|
||||
|
||||
const (
|
||||
BICUBIC Interpolator = iota
|
||||
BILINEAR
|
||||
NOHALO
|
||||
)
|
||||
|
||||
var interpolations = map[Interpolator]string{
|
||||
BICUBIC: "bicubic",
|
||||
BILINEAR: "bilinear",
|
||||
|
|
|
|||
|
|
@ -11,12 +11,6 @@ import (
|
|||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
BICUBIC Interpolator = iota
|
||||
BILINEAR
|
||||
NOHALO
|
||||
)
|
||||
|
||||
const (
|
||||
CENTRE Gravity = iota
|
||||
NORTH
|
||||
|
|
@ -163,9 +157,10 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
|||
|
||||
if affinedWidth != o.Width || affinedHeight != o.Height {
|
||||
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.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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func TestResize(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")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
|
|
|||
15
type.go
15
type.go
|
|
@ -16,23 +16,26 @@ func DetermineImageType(buf []byte) ImageType {
|
|||
}
|
||||
|
||||
func DetermineImageTypeName(buf []byte) string {
|
||||
imageCode := vipsImageType(buf)
|
||||
return getImageTypeName(vipsImageType(buf))
|
||||
}
|
||||
|
||||
func getImageTypeName(code ImageType) string {
|
||||
imageType := "unknown"
|
||||
|
||||
switch {
|
||||
case imageCode == JPEG:
|
||||
case code == JPEG:
|
||||
imageType = "jpeg"
|
||||
break
|
||||
case imageCode == WEBP:
|
||||
case code == WEBP:
|
||||
imageType = "webp"
|
||||
break
|
||||
case imageCode == PNG:
|
||||
case code == PNG:
|
||||
imageType = "png"
|
||||
break
|
||||
case imageCode == TIFF:
|
||||
case code == TIFF:
|
||||
imageType = "tiff"
|
||||
break
|
||||
case imageCode == MAGICK:
|
||||
case code == MAGICK:
|
||||
imageType = "magick"
|
||||
break
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue