expo-image-manipulator
Version:
Provides functions that let you manipulation images on the local file system, eg: resize, crop.
66 lines (64 loc) • 2.41 kB
JavaScript
import { CodedError } from 'expo';
export function getContext(canvas) {
const ctx = canvas.getContext('2d');
if (!ctx) {
throw new CodedError('ERR_IMAGE_MANIPULATOR', 'Failed to create canvas context');
}
return ctx;
}
export async function blobToBase64String(blob) {
const dataURL = await new Promise((resolve, reject)=>{
const reader = new FileReader();
reader.onloadend = ()=>resolve(reader.result);
reader.onerror = ()=>reject(new Error(`Unable to convert blob to base64 string: ${reader.error}`));
reader.readAsDataURL(blob);
});
return dataURL.replace(/^data:image\/\w+;base64,/, '');
}
export function releaseCanvas(canvas) {
canvas.width = 0;
canvas.height = 0;
}
export function loadImageAsync(uri, options) {
return new Promise((resolve, reject)=>{
const imageSource = new Image();
imageSource.crossOrigin = 'anonymous';
const canvas = document.createElement('canvas');
imageSource.onload = ()=>{
const { width, height } = boundedSize(imageSource.naturalWidth, imageSource.naturalHeight, options?.maxWidth, options?.maxHeight);
canvas.width = width;
canvas.height = height;
const context = getContext(canvas);
context.drawImage(imageSource, 0, 0, width, height);
resolve(canvas);
};
imageSource.onerror = ()=>{
releaseCanvas(canvas);
reject(new CodedError('ERR_IMAGE_MANIPULATOR_LOAD', 'Failed to load the image. Make sure the source URI is accessible and has not been revoked or released.'));
};
imageSource.src = uri;
});
}
/**
* Computes the size that bounds an image of the given dimensions to `maxWidth`/`maxHeight`,
* preserving the aspect ratio. Images that already fit within the bounds keep their size.
*/ export function boundedSize(width, height, maxWidth, maxHeight) {
let scale = 1;
if (maxWidth && maxWidth > 0) {
scale = Math.min(scale, maxWidth / width);
}
if (maxHeight && maxHeight > 0) {
scale = Math.min(scale, maxHeight / height);
}
if (scale >= 1) {
return {
width,
height
};
}
return {
width: Math.max(1, Math.round(width * scale)),
height: Math.max(1, Math.round(height * scale))
};
}
//# sourceMappingURL=utils.web.js.map