Skip to content

Commit

Permalink
feat(ActionsMenu/resizeImage): Remove unnecessary param
Browse files Browse the repository at this point in the history
Use directly `MAX_RESIZE_IMAGE_SIZE` constant.
  • Loading branch information
Merkur39 committed Jul 9, 2024
1 parent ee7e46c commit c6d06e8
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions react/ActionsMenu/Actions/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,13 @@ export const makeBase64FromFile = async file => {

/**
* @param {HTMLImageElement} image
* @param {number} [maxSizeInPixel] - Maximum size before being resized
* @returns {number}
*/
const getImageScaleRatio = (image, maxSize) => {
const getImageScaleRatio = image => {
const longerSideSizeInPixel = Math.max(image.height, image.width)
let scaleRatio = 1
if (maxSize < longerSideSizeInPixel) {
scaleRatio = maxSize / longerSideSizeInPixel
if (MAX_RESIZE_IMAGE_SIZE < longerSideSizeInPixel) {
scaleRatio = MAX_RESIZE_IMAGE_SIZE / longerSideSizeInPixel
}

return scaleRatio
Expand All @@ -108,17 +107,16 @@ const getImageScaleRatio = (image, maxSize) => {
/**
* @param {object} opts
* @param {string} opts.base64 - Base64 of image
* @param {number} opts.maxSize - Maximum size before being resized
* @returns {Promise<string>}
*/
const resizeImage = async ({ base64: fileDataUri, maxSize }) => {
const resizeImage = async ({ base64: fileDataUri }) => {
return new Promise((resolve, reject) => {
const newImage = new Image()
newImage.src = fileDataUri
newImage.onerror = reject
newImage.onload = () => {
const canvas = document.createElement('canvas')
const scaleRatio = getImageScaleRatio(newImage, maxSize)
const scaleRatio = getImageScaleRatio(newImage)
const scaledWidth = scaleRatio * newImage.width
const scaledHeight = scaleRatio * newImage.height
const quality =
Expand Down Expand Up @@ -160,8 +158,7 @@ const fileToDataUri = async file => {
const addImageToPdf = async (pdfDoc, file) => {
const fileDataUri = await fileToDataUri(file)
const resizedImage = await resizeImage({
base64: fileDataUri,
maxSize: MAX_RESIZE_IMAGE_SIZE
base64: fileDataUri
})
const img = await pdfDoc.embedJpg(resizedImage)

Expand Down

0 comments on commit c6d06e8

Please sign in to comment.