@midscene/shared
Advanced tools
Comparing version 0.4.1-beta-20240902070948.0 to 0.4.1-beta-20240909082645.0
@@ -55,14 +55,14 @@ // src/img/info.ts | ||
} | ||
async function resizeImg(base64Data) { | ||
const base64Image = base64Data.split(";base64,").pop() || base64Data; | ||
const imageBuffer = Buffer2.from(base64Image, "base64"); | ||
async function resizeImg(inputData, newSize) { | ||
const isBase64 = typeof inputData === "string"; | ||
const imageBuffer = isBase64 ? Buffer2.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData; | ||
const image = await Jimp2.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("undefined width or height with url"); | ||
throw Error("Undefined width or height from the input image."); | ||
} | ||
const newSize = calculateNewDimensions(width, height); | ||
image.resize(newSize.width, newSize.height); | ||
const buffer = await image.getBufferAsync(Jimp2.MIME_PNG); | ||
return buffer.toString("base64"); | ||
const finalNewSize = newSize || calculateNewDimensions(width, height); | ||
image.resize(finalNewSize.width, finalNewSize.height); | ||
const resizedBuffer = await image.getBufferAsync(Jimp2.MIME_PNG); | ||
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer; | ||
} | ||
@@ -69,0 +69,0 @@ function calculateNewDimensions(originalWidth, originalHeight) { |
@@ -99,14 +99,14 @@ "use strict"; | ||
} | ||
async function resizeImg(base64Data) { | ||
const base64Image = base64Data.split(";base64,").pop() || base64Data; | ||
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64"); | ||
async function resizeImg(inputData, newSize) { | ||
const isBase64 = typeof inputData === "string"; | ||
const imageBuffer = isBase64 ? import_node_buffer2.Buffer.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData; | ||
const image = await import_jimp2.default.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error("undefined width or height with url"); | ||
throw Error("Undefined width or height from the input image."); | ||
} | ||
const newSize = calculateNewDimensions(width, height); | ||
image.resize(newSize.width, newSize.height); | ||
const buffer = await image.getBufferAsync(import_jimp2.default.MIME_PNG); | ||
return buffer.toString("base64"); | ||
const finalNewSize = newSize || calculateNewDimensions(width, height); | ||
image.resize(finalNewSize.width, finalNewSize.height); | ||
const resizedBuffer = await image.getBufferAsync(import_jimp2.default.MIME_PNG); | ||
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer; | ||
} | ||
@@ -113,0 +113,0 @@ function calculateNewDimensions(originalWidth, originalHeight) { |
@@ -61,3 +61,6 @@ import { Buffer } from 'node:buffer'; | ||
*/ | ||
declare function resizeImg(base64Data: string): Promise<string>; | ||
declare function resizeImg(inputData: string | Buffer, newSize?: { | ||
width: number; | ||
height: number; | ||
}): Promise<string | Buffer>; | ||
/** | ||
@@ -64,0 +67,0 @@ * Calculates new dimensions for an image while maintaining its aspect ratio. |
{ | ||
"name": "@midscene/shared", | ||
"version": "0.4.1-beta-20240902070948.0", | ||
"version": "0.4.1-beta-20240909082645.0", | ||
"types": "./dist/types/index.d.ts", | ||
@@ -22,2 +22,7 @@ "main": "./dist/lib/index.js", | ||
"require": "./dist/lib/img.js" | ||
}, | ||
"./fs": { | ||
"types": "./dist/types/fs.d.ts", | ||
"import": "./dist/es/fs.js", | ||
"require": "./dist/lib/fs.js" | ||
} | ||
@@ -35,2 +40,5 @@ }, | ||
"./dist/types/img.d.ts" | ||
], | ||
"fs": [ | ||
"./dist/types/fs.d.ts" | ||
] | ||
@@ -37,0 +45,0 @@ } |
import { Buffer } from 'node:buffer'; | ||
import Jimp from 'jimp'; | ||
import type { Rect } from '../types'; | ||
@@ -48,20 +47,27 @@ /** | ||
*/ | ||
export async function resizeImg(base64Data: string) { | ||
// Remove the base64 data prefix (if any) | ||
const base64Image = base64Data.split(';base64,').pop() || base64Data; | ||
export async function resizeImg( | ||
inputData: string | Buffer, | ||
newSize?: { | ||
width: number; | ||
height: number; | ||
}, | ||
): Promise<string | Buffer> { | ||
const isBase64 = typeof inputData === 'string'; | ||
const imageBuffer = isBase64 | ||
? Buffer.from(inputData.split(';base64,').pop() || inputData, 'base64') | ||
: inputData; | ||
// Converts base64 data to buffer | ||
const imageBuffer = Buffer.from(base64Image, 'base64'); | ||
const image = await Jimp.read(imageBuffer); | ||
const { width, height } = image.bitmap; | ||
if (!width || !height) { | ||
throw Error('undefined width or height with url'); | ||
throw Error('Undefined width or height from the input image.'); | ||
} | ||
const newSize = calculateNewDimensions(width, height); | ||
const finalNewSize = newSize || calculateNewDimensions(width, height); | ||
image.resize(newSize.width, newSize.height); | ||
const buffer = await image.getBufferAsync(Jimp.MIME_PNG); | ||
return buffer.toString('base64'); | ||
image.resize(finalNewSize.width, finalNewSize.height); | ||
const resizedBuffer = await image.getBufferAsync(Jimp.MIME_PNG); | ||
return isBase64 ? resizedBuffer.toString('base64') : resizedBuffer; | ||
} | ||
@@ -68,0 +74,0 @@ |
52908
24
1387
5