Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475export function extractDataUriMime(uri: string): string { return uri.substring(uri.indexOf(':') + 1, uri.indexOf(';'))}
export function getResizedDimensions( originalDims: { width: number height: number }, maxDimension: number,) { if ( originalDims.width <= maxDimension && originalDims.height <= maxDimension ) { return originalDims }
const ratio = Math.min( maxDimension / originalDims.width, maxDimension / originalDims.height, )
return { width: Math.round(originalDims.width * ratio), height: Math.round(originalDims.height * ratio), }}
// Fairly accurate estimate that is more performant// than decoding and checking length of URIexport function getDataUriSize(uri: string): number { return Math.round((uri.length * 3) / 4)}
export function isUriImage(uri: string): boolean { return /\.(jpg|jpeg|png|webp).*$/.test(uri)}
export function blobToDataUri(blob: Blob): Promise<string> { return new Promise((resolve, reject) => { const reader = new FileReader() reader.onloadend = () => { if (typeof reader.result === 'string') { resolve(reader.result) } else { reject(new Error('Failed to read blob')) } } reader.onerror = reject reader.readAsDataURL(blob) })}
export type ImgproxyPreset = | 'default' | 'avatar_thumbnail' | 'avatar' | 'banner' | 'feed_fullsize' | 'feed_thumbnail' | 'download'
// Using capturing groups here instead of lookbehinds in order to support older versions of Safari.// https://bugs.webkit.org/show_bug.cgi?id=174931const IMGPROXY_PRESET_RE = /(\/img\/)(default|avatar_thumbnail|avatar|banner|feed_fullsize|feed_thumbnail|download)(\/)/
/** * Replaces any imgproxy preset in a CDN URI with the given preset. */export function convertCdnPreset(uri: string, preset: ImgproxyPreset): string { return uri.replace(IMGPROXY_PRESET_RE, `$1${preset}$3`)}