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

Can't load PNG, JPEG Images with scrimage-webp #245

Closed
mr-w1lde opened this issue Feb 26, 2022 · 10 comments
Closed

Can't load PNG, JPEG Images with scrimage-webp #245

mr-w1lde opened this issue Feb 26, 2022 · 10 comments

Comments

@mr-w1lde
Copy link

mr-w1lde commented Feb 26, 2022

I need a scrimage-webp lib for compression, but if I use it, I can't load png or jpg files via ImmutableImage.loader().fromBytes(receiveBytes)

The error I've got
Screenshot 2022-02-23 at 21 33 25

But if I'll remove the scrimage-webp, when everything is working fine

scrimage version 4.0.27.
OS: MacOS 11.6.2
AdoptOpenJDK 11

@sksamuel
Copy link
Owner

sksamuel commented Feb 26, 2022 via email

@sksamuel
Copy link
Owner

sksamuel commented Feb 26, 2022 via email

@mr-w1lde
Copy link
Author

mr-w1lde commented Feb 26, 2022

@sksamuel
Okay... I'm using ktor to get an image and seding a POST request via Postman

install(Routing) {
    post("api/v1/media") {
        val multipartData = call.receiveMultipart()
        val logger = LoggerFactory.getLogger(this::class.java)

        when (val part = multipartData.readPart()) {
            is PartData.FileItem -> {
                val receiveBytes = part.streamProvider().readBytes()
                val format = FormatDetector.detect(receiveBytes)
                logger.info("Image format - {}", format.get().name)
                val originalImage = ImmutableImage.loader().fromBytes(receiveBytes)

                val meta = ImageMetadata.fromImage(originalImage)

                call.respondText(meta.tags().toList())
            }
        }
    }
}

it doesn't work with any png or jpg image.

2021-04-13 23 16 43

sksamuel added a commit that referenced this issue Feb 26, 2022
@sksamuel
Copy link
Owner

This isn't a stand alone test.

You say the issue is with scrimage, but in order for me to test this I have to setup an entire ktor build, and then use an external http client in order to send images in.

What I've done instead is add a test to the webp module that loads the above image using the immutable image loader.
Firstly with the defaults, secondly by specifying webp first.

https://github.com/sksamuel/scrimage/blob/master/scrimage-webp/src/test/kotlin/com/sksamuel/scrimage/webp/Issue245Test.kt

It passes fine. So I guess the issue is external. But if you believe the issue is with the webp handler, then fork this repo, and make that Issue245 test fail like what your are seeing, then I should be able to fix whatever is up.

One thing you can try is copying the input stream to a byte array, because it's probably being scanned twice.

@mr-w1lde
Copy link
Author

Okay, I will try. Thanks

@sksamuel
Copy link
Owner

I know what you might be seeing.
Are you making a fat jar ?

@mr-w1lde
Copy link
Author

yes, using shadowJar

@sksamuel
Copy link
Owner

You need to merge your service files inside maven or gradle when you build a shadow jar otherwise Java's service loader framework can't work properly as the files overwrite each other inside the final jar. This would cause a problem in any library that relies on service files.

If you are using shadow jar in gradle see https://imperceptiblethoughts.com/shadow/configuration/merging/#merging-service-descriptor-files

Or you can setup the image loader like this (be on the latest version) where you specify the readers manually:

ImmutableImage.loader()
.withImageReaders(listOf(ImageIOReader(), WebpImageReader()))
.fromBytes(...)

@mr-w1lde
Copy link
Author

@sksamuel the second part is working for me. Thanks for your help!

@sksamuel
Copy link
Owner

We got there in the end :)

an-tex pushed a commit to an-tex/scrimage that referenced this issue Mar 13, 2024
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

2 participants