numandev1/react-native-compressor

Unwanted image rotation on newer Androids after compression

MrTurtlev2 opened this issue · 4 comments

Android >= 12
ReactNative: 0.68.0
react-native-compressor: 1.8.24

After compressing the image manually, it rotates by 90 degrees. However, the automatic compression method works correctly. This issue was also present on iOS, but it was resolved with version 1.8.24.

👋 @MrTurtlev2
Thanks for opening your issue here! If you find this package useful hit the star🌟!

I have discovered a temporary solution for this problem.

Navigate to the "ImageCompressor.ks" file located at:
node-modules/react-native-compressor/android/scr/main/java/com/reactnativecompressor/Image

Then, replace the current implementation of the "manualCompressImage" function with the following code:

fun manualCompressImage(imagePath: String?, options: ImageCompressorOptions, reactContext: ReactApplicationContext?): String? {
    val image = if (options.input === ImageCompressorOptions.InputType.base64) decodeImage(imagePath) else loadImage(imagePath)
    val resizedImage = resize(image, options.maxWidth, options.maxHeight)
    val isBase64 = options.returnableOutputType === ImageCompressorOptions.ReturnableOutputType.base64
    val uri = Uri.parse(imagePath)
    val imagePathNew = uri.path
    var scaledBitmap: Bitmap? = correctImageOrientation(resizedImage, imagePathNew)
    val imageDataByteArrayOutputStream = compress(scaledBitmap, options.output, options.quality, options.disablePngTransparency)
    val compressedImagePath = encodeImage(imageDataByteArrayOutputStream, isBase64, options.output.toString(), imagePath, reactContext)
    if (isCompressedSizeLessThanActualFile(imagePath!!, compressedImagePath)) {
        return compressedImagePath
    } else {
        MediaCache.deleteFile(compressedImagePath!!)
        return slashifyFilePath(imagePath)
    }
} 

after updating the code just patch package with patch-package

yarn patch-package react-native-compressor