1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-12-03 23:19:17 +02:00
Files
imgproxy/process.go

482 lines
10 KiB
Go
Raw Normal View History

2017-06-20 16:58:55 +03:00
package main
import (
2018-10-05 21:17:36 +06:00
"context"
2017-06-20 16:58:55 +03:00
"math"
2017-09-27 14:42:49 +06:00
"runtime"
2018-11-08 16:34:21 +06:00
"golang.org/x/sync/errgroup"
2017-09-27 14:42:49 +06:00
)
func extractMeta(img *vipsImage) (int, int, int, bool) {
width := img.Width()
height := img.Height()
2018-12-02 19:02:19 +06:00
angle := vipsAngleD0
2017-10-18 04:23:17 +06:00
flip := false
orientation := img.Orientation()
2019-03-22 20:57:10 +06:00
if orientation >= 5 && orientation <= 8 {
width, height = height, width
}
if orientation == 3 || orientation == 4 {
angle = vipsAngleD180
}
if orientation == 5 || orientation == 6 {
angle = vipsAngleD90
}
if orientation == 7 || orientation == 8 {
angle = vipsAngleD270
}
if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
2017-10-18 04:23:17 +06:00
flip = true
}
return width, height, angle, flip
}
2018-12-11 16:02:23 +06:00
func calcScale(width, height int, po *processingOptions, imgtype imageType) float64 {
2018-11-15 17:19:27 +06:00
// If we're going only to crop, we need only to scale down to DPR.
// Scaling up while cropping is not optimal on this stage, we'll do it later if needed.
if po.Resize == resizeCrop {
if po.Dpr < 1 {
return po.Dpr
}
return 1
}
2018-10-28 18:20:58 +06:00
var scale float64
2017-06-20 16:58:55 +03:00
2018-11-15 17:19:27 +06:00
srcW, srcH := float64(width), float64(height)
if (po.Width == 0 || po.Width == width) && (po.Height == 0 || po.Height == height) {
scale = 1
2018-10-28 18:20:58 +06:00
} else {
2018-11-15 17:19:27 +06:00
wr := float64(po.Width) / srcW
hr := float64(po.Height) / srcH
if po.Width == 0 {
scale = hr
} else if po.Height == 0 {
scale = wr
} else if po.Resize == resizeFit {
scale = math.Min(wr, hr)
} else {
scale = math.Max(wr, hr)
}
}
scale = scale * po.Dpr
2018-12-11 16:02:23 +06:00
if !po.Enlarge && scale > 1 && imgtype != imageTypeSVG {
2018-11-15 17:19:27 +06:00
return 1
}
2018-10-28 18:20:58 +06:00
if srcW*scale < 1 {
scale = 1 / srcW
}
2018-10-28 18:20:58 +06:00
if srcH*scale < 1 {
scale = 1 / srcH
2017-06-20 16:58:55 +03:00
}
2018-10-28 18:20:58 +06:00
return scale
2017-06-20 16:58:55 +03:00
}
func canScaleOnLoad(imgtype imageType, scale float64) bool {
if imgtype == imageTypeSVG {
return true
}
if conf.DisableShrinkOnLoad || scale >= 1 {
return false
}
return imgtype == imageTypeJPEG || imgtype == imageTypeWEBP
}
func calcJpegShink(scale float64, imgtype imageType) int {
shrink := int(1.0 / scale)
switch {
case shrink >= 8:
return 8
case shrink >= 4:
return 4
case shrink >= 2:
return 2
2017-10-18 03:12:58 +06:00
}
return 1
}
2018-11-15 17:19:27 +06:00
func calcCrop(width, height, cropWidth, cropHeight int, gravity *gravityOptions) (left, top int) {
if gravity.Type == gravityFocusPoint {
pointX := int(float64(width) * gravity.X)
pointY := int(float64(height) * gravity.Y)
2018-10-19 15:47:11 +06:00
2018-11-15 17:19:27 +06:00
left = maxInt(0, minInt(pointX-cropWidth/2, width-cropWidth))
top = maxInt(0, minInt(pointY-cropHeight/2, height-cropHeight))
2018-10-19 15:47:11 +06:00
return
}
2018-11-15 17:19:27 +06:00
left = (width - cropWidth + 1) / 2
top = (height - cropHeight + 1) / 2
2017-06-20 16:58:55 +03:00
2018-11-15 17:19:27 +06:00
if gravity.Type == gravityNorth || gravity.Type == gravityNorthEast || gravity.Type == gravityNorthWest {
2017-09-27 14:42:49 +06:00
top = 0
}
2017-06-20 16:58:55 +03:00
2018-11-15 17:19:27 +06:00
if gravity.Type == gravityEast || gravity.Type == gravityNorthEast || gravity.Type == gravitySouthEast {
left = width - cropWidth
2017-06-20 16:58:55 +03:00
}
2018-11-15 17:19:27 +06:00
if gravity.Type == gravitySouth || gravity.Type == gravitySouthEast || gravity.Type == gravitySouthWest {
top = height - cropHeight
2017-06-20 16:58:55 +03:00
}
2018-11-15 17:19:27 +06:00
if gravity.Type == gravityWest || gravity.Type == gravityNorthWest || gravity.Type == gravitySouthWest {
2017-09-27 14:42:49 +06:00
left = 0
}
return
}
func transformImage(ctx context.Context, img *vipsImage, data []byte, po *processingOptions, imgtype imageType) error {
2018-11-15 17:19:27 +06:00
var err error
2018-10-02 18:20:23 +06:00
imgWidth, imgHeight, angle, flip := extractMeta(img)
2017-10-18 04:23:17 +06:00
hasAlpha := img.HasAlpha()
2018-11-15 17:19:27 +06:00
2019-03-22 20:57:10 +06:00
scale := calcScale(imgWidth, imgHeight, po, imgtype)
if scale != 1 && data != nil && canScaleOnLoad(imgtype, scale) {
if imgtype == imageTypeWEBP || imgtype == imageTypeSVG {
// Do some scale-on-load
if err := img.Load(data, imgtype, 1, scale, 1); err != nil {
2018-12-11 16:02:23 +06:00
return err
}
} else if imgtype == imageTypeJPEG {
2018-12-11 16:02:23 +06:00
// Do some shrink-on-load
if shrink := calcJpegShink(scale, imgtype); shrink != 1 {
if err := img.Load(data, imgtype, shrink, 1.0, 1); err != nil {
return err
2019-03-22 20:57:10 +06:00
}
2018-12-11 16:02:23 +06:00
}
}
// Update actual image size ans scale after scale-on-load
imgWidth, imgHeight, _, _ = extractMeta(img)
scale = calcScale(imgWidth, imgHeight, po, imgtype)
2019-03-22 20:57:10 +06:00
}
if err = img.Rad2Float(); err != nil {
2019-03-22 20:57:10 +06:00
return err
}
convertToLinear := conf.UseLinearColorspace && (scale != 1 || po.Dpr != 1)
2019-04-01 21:30:24 +06:00
if convertToLinear {
if err = img.ImportColourProfile(true); err != nil {
return err
}
if err = img.LinearColourspace(); err != nil {
2019-04-01 21:30:24 +06:00
return err
}
2019-03-22 20:57:10 +06:00
}
if scale != 1 {
if err = img.Resize(scale, hasAlpha); err != nil {
2019-03-22 20:57:10 +06:00
return err
}
}
// Update actual image size after resize
imgWidth, imgHeight, _, _ = extractMeta(img)
2019-03-22 20:57:10 +06:00
2018-10-05 21:17:36 +06:00
checkTimeout(ctx)
if angle != vipsAngleD0 || flip {
if err = img.CopyMemory(); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
if angle != vipsAngleD0 {
if err = img.Rotate(angle); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
2017-09-27 14:42:49 +06:00
}
2017-09-28 01:17:17 +06:00
if flip {
if err = img.Flip(); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
}
}
2018-10-05 21:17:36 +06:00
checkTimeout(ctx)
2017-09-28 01:17:17 +06:00
2018-11-15 17:19:27 +06:00
cropW, cropH := po.Width, po.Height
if po.Dpr < 1 || (po.Dpr > 1 && po.Resize != resizeCrop) {
cropW = int(float64(cropW) * po.Dpr)
cropH = int(float64(cropH) * po.Dpr)
}
2018-11-15 17:19:27 +06:00
if cropW == 0 {
cropW = imgWidth
} else {
cropW = minInt(cropW, imgWidth)
}
2018-11-15 17:19:27 +06:00
if cropH == 0 {
cropH = imgHeight
} else {
cropH = minInt(cropH, imgHeight)
}
if cropW < imgWidth || cropH < imgHeight {
2018-09-11 17:23:28 +06:00
if po.Gravity.Type == gravitySmart {
if err = img.CopyMemory(); err != nil {
2018-11-08 16:34:21 +06:00
return err
2017-09-27 14:42:49 +06:00
}
if err = img.SmartCrop(cropW, cropH); err != nil {
2018-11-08 16:34:21 +06:00
return err
2017-10-18 04:23:17 +06:00
}
// Applying additional modifications after smart crop causes SIGSEGV on Alpine
// so we have to copy memory after it
if err = img.CopyMemory(); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
} else {
2018-11-15 17:19:27 +06:00
left, top := calcCrop(imgWidth, imgHeight, cropW, cropH, &po.Gravity)
if err = img.Crop(left, top, cropW, cropH); err != nil {
2018-11-08 16:34:21 +06:00
return err
2017-10-18 03:12:58 +06:00
}
}
2018-10-05 21:17:36 +06:00
checkTimeout(ctx)
2017-09-27 14:42:49 +06:00
}
2018-11-15 17:19:27 +06:00
if po.Enlarge && po.Resize == resizeCrop && po.Dpr > 1 {
// We didn't enlarge the image before, because is wasn't optimal. Now it's time to do it
if err = img.Resize(po.Dpr, hasAlpha); err != nil {
2018-11-15 17:19:27 +06:00
return err
}
if err = img.CopyMemory(); err != nil {
2018-11-15 17:19:27 +06:00
return err
}
}
2019-04-01 21:30:24 +06:00
if convertToLinear {
if err = img.FixColourspace(); err != nil {
2019-04-01 21:30:24 +06:00
return err
}
} else {
if err = img.ImportColourProfile(false); err != nil {
return err
}
2019-04-01 21:30:24 +06:00
}
if po.Expand && (po.Width > img.Width() || po.Height > img.Height()) {
if err = img.EnsureAlpha(); err != nil {
2019-02-21 21:55:20 +06:00
return err
}
hasAlpha = true
if err = img.Embed(gravityCenter, po.Width, po.Height, 0, 0); err != nil {
2019-02-21 21:55:20 +06:00
return err
}
}
if hasAlpha && (po.Flatten || po.Format == imageTypeJPEG) {
if err = img.Flatten(po.Background); err != nil {
2018-11-08 16:34:21 +06:00
return err
2018-10-02 18:20:23 +06:00
}
}
2018-09-07 20:46:16 +06:00
if po.Blur > 0 {
if err = img.Blur(po.Blur); err != nil {
2018-11-08 16:34:21 +06:00
return err
2018-09-07 20:46:16 +06:00
}
}
if po.Sharpen > 0 {
if err = img.Sharpen(po.Sharpen); err != nil {
2018-11-08 16:34:21 +06:00
return err
2018-09-07 20:46:16 +06:00
}
}
2018-10-05 21:17:36 +06:00
checkTimeout(ctx)
2018-09-07 20:46:16 +06:00
2018-10-19 15:47:11 +06:00
if po.Watermark.Enabled {
if err = img.ApplyWatermark(&po.Watermark); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
}
return img.FixColourspace()
2018-11-08 16:34:21 +06:00
}
func transformAnimated(ctx context.Context, img *vipsImage, data []byte, po *processingOptions, imgtype imageType) error {
imgWidth := img.Width()
2018-11-08 16:34:21 +06:00
frameHeight, err := img.GetInt("page-height")
2019-06-03 16:53:47 +06:00
if err != nil {
2018-11-08 16:34:21 +06:00
return err
}
framesCount := minInt(img.Height()/frameHeight, conf.MaxGifFrames)
2019-06-03 16:53:47 +06:00
// Double check dimensions because animated image has many frames
if err := checkDimensions(imgWidth, frameHeight*framesCount); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
2019-06-03 16:53:47 +06:00
// Vips 8.8+ supports n-pages and doesn't load the whole animated image on header access
if nPages, _ := img.GetInt("n-pages"); nPages > 0 {
2019-06-03 16:53:47 +06:00
scale := calcScale(imgWidth, frameHeight, po, imgtype)
if nPages > framesCount || canScaleOnLoad(imgtype, scale) {
// Do some scale-on-load
if err := img.Load(data, imgtype, 1, scale, framesCount); err != nil {
2019-06-03 16:53:47 +06:00
return err
}
}
imgWidth = img.Width()
2019-06-03 16:53:47 +06:00
frameHeight, err = img.GetInt("page-height")
2019-06-03 16:53:47 +06:00
if err != nil {
return err
}
}
delay, err := img.GetInt("gif-delay")
2018-11-08 16:34:21 +06:00
if err != nil {
return err
}
loop, err := img.GetInt("gif-loop")
2018-11-08 16:34:21 +06:00
if err != nil {
return err
}
frames := make([]*vipsImage, framesCount)
2018-11-08 16:34:21 +06:00
defer func() {
for _, frame := range frames {
frame.Clear()
2018-11-08 16:34:21 +06:00
}
}()
var errg errgroup.Group
for i := 0; i < framesCount; i++ {
ind := i
errg.Go(func() error {
frame := new(vipsImage)
2018-11-08 16:34:21 +06:00
if err := img.Extract(frame, 0, ind*frameHeight, imgWidth, frameHeight); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
if err := transformImage(ctx, frame, nil, po, imgtype); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
frames[ind] = frame
return nil
})
}
if err := errg.Wait(); err != nil {
return err
}
checkTimeout(ctx)
if err := img.Arrayjoin(frames); err != nil {
2018-11-08 16:34:21 +06:00
return err
}
img.SetInt("page-height", frames[0].Height())
img.SetInt("gif-delay", delay)
img.SetInt("gif-loop", loop)
img.SetInt("n-pages", framesCount)
2018-11-08 16:34:21 +06:00
return nil
}
func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
2019-01-09 18:41:00 +06:00
runtime.LockOSThread()
defer runtime.UnlockOSThread()
2018-11-08 16:34:21 +06:00
if newRelicEnabled {
newRelicCancel := startNewRelicSegment(ctx, "Processing image")
defer newRelicCancel()
}
if prometheusEnabled {
defer startPrometheusDuration(prometheusProcessingDuration)()
}
defer vipsCleanup()
2018-11-08 16:34:21 +06:00
po := getProcessingOptions(ctx)
2018-11-08 16:34:21 +06:00
data := getImageData(ctx).Bytes()
imgtype := getImageType(ctx)
if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop {
return nil, func() {}, errSmartCropNotSupported
2018-11-08 16:34:21 +06:00
}
if po.Format == imageTypeUnknown {
if vipsTypeSupportSave[imgtype] {
po.Format = imgtype
} else {
po.Format = imageTypeJPEG
}
}
2019-06-03 16:53:47 +06:00
animationSupport := conf.MaxGifFrames > 1 && vipsSupportAnimation(imgtype) && vipsSupportAnimation(po.Format)
pages := 1
if animationSupport {
pages = -1
}
img := new(vipsImage)
defer img.Clear()
if err := img.Load(data, imgtype, 1, 1.0, pages); err != nil {
return nil, func() {}, err
2018-11-08 16:34:21 +06:00
}
if animationSupport && img.IsAnimated() {
if err := transformAnimated(ctx, img, data, po, imgtype); err != nil {
return nil, func() {}, err
2018-11-08 16:34:21 +06:00
}
} else {
if err := transformImage(ctx, img, data, po, imgtype); err != nil {
return nil, func() {}, err
2018-10-19 15:47:11 +06:00
}
}
2018-11-08 16:34:21 +06:00
checkTimeout(ctx)
if po.Format == imageTypeGIF {
if err := img.CastUchar(); err != nil {
return nil, func() {}, err
2018-11-08 16:34:21 +06:00
}
checkTimeout(ctx)
}
return img.Save(po.Format, po.Quality)
2017-06-20 16:58:55 +03:00
}