Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwanted image rotation on newer Androids after compression #280

Open
MrTurtlev2 opened this issue Mar 27, 2024 · 2 comments
Open

Unwanted image rotation on newer Androids after compression #280

MrTurtlev2 opened this issue Mar 27, 2024 · 2 comments

Comments

@MrTurtlev2
Copy link

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.

Copy link

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

@MrTurtlev2
Copy link
Author

MrTurtlev2 commented Mar 27, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant