mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-02-02 11:34:20 +02:00
Shrink on load for jpg and webp
This commit is contained in:
parent
168efd16bb
commit
8c4755d9e0
49
process.go
49
process.go
@ -186,6 +186,25 @@ func calcScale(width, height int, po processingOptions) float64 {
|
|||||||
return math.Max(wr, hr)
|
return math.Max(wr, hr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func calcShink(scale float64, imgtype imageType) int {
|
||||||
|
shrink := int(1.0 / scale)
|
||||||
|
|
||||||
|
if imgtype != JPEG {
|
||||||
|
return shrink
|
||||||
|
}
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case shrink >= 16:
|
||||||
|
return 8
|
||||||
|
case shrink >= 8:
|
||||||
|
return 4
|
||||||
|
case shrink >= 4:
|
||||||
|
return 2
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
func calcCrop(width, height int, po processingOptions) (left, top int) {
|
func calcCrop(width, height int, po processingOptions) (left, top int) {
|
||||||
left = (width - po.width + 1) / 2
|
left = (width - po.width + 1) / 2
|
||||||
top = (height - po.height + 1) / 2
|
top = (height - po.height + 1) / 2
|
||||||
@ -224,7 +243,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
|
|||||||
defer C.vips_cleanup()
|
defer C.vips_cleanup()
|
||||||
|
|
||||||
// Load the image
|
// Load the image
|
||||||
err = C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), &img)
|
err = C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), 1, &img)
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return nil, vipsError()
|
return nil, vipsError()
|
||||||
}
|
}
|
||||||
@ -245,18 +264,13 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
|
|||||||
}
|
}
|
||||||
|
|
||||||
if po.width != imgWidth || po.height != imgHeight {
|
if po.width != imgWidth || po.height != imgHeight {
|
||||||
pResize, pCrop, pSmart := C.FALSE, C.FALSE, C.FALSE
|
pCrop, pSmart := 0, 0
|
||||||
|
pScale := 1.0
|
||||||
|
|
||||||
var (
|
var pLeft, pTop, pWidth, pHeight int
|
||||||
pScale float64
|
|
||||||
pLeft, pTop, pWidth, pHeight int
|
|
||||||
)
|
|
||||||
|
|
||||||
if po.resize == FILL || po.resize == FIT {
|
if po.resize == FILL || po.resize == FIT {
|
||||||
pResize = 1
|
|
||||||
pScale = calcScale(imgWidth, imgHeight, po)
|
pScale = calcScale(imgWidth, imgHeight, po)
|
||||||
} else {
|
|
||||||
pScale = 1.0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if po.resize == FILL || po.resize == CROP {
|
if po.resize == FILL || po.resize == CROP {
|
||||||
@ -270,7 +284,22 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = C.vips_process_image(&img, C.gboolean(pResize), C.double(pScale), C.gboolean(pCrop), C.gboolean(pSmart), C.int(pLeft), C.int(pTop), C.int(pWidth), C.int(pHeight), C.VipsAngle(angle), C.gboolean(flip))
|
// Do some shrink-on-load
|
||||||
|
if pScale < 1.0 {
|
||||||
|
if imgtype == JPEG || imgtype == WEBP {
|
||||||
|
shrink := calcShink(pScale, imgtype)
|
||||||
|
pScale = pScale * float64(shrink)
|
||||||
|
|
||||||
|
var tmp *C.struct__VipsImage
|
||||||
|
err = C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), C.int(shrink), &tmp)
|
||||||
|
if err != 0 {
|
||||||
|
return nil, vipsError()
|
||||||
|
}
|
||||||
|
C.swap_and_clear(&img, tmp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = C.vips_process_image(&img, C.double(pScale), C.gboolean(pCrop), C.gboolean(pSmart), C.int(pLeft), C.int(pTop), C.int(pWidth), C.int(pHeight), C.VipsAngle(angle), C.gboolean(flip))
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return nil, vipsError()
|
return nil, vipsError()
|
||||||
}
|
}
|
||||||
|
12
vips.h
12
vips.h
@ -71,13 +71,19 @@ vips_type_find_save_go(int imgtype) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_load_buffer(void *buf, size_t len, int imgtype, VipsImage **out) {
|
vips_load_buffer(void *buf, size_t len, int imgtype, int shrink, VipsImage **out) {
|
||||||
switch (imgtype) {
|
switch (imgtype) {
|
||||||
case JPEG:
|
case JPEG:
|
||||||
|
if (shrink > 1) {
|
||||||
|
return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
|
||||||
|
}
|
||||||
return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
case PNG:
|
case PNG:
|
||||||
return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
case WEBP:
|
case WEBP:
|
||||||
|
if (shrink > 1) {
|
||||||
|
return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
|
||||||
|
}
|
||||||
return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
#if VIPS_SUPPORT_GIF
|
#if VIPS_SUPPORT_GIF
|
||||||
case GIF:
|
case GIF:
|
||||||
@ -109,10 +115,10 @@ vips_support_smartcrop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_process_image(VipsImage **img, gboolean resize, double scale, gboolean crop, gboolean smart, int left, int top, int width, int height, VipsAngle angle, gboolean flip) {
|
vips_process_image(VipsImage **img, double scale, gboolean crop, gboolean smart, int left, int top, int width, int height, VipsAngle angle, gboolean flip) {
|
||||||
VipsImage *tmp;
|
VipsImage *tmp;
|
||||||
|
|
||||||
if (resize && scale != 1.0) {
|
if (scale != 1.0) {
|
||||||
if (vips_resize(*img, &tmp, scale, NULL)) return 1;
|
if (vips_resize(*img, &tmp, scale, NULL)) return 1;
|
||||||
swap_and_clear(img, tmp);
|
swap_and_clear(img, tmp);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user