Apply per-use-case image size configs (#10690)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import {createDownloadResumable, deleteAsync} from 'expo-file-system/legacy'
|
import {createDownloadResumable, deleteAsync} from 'expo-file-system/legacy'
|
||||||
import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
|
import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
|
||||||
|
|
||||||
|
import {IMAGE_SIZE_CONFIG_2K_1MB} from '../../src/lib/constants'
|
||||||
import {
|
import {
|
||||||
downloadAndResize,
|
downloadAndResize,
|
||||||
type DownloadAndResizeOpts,
|
type DownloadAndResizeOpts,
|
||||||
getResizedDimensions,
|
|
||||||
} from '../../src/lib/media/manip'
|
} from '../../src/lib/media/manip'
|
||||||
|
import {getResizedDimensions} from '../../src/lib/media/util'
|
||||||
|
|
||||||
const mockResizedImage = {
|
const mockResizedImage = {
|
||||||
path: 'file://resized-image.jpg',
|
path: 'file://resized-image.jpg',
|
||||||
@@ -41,10 +42,8 @@ describe('downloadAndResize', () => {
|
|||||||
|
|
||||||
const opts: DownloadAndResizeOpts = {
|
const opts: DownloadAndResizeOpts = {
|
||||||
uri: 'https://example.com/image.jpg',
|
uri: 'https://example.com/image.jpg',
|
||||||
width: 100,
|
maxDimension: 2000,
|
||||||
height: 100,
|
|
||||||
maxSize: 500000,
|
maxSize: 500000,
|
||||||
mode: 'cover',
|
|
||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,9 +59,11 @@ describe('downloadAndResize', () => {
|
|||||||
|
|
||||||
// First time it gets called is to get dimensions
|
// First time it gets called is to get dimensions
|
||||||
expect(manipulateAsync).toHaveBeenCalledWith(expect.any(String), [], {})
|
expect(manipulateAsync).toHaveBeenCalledWith(expect.any(String), [], {})
|
||||||
|
// The mocked source image is 100x100, below maxDimension, so it is not
|
||||||
|
// downsized.
|
||||||
expect(manipulateAsync).toHaveBeenCalledWith(
|
expect(manipulateAsync).toHaveBeenCalledWith(
|
||||||
expect.any(String),
|
expect.any(String),
|
||||||
[{resize: {height: opts.height, width: opts.width}}],
|
[{resize: {height: 100, width: 100}}],
|
||||||
{format: SaveFormat.JPEG, compress: 1.0},
|
{format: SaveFormat.JPEG, compress: 1.0},
|
||||||
)
|
)
|
||||||
expect(deleteAsync).toHaveBeenCalledWith(expect.any(String), {
|
expect(deleteAsync).toHaveBeenCalledWith(expect.any(String), {
|
||||||
@@ -73,10 +74,8 @@ describe('downloadAndResize', () => {
|
|||||||
it('should return undefined for invalid URI', async () => {
|
it('should return undefined for invalid URI', async () => {
|
||||||
const opts: DownloadAndResizeOpts = {
|
const opts: DownloadAndResizeOpts = {
|
||||||
uri: 'invalid-uri',
|
uri: 'invalid-uri',
|
||||||
width: 100,
|
maxDimension: 2000,
|
||||||
height: 100,
|
|
||||||
maxSize: 500000,
|
maxSize: 500000,
|
||||||
mode: 'cover',
|
|
||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,13 +89,19 @@ describe('downloadAndResize', () => {
|
|||||||
width: 1200,
|
width: 1200,
|
||||||
height: 1000,
|
height: 1000,
|
||||||
}
|
}
|
||||||
const resizedDimensionsOne = getResizedDimensions(initialDimensionsOne)
|
const resizedDimensionsOne = getResizedDimensions(
|
||||||
|
initialDimensionsOne,
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
|
)
|
||||||
|
|
||||||
const initialDimensionsTwo = {
|
const initialDimensionsTwo = {
|
||||||
width: 1000,
|
width: 1000,
|
||||||
height: 1200,
|
height: 1200,
|
||||||
}
|
}
|
||||||
const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo)
|
const resizedDimensionsTwo = getResizedDimensions(
|
||||||
|
initialDimensionsTwo,
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
|
)
|
||||||
|
|
||||||
expect(resizedDimensionsOne).toEqual(initialDimensionsOne)
|
expect(resizedDimensionsOne).toEqual(initialDimensionsOne)
|
||||||
expect(resizedDimensionsTwo).toEqual(initialDimensionsTwo)
|
expect(resizedDimensionsTwo).toEqual(initialDimensionsTwo)
|
||||||
@@ -107,13 +112,19 @@ describe('downloadAndResize', () => {
|
|||||||
width: 3000,
|
width: 3000,
|
||||||
height: 1500,
|
height: 1500,
|
||||||
}
|
}
|
||||||
const resizedDimensionsOne = getResizedDimensions(initialDimensionsOne)
|
const resizedDimensionsOne = getResizedDimensions(
|
||||||
|
initialDimensionsOne,
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
|
)
|
||||||
|
|
||||||
const initialDimensionsTwo = {
|
const initialDimensionsTwo = {
|
||||||
width: 2000,
|
width: 2000,
|
||||||
height: 4000,
|
height: 4000,
|
||||||
}
|
}
|
||||||
const resizedDimensionsTwo = getResizedDimensions(initialDimensionsTwo)
|
const resizedDimensionsTwo = getResizedDimensions(
|
||||||
|
initialDimensionsTwo,
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB.maxDimension,
|
||||||
|
)
|
||||||
|
|
||||||
expect(resizedDimensionsOne).toEqual({
|
expect(resizedDimensionsOne).toEqual({
|
||||||
width: 2000,
|
width: 2000,
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {sha256} from 'js-sha256'
|
|||||||
import {CID} from 'multiformats/cid'
|
import {CID} from 'multiformats/cid'
|
||||||
import * as Hasher from 'multiformats/hashes/hasher'
|
import * as Hasher from 'multiformats/hashes/hasher'
|
||||||
|
|
||||||
|
import {IMAGE_SIZE_CONFIG_POSTS} from '#/lib/constants'
|
||||||
import {isNetworkError} from '#/lib/strings/errors'
|
import {isNetworkError} from '#/lib/strings/errors'
|
||||||
import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip'
|
import {shortenLinks, stripInvalidMentions} from '#/lib/strings/rich-text-manip'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
@@ -324,7 +325,10 @@ async function resolveMedia(
|
|||||||
const images: AppBskyEmbedImages.Image[] = await Promise.all(
|
const images: AppBskyEmbedImages.Image[] = await Promise.all(
|
||||||
imagesDraft.map(async (image, i) => {
|
imagesDraft.map(async (image, i) => {
|
||||||
logger.debug(`Compressing image #${i}`)
|
logger.debug(`Compressing image #${i}`)
|
||||||
const {path, width, height, mime} = await compressImage(image)
|
const {path, width, height, mime} = await compressImage(
|
||||||
|
image,
|
||||||
|
IMAGE_SIZE_CONFIG_POSTS,
|
||||||
|
)
|
||||||
logger.debug(`Uploading image #${i}`)
|
logger.debug(`Uploading image #${i}`)
|
||||||
const res = await uploadBlob(agent, path, mime)
|
const res = await uploadBlob(agent, path, mime)
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
} from '@atproto/api'
|
} from '@atproto/api'
|
||||||
import {AtUri} from '@atproto/api'
|
import {AtUri} from '@atproto/api'
|
||||||
|
|
||||||
import {DM_SERVICE_HEADERS, POST_IMG_MAX} from '#/lib/constants'
|
import {DM_SERVICE_HEADERS, IMAGE_SIZE_CONFIG_2K_1MB} from '#/lib/constants'
|
||||||
import {getLinkMeta, type LinkMeta} from '#/lib/link-meta/link-meta'
|
import {getLinkMeta, type LinkMeta} from '#/lib/link-meta/link-meta'
|
||||||
import {resolveShortLink} from '#/lib/link-meta/resolve-short-link'
|
import {resolveShortLink} from '#/lib/link-meta/resolve-short-link'
|
||||||
import {downloadAndResize} from '#/lib/media/manip'
|
import {downloadAndResize} from '#/lib/media/manip'
|
||||||
@@ -284,10 +284,7 @@ export async function imageToThumb(
|
|||||||
try {
|
try {
|
||||||
const img = await downloadAndResize({
|
const img = await downloadAndResize({
|
||||||
uri: imageUri,
|
uri: imageUri,
|
||||||
width: POST_IMG_MAX.width,
|
...IMAGE_SIZE_CONFIG_2K_1MB,
|
||||||
height: POST_IMG_MAX.height,
|
|
||||||
mode: 'contain',
|
|
||||||
maxSize: POST_IMG_MAX.size,
|
|
||||||
timeout: 15e3,
|
timeout: 15e3,
|
||||||
})
|
})
|
||||||
if (img) {
|
if (img) {
|
||||||
|
|||||||
@@ -97,10 +97,14 @@ export const STAGING_FEEDS = [
|
|||||||
`feedgen|${STAGING_DEFAULT_FEED('thevids')}`,
|
`feedgen|${STAGING_DEFAULT_FEED('thevids')}`,
|
||||||
]
|
]
|
||||||
|
|
||||||
export const POST_IMG_MAX = {
|
export const IMAGE_SIZE_CONFIG_POSTS = {
|
||||||
width: 2000,
|
maxDimension: 4000,
|
||||||
height: 2000,
|
maxSize: 2000000,
|
||||||
size: 1000000,
|
}
|
||||||
|
|
||||||
|
export const IMAGE_SIZE_CONFIG_2K_1MB = {
|
||||||
|
maxDimension: 2000,
|
||||||
|
maxSize: 1000000,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const STAGING_LINK_META_PROXY =
|
export const STAGING_LINK_META_PROXY =
|
||||||
|
|||||||
+16
-39
@@ -16,24 +16,21 @@ import {manipulateAsync, SaveFormat} from 'expo-image-manipulator'
|
|||||||
import * as MediaLibrary from 'expo-media-library'
|
import * as MediaLibrary from 'expo-media-library'
|
||||||
import * as Sharing from 'expo-sharing'
|
import * as Sharing from 'expo-sharing'
|
||||||
|
|
||||||
import {POST_IMG_MAX} from '#/lib/constants'
|
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
import {IS_ANDROID, IS_IOS} from '#/env'
|
import {IS_ANDROID, IS_IOS} from '#/env'
|
||||||
import {type PickerImage} from './picker.shared'
|
import {type PickerImage} from './picker.shared'
|
||||||
import {type Dimensions} from './types'
|
import {type Dimensions} from './types'
|
||||||
import {convertCdnPreset} from './util'
|
import {convertCdnPreset, getResizedDimensions} from './util'
|
||||||
|
|
||||||
export async function compressIfNeeded(
|
export async function compressIfNeeded(
|
||||||
img: PickerImage,
|
img: PickerImage,
|
||||||
maxSize: number = POST_IMG_MAX.size,
|
{maxDimension, maxSize}: {maxDimension: number; maxSize: number},
|
||||||
): Promise<PickerImage> {
|
): Promise<PickerImage> {
|
||||||
if (img.size < maxSize) {
|
if (img.size < maxSize) {
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
const resizedImage = await doResize(normalizePath(img.path), {
|
const resizedImage = await doResize(normalizePath(img.path), {
|
||||||
width: img.width,
|
maxDimension,
|
||||||
height: img.height,
|
|
||||||
mode: 'stretch',
|
|
||||||
maxSize,
|
maxSize,
|
||||||
})
|
})
|
||||||
const finalImageMovedPath = await moveToPermanentPath(
|
const finalImageMovedPath = await moveToPermanentPath(
|
||||||
@@ -49,9 +46,7 @@ export async function compressIfNeeded(
|
|||||||
|
|
||||||
export interface DownloadAndResizeOpts {
|
export interface DownloadAndResizeOpts {
|
||||||
uri: string
|
uri: string
|
||||||
width: number
|
maxDimension: number
|
||||||
height: number
|
|
||||||
mode: 'contain' | 'cover' | 'stretch'
|
|
||||||
maxSize: number
|
maxSize: number
|
||||||
timeout: number
|
timeout: number
|
||||||
}
|
}
|
||||||
@@ -67,7 +62,10 @@ export async function downloadAndResize(opts: DownloadAndResizeOpts) {
|
|||||||
const path = await downloadImage(opts.uri, String(uuid.v4()), opts.timeout)
|
const path = await downloadImage(opts.uri, String(uuid.v4()), opts.timeout)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await doResize(path, opts)
|
return await doResize(path, {
|
||||||
|
maxDimension: opts.maxDimension,
|
||||||
|
maxSize: opts.maxSize,
|
||||||
|
})
|
||||||
} finally {
|
} finally {
|
||||||
void safeDeleteAsync(path)
|
void safeDeleteAsync(path)
|
||||||
}
|
}
|
||||||
@@ -188,9 +186,7 @@ export function getImageDim(path: string): Promise<Dimensions> {
|
|||||||
// =
|
// =
|
||||||
|
|
||||||
interface DoResizeOpts {
|
interface DoResizeOpts {
|
||||||
width: number
|
maxDimension: number
|
||||||
height: number
|
|
||||||
mode: 'contain' | 'cover' | 'stretch'
|
|
||||||
maxSize: number
|
maxSize: number
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,10 +200,13 @@ async function doResize(
|
|||||||
// Performing an "empty" manipulation lets us get the dimensions of the original image. React Native's Image.getSize()
|
// Performing an "empty" manipulation lets us get the dimensions of the original image. React Native's Image.getSize()
|
||||||
// does not work for local files...
|
// does not work for local files...
|
||||||
const imageRes = await manipulateAsync(localUri, [], {})
|
const imageRes = await manipulateAsync(localUri, [], {})
|
||||||
const newDimensions = getResizedDimensions({
|
const newDimensions = getResizedDimensions(
|
||||||
width: imageRes.width,
|
{
|
||||||
height: imageRes.height,
|
width: imageRes.width,
|
||||||
})
|
height: imageRes.height,
|
||||||
|
},
|
||||||
|
opts.maxDimension,
|
||||||
|
)
|
||||||
|
|
||||||
let minQualityPercentage = 0
|
let minQualityPercentage = 0
|
||||||
let maxQualityPercentage = 101 // exclusive
|
let maxQualityPercentage = 101 // exclusive
|
||||||
@@ -388,28 +387,6 @@ async function withTempFile<T>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getResizedDimensions(originalDims: {
|
|
||||||
width: number
|
|
||||||
height: number
|
|
||||||
}) {
|
|
||||||
if (
|
|
||||||
originalDims.width <= POST_IMG_MAX.width &&
|
|
||||||
originalDims.height <= POST_IMG_MAX.height
|
|
||||||
) {
|
|
||||||
return originalDims
|
|
||||||
}
|
|
||||||
|
|
||||||
const ratio = Math.min(
|
|
||||||
POST_IMG_MAX.width / originalDims.width,
|
|
||||||
POST_IMG_MAX.height / originalDims.height,
|
|
||||||
)
|
|
||||||
|
|
||||||
return {
|
|
||||||
width: Math.round(originalDims.width * ratio),
|
|
||||||
height: Math.round(originalDims.height * ratio),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function downloadImage(uri: string, destName: string, timeout: number) {
|
async function downloadImage(uri: string, destName: string, timeout: number) {
|
||||||
// Download to a temp path first, then rename with the correct extension
|
// Download to a temp path first, then rename with the correct extension
|
||||||
// based on the response's mimeType.
|
// based on the response's mimeType.
|
||||||
|
|||||||
+22
-17
@@ -1,27 +1,28 @@
|
|||||||
import {type PickerImage} from './picker.shared'
|
import {type PickerImage} from './picker.shared'
|
||||||
import {type Dimensions} from './types'
|
import {type Dimensions} from './types'
|
||||||
import {blobToDataUri, convertCdnPreset, getDataUriSize} from './util'
|
import {
|
||||||
|
blobToDataUri,
|
||||||
|
convertCdnPreset,
|
||||||
|
getDataUriSize,
|
||||||
|
getResizedDimensions,
|
||||||
|
} from './util'
|
||||||
|
|
||||||
export async function compressIfNeeded(
|
export async function compressIfNeeded(
|
||||||
img: PickerImage,
|
img: PickerImage,
|
||||||
maxSize: number,
|
{maxDimension, maxSize}: {maxDimension: number; maxSize: number},
|
||||||
): Promise<PickerImage> {
|
): Promise<PickerImage> {
|
||||||
if (img.size < maxSize) {
|
if (img.size < maxSize) {
|
||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
return await doResize(img.path, {
|
return await doResize(img.path, {
|
||||||
width: img.width,
|
maxDimension,
|
||||||
height: img.height,
|
|
||||||
mode: 'stretch',
|
|
||||||
maxSize,
|
maxSize,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DownloadAndResizeOpts {
|
export interface DownloadAndResizeOpts {
|
||||||
uri: string
|
uri: string
|
||||||
width: number
|
maxDimension: number
|
||||||
height: number
|
|
||||||
mode: 'contain' | 'cover' | 'stretch'
|
|
||||||
maxSize: number
|
maxSize: number
|
||||||
timeout: number
|
timeout: number
|
||||||
}
|
}
|
||||||
@@ -34,7 +35,10 @@ export async function downloadAndResize(opts: DownloadAndResizeOpts) {
|
|||||||
clearTimeout(to)
|
clearTimeout(to)
|
||||||
|
|
||||||
const dataUri = await blobToDataUri(resBody)
|
const dataUri = await blobToDataUri(resBody)
|
||||||
return await doResize(dataUri, opts)
|
return await doResize(dataUri, {
|
||||||
|
maxDimension: opts.maxDimension,
|
||||||
|
maxSize: opts.maxSize,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function shareImageModal(_opts: {uri: string}) {
|
export async function shareImageModal(_opts: {uri: string}) {
|
||||||
@@ -70,9 +74,7 @@ export async function getImageDim(path: string): Promise<Dimensions> {
|
|||||||
// =
|
// =
|
||||||
|
|
||||||
interface DoResizeOpts {
|
interface DoResizeOpts {
|
||||||
width: number
|
maxDimension: number
|
||||||
height: number
|
|
||||||
mode: 'contain' | 'cover' | 'stretch'
|
|
||||||
maxSize: number
|
maxSize: number
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,6 +82,9 @@ async function doResize(
|
|||||||
dataUri: string,
|
dataUri: string,
|
||||||
opts: DoResizeOpts,
|
opts: DoResizeOpts,
|
||||||
): Promise<PickerImage> {
|
): Promise<PickerImage> {
|
||||||
|
const sourceDims = await getImageDim(dataUri)
|
||||||
|
const newDimensions = getResizedDimensions(sourceDims, opts.maxDimension)
|
||||||
|
|
||||||
let newDataUri
|
let newDataUri
|
||||||
|
|
||||||
let minQualityPercentage = 0
|
let minQualityPercentage = 0
|
||||||
@@ -90,10 +95,10 @@ async function doResize(
|
|||||||
(maxQualityPercentage + minQualityPercentage) / 2,
|
(maxQualityPercentage + minQualityPercentage) / 2,
|
||||||
)
|
)
|
||||||
const tempDataUri = await createResizedImage(dataUri, {
|
const tempDataUri = await createResizedImage(dataUri, {
|
||||||
width: opts.width,
|
width: newDimensions.width,
|
||||||
height: opts.height,
|
height: newDimensions.height,
|
||||||
quality: qualityPercentage / 100,
|
quality: qualityPercentage / 100,
|
||||||
mode: opts.mode,
|
mode: 'contain',
|
||||||
})
|
})
|
||||||
|
|
||||||
if (getDataUriSize(tempDataUri) < opts.maxSize) {
|
if (getDataUriSize(tempDataUri) < opts.maxSize) {
|
||||||
@@ -111,8 +116,8 @@ async function doResize(
|
|||||||
path: newDataUri,
|
path: newDataUri,
|
||||||
mime: 'image/jpeg',
|
mime: 'image/jpeg',
|
||||||
size: getDataUriSize(newDataUri),
|
size: getDataUriSize(newDataUri),
|
||||||
width: opts.width,
|
width: newDimensions.width,
|
||||||
height: opts.height,
|
height: newDimensions.height,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import ExpoImageCropTool, {
|
|||||||
type OpenCropperOptions,
|
type OpenCropperOptions,
|
||||||
} from '@bsky.app/expo-image-crop-tool'
|
} from '@bsky.app/expo-image-crop-tool'
|
||||||
|
|
||||||
|
import {IMAGE_SIZE_CONFIG_2K_1MB} from '#/lib/constants'
|
||||||
import {compressIfNeeded} from './manip'
|
import {compressIfNeeded} from './manip'
|
||||||
import {type PickerImage} from './picker.shared'
|
import {type PickerImage} from './picker.shared'
|
||||||
|
|
||||||
@@ -28,13 +29,16 @@ async function getFile() {
|
|||||||
throw new Error('Failed to get file info')
|
throw new Error('Failed to get file info')
|
||||||
}
|
}
|
||||||
|
|
||||||
return await compressIfNeeded({
|
return await compressIfNeeded(
|
||||||
path: file,
|
{
|
||||||
mime: 'image/jpeg',
|
path: file,
|
||||||
size: fileInfo.size,
|
mime: 'image/jpeg',
|
||||||
width: 4288,
|
size: fileInfo.size,
|
||||||
height: 2848,
|
width: 4288,
|
||||||
})
|
height: 2848,
|
||||||
|
},
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function openPicker(): Promise<PickerImage[]> {
|
export async function openPicker(): Promise<PickerImage[]> {
|
||||||
|
|||||||
@@ -2,6 +2,31 @@ export function extractDataUriMime(uri: string): string {
|
|||||||
return uri.substring(uri.indexOf(':') + 1, uri.indexOf(';'))
|
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
|
// Fairly accurate estimate that is more performant
|
||||||
// than decoding and checking length of URI
|
// than decoding and checking length of URI
|
||||||
export function getDataUriSize(uri: string): number {
|
export function getDataUriSize(uri: string): number {
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {msg} from '@lingui/core/macro'
|
|||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import {Trans} from '@lingui/react/macro'
|
import {Trans} from '@lingui/react/macro'
|
||||||
|
|
||||||
|
import {IMAGE_SIZE_CONFIG_2K_1MB} from '#/lib/constants'
|
||||||
import {usePhotoLibraryPermission} from '#/lib/hooks/usePermissions'
|
import {usePhotoLibraryPermission} from '#/lib/hooks/usePermissions'
|
||||||
import {compressIfNeeded} from '#/lib/media/manip'
|
import {compressIfNeeded} from '#/lib/media/manip'
|
||||||
import {openCropper} from '#/lib/media/picker'
|
import {openCropper} from '#/lib/media/picker'
|
||||||
@@ -212,7 +213,7 @@ export function StepProfile() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
image = await compressIfNeeded(image, 1000000)
|
image = await compressIfNeeded(image, IMAGE_SIZE_CONFIG_2K_1MB)
|
||||||
|
|
||||||
// If we are on mobile, prefetching the image will load the image into memory before we try and display it,
|
// If we are on mobile, prefetching the image will load the image into memory before we try and display it,
|
||||||
// stopping any brief flickers.
|
// stopping any brief flickers.
|
||||||
|
|||||||
+16
-6
@@ -201,12 +201,17 @@ export function resetImageManipulation(
|
|||||||
return img
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function compressImage(img: ComposerImage): Promise<PickerImage> {
|
export async function compressImage(
|
||||||
|
img: ComposerImage,
|
||||||
|
{maxDimension, maxSize}: {maxDimension: number; maxSize: number},
|
||||||
|
): Promise<PickerImage> {
|
||||||
const source = img.transformed || img.source
|
const source = img.transformed || img.source
|
||||||
|
|
||||||
let attempts = 0
|
let attempts = 0
|
||||||
let maxDimension = 4000
|
// Seeded from `maxDimension` but shrunk per attempt below, so keep the
|
||||||
let maxBytes = 2000000
|
// passed-in value pristine.
|
||||||
|
let currentDimension = maxDimension
|
||||||
|
const maxBytes = maxSize
|
||||||
|
|
||||||
let minQualityPercentage = 0
|
let minQualityPercentage = 0
|
||||||
let maxQualityPercentage = 101 // exclusive
|
let maxQualityPercentage = 101 // exclusive
|
||||||
@@ -215,7 +220,11 @@ export async function compressImage(img: ComposerImage): Promise<PickerImage> {
|
|||||||
while (maxQualityPercentage - minQualityPercentage > 1) {
|
while (maxQualityPercentage - minQualityPercentage > 1) {
|
||||||
if (attempts >= 4) break
|
if (attempts >= 4) break
|
||||||
|
|
||||||
const [w, h] = containImageRes(source.width, source.height, maxDimension)
|
const [w, h] = containImageRes(
|
||||||
|
source.width,
|
||||||
|
source.height,
|
||||||
|
currentDimension,
|
||||||
|
)
|
||||||
const qualityPercentage = Math.round(
|
const qualityPercentage = Math.round(
|
||||||
(maxQualityPercentage + minQualityPercentage) / 2,
|
(maxQualityPercentage + minQualityPercentage) / 2,
|
||||||
)
|
)
|
||||||
@@ -230,8 +239,9 @@ export async function compressImage(img: ComposerImage): Promise<PickerImage> {
|
|||||||
minQualityPercentage = 0
|
minQualityPercentage = 0
|
||||||
maxQualityPercentage = 101
|
maxQualityPercentage = 101
|
||||||
attempts++
|
attempts++
|
||||||
// 4000px → 3200px → 2560px → 2048px → ~1638px
|
// max.width → 0.8× → 0.64× → 0.512× → ~0.41×
|
||||||
maxDimension = Math.floor(maxDimension * 0.8)
|
// e.g. 4000px → 3200px → 2560px → 2048px → ~1638px
|
||||||
|
currentDimension = Math.floor(currentDimension * 0.8)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import * as MediaLibrary from 'expo-media-library'
|
|||||||
import {msg} from '@lingui/core/macro'
|
import {msg} from '@lingui/core/macro'
|
||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
|
|
||||||
import {POST_IMG_MAX} from '#/lib/constants'
|
|
||||||
import {useCameraPermission} from '#/lib/hooks/usePermissions'
|
import {useCameraPermission} from '#/lib/hooks/usePermissions'
|
||||||
import {openCamera} from '#/lib/media/picker'
|
import {openCamera} from '#/lib/media/picker'
|
||||||
import {logger} from '#/logger'
|
import {logger} from '#/logger'
|
||||||
@@ -35,7 +34,7 @@ export function OpenCameraBtn({disabled, onAdd}: Props) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const img = await openCamera({
|
const img = await openCamera({
|
||||||
aspect: [POST_IMG_MAX.width, POST_IMG_MAX.height],
|
aspect: [1, 1],
|
||||||
})
|
})
|
||||||
|
|
||||||
// If we don't have permissions it's fine, we just wont save it. The post itself will still have access to
|
// If we don't have permissions it's fine, we just wont save it. The post itself will still have access to
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import {type PasteEventPayload, TextInputWrapper} from 'expo-paste-input'
|
|||||||
import {AppBskyRichtextFacet, RichText} from '@atproto/api'
|
import {AppBskyRichtextFacet, RichText} from '@atproto/api'
|
||||||
import {useLingui} from '@lingui/react/macro'
|
import {useLingui} from '@lingui/react/macro'
|
||||||
|
|
||||||
import {POST_IMG_MAX} from '#/lib/constants'
|
import {IMAGE_SIZE_CONFIG_POSTS} from '#/lib/constants'
|
||||||
import {downloadAndResize} from '#/lib/media/manip'
|
import {downloadAndResize} from '#/lib/media/manip'
|
||||||
import {isUriImage} from '#/lib/media/util'
|
import {isUriImage} from '#/lib/media/util'
|
||||||
import {getMentionAt, insertMentionAt} from '#/lib/strings/mention-manip'
|
import {getMentionAt, insertMentionAt} from '#/lib/strings/mention-manip'
|
||||||
@@ -93,10 +93,7 @@ export function TextInput({
|
|||||||
if (isUriImage(feature.uri)) {
|
if (isUriImage(feature.uri)) {
|
||||||
const res = await downloadAndResize({
|
const res = await downloadAndResize({
|
||||||
uri: feature.uri,
|
uri: feature.uri,
|
||||||
width: POST_IMG_MAX.width,
|
...IMAGE_SIZE_CONFIG_POSTS,
|
||||||
height: POST_IMG_MAX.height,
|
|
||||||
mode: 'contain',
|
|
||||||
maxSize: POST_IMG_MAX.size,
|
|
||||||
timeout: 15e3,
|
timeout: 15e3,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import {useLingui} from '@lingui/react'
|
|||||||
import {Trans} from '@lingui/react/macro'
|
import {Trans} from '@lingui/react/macro'
|
||||||
import {useQueryClient} from '@tanstack/react-query'
|
import {useQueryClient} from '@tanstack/react-query'
|
||||||
|
|
||||||
|
import {IMAGE_SIZE_CONFIG_2K_1MB} from '#/lib/constants'
|
||||||
import {useHaptics} from '#/lib/haptics'
|
import {useHaptics} from '#/lib/haptics'
|
||||||
import {
|
import {
|
||||||
useCameraPermission,
|
useCameraPermission,
|
||||||
@@ -395,6 +396,7 @@ let EditableUserAvatar = ({
|
|||||||
await openCamera({
|
await openCamera({
|
||||||
aspect: [1, 1],
|
aspect: [1, 1],
|
||||||
}),
|
}),
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}, [onSelectNewAvatar, requestCameraAccessIfNeeded])
|
}, [onSelectNewAvatar, requestCameraAccessIfNeeded])
|
||||||
@@ -423,6 +425,7 @@ let EditableUserAvatar = ({
|
|||||||
shape: circular ? 'circle' : 'rectangle',
|
shape: circular ? 'circle' : 'rectangle',
|
||||||
aspectRatio: 1,
|
aspectRatio: 1,
|
||||||
}),
|
}),
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
@@ -449,7 +452,7 @@ let EditableUserAvatar = ({
|
|||||||
|
|
||||||
const onChangeEditImage = useCallback(
|
const onChangeEditImage = useCallback(
|
||||||
async (image: ComposerImage) => {
|
async (image: ComposerImage) => {
|
||||||
const compressed = await compressImage(image)
|
const compressed = await compressImage(image, IMAGE_SIZE_CONFIG_2K_1MB)
|
||||||
onSelectNewAvatar(compressed)
|
onSelectNewAvatar(compressed)
|
||||||
},
|
},
|
||||||
[onSelectNewAvatar],
|
[onSelectNewAvatar],
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {msg} from '@lingui/core/macro'
|
|||||||
import {useLingui} from '@lingui/react'
|
import {useLingui} from '@lingui/react'
|
||||||
import {Trans} from '@lingui/react/macro'
|
import {Trans} from '@lingui/react/macro'
|
||||||
|
|
||||||
|
import {IMAGE_SIZE_CONFIG_2K_1MB} from '#/lib/constants'
|
||||||
import {
|
import {
|
||||||
useCameraPermission,
|
useCameraPermission,
|
||||||
usePhotoLibraryPermission,
|
usePhotoLibraryPermission,
|
||||||
@@ -62,6 +63,7 @@ export function UserBanner({
|
|||||||
await openCamera({
|
await openCamera({
|
||||||
aspect: [3, 1],
|
aspect: [3, 1],
|
||||||
}),
|
}),
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}, [onSelectNewBanner, requestCameraAccessIfNeeded])
|
}, [onSelectNewBanner, requestCameraAccessIfNeeded])
|
||||||
@@ -83,6 +85,7 @@ export function UserBanner({
|
|||||||
imageUri: items[0].path,
|
imageUri: items[0].path,
|
||||||
aspectRatio: 3 / 1,
|
aspectRatio: 3 / 1,
|
||||||
}),
|
}),
|
||||||
|
IMAGE_SIZE_CONFIG_2K_1MB,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
@@ -108,7 +111,7 @@ export function UserBanner({
|
|||||||
|
|
||||||
const onChangeEditImage = useCallback(
|
const onChangeEditImage = useCallback(
|
||||||
async (image: ComposerImage) => {
|
async (image: ComposerImage) => {
|
||||||
const compressed = await compressImage(image)
|
const compressed = await compressImage(image, IMAGE_SIZE_CONFIG_2K_1MB)
|
||||||
onSelectNewBanner?.(compressed)
|
onSelectNewBanner?.(compressed)
|
||||||
},
|
},
|
||||||
[onSelectNewBanner],
|
[onSelectNewBanner],
|
||||||
|
|||||||
Reference in New Issue
Block a user