Browse Source

Check timeout after every save attempt in saveImageToFitBytes

DarthSim 4 years ago
parent
commit
53f8b0dbdd
1 changed files with 4 additions and 4 deletions
  1. 4 4
      process.go

+ 4 - 4
process.go

@@ -734,12 +734,10 @@ func getIcoData(imgdata *imageData) (*imageData, error) {
 	return nil, fmt.Errorf("Can't load %s from ICO", meta.Format())
 }
 
-func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context.CancelFunc, error) {
+func saveImageToFitBytes(ctx context.Context, po *processingOptions, img *vipsImage) ([]byte, context.CancelFunc, error) {
 	var diff float64
 	quality := po.getQuality()
 
-	img.CopyMemory()
-
 	for {
 		result, cancel, err := img.Save(po.Format, quality)
 		if len(result) <= po.MaxBytes || quality <= 10 || err != nil {
@@ -747,6 +745,8 @@ func saveImageToFitBytes(po *processingOptions, img *vipsImage) ([]byte, context
 		}
 		cancel()
 
+		checkTimeout(ctx)
+
 		delta := float64(len(result)) / float64(po.MaxBytes)
 		switch {
 		case delta > 3:
@@ -866,7 +866,7 @@ func processImage(ctx context.Context) ([]byte, context.CancelFunc, error) {
 	}
 
 	if po.MaxBytes > 0 && canFitToBytes(po.Format) {
-		return saveImageToFitBytes(po, img)
+		return saveImageToFitBytes(ctx, po, img)
 	}
 
 	return img.Save(po.Format, po.getQuality())