Skip to content

Commit

Permalink
feat(resizeImage): Reduce quality for big image
Browse files Browse the repository at this point in the history
  • Loading branch information
JF-Cozy committed Mar 27, 2024
1 parent 1d1b388 commit 2b6cacf
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion react/ActionsMenu/Actions/helpers.js
Expand Up @@ -3,6 +3,7 @@ import { fetchBlobFileById } from 'cozy-client/dist/models/file'

// Should guarantee good resolution for different uses (printing, downloading, etc.)
const MAX_RESIZE_IMAGE_SIZE = 3840
const MAX_IMAGE_SIDE_SIZE = 1920

/**
* Make array of actions and hydrate actions with options
Expand Down Expand Up @@ -125,14 +126,19 @@ const resizeImage = async ({
const scaleRatio = getImageScaleRatio(newImage, maxSize)
const scaledWidth = scaleRatio * newImage.width
const scaledHeight = scaleRatio * newImage.height
const quality =
scaledWidth >= MAX_IMAGE_SIDE_SIZE ||
scaledHeight >= MAX_IMAGE_SIDE_SIZE
? 0.35
: 0.75

canvas.width = scaledWidth
canvas.height = scaledHeight
canvas
.getContext('2d')
.drawImage(newImage, 0, 0, scaledWidth, scaledHeight)

resolve(canvas.toDataURL(fileType))
resolve(canvas.toDataURL(fileType, quality))
}
})
}
Expand Down

0 comments on commit 2b6cacf

Please sign in to comment.