mirror of
https://github.com/talgo-cloud/bimg.git
synced 2026-03-15 10:25:55 -07:00
parent
63f4b01c8d
commit
61997bc68b
7 changed files with 107 additions and 53 deletions
50
resize.go
50
resize.go
|
|
@ -18,7 +18,6 @@ const (
|
|||
|
||||
func Resize(buf []byte, o Options) ([]byte, error) {
|
||||
// detect (if possible) the file type
|
||||
|
||||
defer C.vips_thread_shutdown()
|
||||
|
||||
image, err := vipsRead(buf)
|
||||
|
|
@ -26,16 +25,6 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
//var tmpImage *C.struct__VipsImage
|
||||
/*
|
||||
// feed it
|
||||
imageLength := C.size_t(len(buf))
|
||||
imageBuf := unsafe.Pointer(&buf[0])
|
||||
debug("buffer: %s", buf[0])
|
||||
|
||||
C.vips_jpegload_buffer_seq(imageBuf, imageLength, &image)
|
||||
*/
|
||||
|
||||
// defaults
|
||||
if o.Quality == 0 {
|
||||
o.Quality = QUALITY
|
||||
|
|
@ -46,7 +35,6 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
|||
inHeight := int(image.Ysize)
|
||||
|
||||
// crop
|
||||
|
||||
if o.Crop {
|
||||
left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity)
|
||||
o.Width = int(math.Min(float64(inWidth), float64(o.Width)))
|
||||
|
|
@ -55,31 +43,16 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//err := C.vips_extract_area_0(image, &tmpImage, C.int(left), C.int(top), C.int(o.Width), C.int(o.Height))
|
||||
//C.g_object_unref(C.gpointer(image))
|
||||
//image = tmpImage
|
||||
}
|
||||
|
||||
// rotate
|
||||
r := Rotation{180}
|
||||
image, err = Rotate(image, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if o.Rotate > 0 {
|
||||
image, err = Rotate(image, Rotation{o.Rotate})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Finally save
|
||||
//var ptr unsafe.Pointer
|
||||
//length := C.size_t(0)
|
||||
|
||||
//C.vips_jpegsave_custom(image, &ptr, &length, 1, C.int(o.Quality), 0)
|
||||
//C.g_object_unref(C.gpointer(image))
|
||||
//C.g_object_unref(C.gpointer(newImage))
|
||||
|
||||
// get back the buffer
|
||||
//buf = C.GoBytes(ptr, C.int(length))
|
||||
// cleanup
|
||||
//C.g_free(C.gpointer(ptr))
|
||||
|
||||
buf, err = vipsSave(image, vipsSaveOptions{Quality: o.Quality})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -89,19 +62,6 @@ func Resize(buf []byte, o Options) ([]byte, error) {
|
|||
return buf, nil
|
||||
}
|
||||
|
||||
type Rotation struct {
|
||||
angle int
|
||||
}
|
||||
|
||||
func (a Rotation) calculate() int {
|
||||
angle := a.angle
|
||||
divisor := angle % 90
|
||||
if divisor != 0 {
|
||||
angle = a.angle - divisor
|
||||
}
|
||||
return angle
|
||||
}
|
||||
|
||||
func Rotate(image *C.struct__VipsImage, r Rotation) (*C.struct__VipsImage, error) {
|
||||
//vips := &Vips{}
|
||||
return vipsRotate(image, r.calculate())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue