From 8823a8009b18950d5be2682a86cfcf0c7014a068 Mon Sep 17 00:00:00 2001 From: Mathieu Acthernoene Date: Tue, 12 May 2026 11:03:26 -0700 Subject: [PATCH] Fix Image.getSize returning downsampled dimensions on Android (#56736) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: `Image.getSize` and `getSizeWithHeaders` call Fresco's `fetchDecodedImage`, which returns a `CloseableImage` whose width / height reflect the bitmap after Fresco's automatic downsampling (typically capped at the screen size). For sources larger than the screen this returned the wrong dimensions. ex: a 4000x3000 image on a 1920x1080 device came back as 1920x1440. Switch to `fetchEncodedImage` and read the dimensions from the parsed image metadata (JPEG / PNG / WebP / HEIF headers), so the values are the true source dimensions. Swap width / height for 90°/270° EXIF rotation to match the iOS behavior in `RCTImageLoader`. Fixes https://github.com/facebook/react-native/issues/33498 Closes facebook/fresco#2236 ## Changelog: [ANDROID] [FIXED] - `Image.getSize` and `Image.getSizeWithHeaders` now return the true source dimensions instead of Fresco's downsampled values Pull Request resolved: https://github.com/facebook/react-native/pull/56736 Test Plan: In the RNTester app, add: ```ts useEffect(() => { Image.getSize( 'https://images.pexels.com/photos/842711/pexels-photo-842711.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2', (width, height) => { console.log({width, height}); }, ); }, []); ``` ### Before before ### After after Reviewed By: javache Differential Revision: D104533593 Pulled By: Abbondanzo fbshipit-source-id: f087d528474c40cffbf24eb1e3958b4b18fb118e --- .../react/modules/image/ImageLoaderModule.kt | 125 +++++++++--------- 1 file changed, 61 insertions(+), 64 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt index d76da5ffc60..509efc1f7b0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/image/ImageLoaderModule.kt @@ -7,17 +7,20 @@ package com.facebook.react.modules.image +import android.media.ExifInterface import android.net.Uri import android.util.SparseArray import com.facebook.common.executors.CallerThreadExecutor +import com.facebook.common.memory.PooledByteBuffer import com.facebook.common.references.CloseableReference import com.facebook.datasource.BaseDataSubscriber import com.facebook.datasource.DataSource import com.facebook.datasource.DataSubscriber import com.facebook.drawee.backends.pipeline.Fresco import com.facebook.fbreact.specs.NativeImageLoaderAndroidSpec +import com.facebook.imagepipeline.common.RotationOptions import com.facebook.imagepipeline.core.ImagePipeline -import com.facebook.imagepipeline.image.CloseableImage +import com.facebook.imagepipeline.image.EncodedImage import com.facebook.imagepipeline.request.ImageRequest import com.facebook.imagepipeline.request.ImageRequestBuilder import com.facebook.react.bridge.GuardedAsyncTask @@ -82,39 +85,13 @@ internal class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventL return } val source = ImageSource(reactApplicationContext, uriString) - val request: ImageRequest = ImageRequestBuilder.newBuilderWithSource(source.uri).build() - val dataSource: DataSource> = - this.imagePipeline.fetchDecodedImage(request, this.callerContext) - val dataSubscriber: DataSubscriber> = - object : BaseDataSubscriber>() { - override fun onNewResultImpl(dataSource: DataSource>) { - if (!dataSource.isFinished) { - return - } - val ref = dataSource.result - if (ref != null) { - try { - val image: CloseableImage = ref.get() - val sizes = buildReadableMap { - put("width", image.width) - put("height", image.height) - } - promise.resolve(sizes) - } catch (e: Exception) { - promise.reject(ERROR_GET_SIZE_FAILURE, e) - } finally { - CloseableReference.closeSafely(ref) - } - } else { - promise.reject(ERROR_GET_SIZE_FAILURE, "Failed to get the size of the image") - } - } - - override fun onFailureImpl(dataSource: DataSource>) { - promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.failureCause) - } - } - dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance()) + val request: ImageRequest = + ImageRequestBuilder.newBuilderWithSource(source.uri) + .setRotationOptions(RotationOptions.disableRotation()) + .build() + val dataSource: DataSource> = + this.imagePipeline.fetchEncodedImage(request, this.callerContext) + dataSource.subscribe(createSizeSubscriber(promise), CallerThreadExecutor.getInstance()) } /** @@ -134,41 +111,61 @@ internal class ImageLoaderModule : NativeImageLoaderAndroidSpec, LifecycleEventL val source = ImageSource(reactApplicationContext, uriString) val imageRequestBuilder: ImageRequestBuilder = ImageRequestBuilder.newBuilderWithSource(source.uri) + .setRotationOptions(RotationOptions.disableRotation()) val request: ImageRequest = ReactNetworkImageRequest.fromBuilderWithHeaders(imageRequestBuilder, headers) - val dataSource: DataSource> = - this.imagePipeline.fetchDecodedImage(request, this.callerContext) - val dataSubscriber: DataSubscriber> = - object : BaseDataSubscriber>() { - override fun onNewResultImpl(dataSource: DataSource>) { - if (!dataSource.isFinished) { - return - } - val ref = dataSource.result - if (ref != null) { - try { - val image: CloseableImage = ref.get() - val sizes = buildReadableMap { - put("width", image.width) - put("height", image.height) - } - promise.resolve(sizes) - } catch (e: Exception) { - promise.reject(ERROR_GET_SIZE_FAILURE, e) - } finally { - CloseableReference.closeSafely(ref) - } - } else { - promise.reject(ERROR_GET_SIZE_FAILURE, "Failed to get the size of the image") - } - } + val dataSource: DataSource> = + this.imagePipeline.fetchEncodedImage(request, this.callerContext) + dataSource.subscribe(createSizeSubscriber(promise), CallerThreadExecutor.getInstance()) + } - override fun onFailureImpl(dataSource: DataSource>) { - promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.failureCause) + private fun createSizeSubscriber( + promise: Promise + ): DataSubscriber> = + object : BaseDataSubscriber>() { + override fun onNewResultImpl(dataSource: DataSource>) { + if (!dataSource.isFinished) { + return + } + val ref = dataSource.result + if (ref != null) { + var encodedImage: EncodedImage? = null + try { + encodedImage = EncodedImage(ref) + // Swap width and height when the image's EXIF orientation swaps the X/Y axes + // (90°/270° rotations, or transpose/transverse), so the values reflect the + // visible dimensions, matching iOS behavior. + val rotated = + encodedImage.rotationAngle == 90 || + encodedImage.rotationAngle == 270 || + encodedImage.exifOrientation == ExifInterface.ORIENTATION_TRANSPOSE || + encodedImage.exifOrientation == ExifInterface.ORIENTATION_TRANSVERSE + val width = if (rotated) encodedImage.height else encodedImage.width + val height = if (rotated) encodedImage.width else encodedImage.height + if (width < 0 || height < 0) { + promise.reject(ERROR_GET_SIZE_FAILURE, "Failed to get the size of the image") + return + } + val sizes = buildReadableMap { + put("width", width) + put("height", height) + } + promise.resolve(sizes) + } catch (e: Exception) { + promise.reject(ERROR_GET_SIZE_FAILURE, e) + } finally { + encodedImage?.close() + CloseableReference.closeSafely(ref) + } + } else { + promise.reject(ERROR_GET_SIZE_FAILURE, "Failed to get the size of the image") } } - dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance()) - } + + override fun onFailureImpl(dataSource: DataSource>) { + promise.reject(ERROR_GET_SIZE_FAILURE, dataSource.failureCause) + } + } /** * Prefetches the given image to the Fresco image disk cache.