Laravel-medialibrary: Unable to decode image from file error, introduced since 6.2.1

Created on 24 Oct 2017  路  16Comments  路  Source: spatie/laravel-medialibrary

This change:

https://github.com/spatie/laravel-medialibrary/compare/6.2.0...6.2.1

.. broke media conversions for me as I updated spatie/laravel-medialibrary from a version before 6.2.1.

I get this error:

Intervention\Image\Exception\NotReadableException
Unable to decode image from file (/tmp/GlideBKMlDI).

When I look at the temp file, I see that it is consistently shorter than the original file.

I was able to fix it by changing be fread() to fgets(). Why it makes a difference I have no clue. It doesn't seem right to use fgets() for binary data :-)

The environment is alpine linux 3.6 and php 7.1 with nginx and fpm.

Most helpful comment

Having same issue Too T.T
when using s3 driver, same error as mentioned above.

on Windows + PHP 7.1.2

All 16 comments

That's really strange... I tried to reproduce this issue but couldn't seem to make anything break.

Does this happen with all images? Can you try this with a regular text file?

It happened with a 616696 bytes png file.

Is this still a problem in the latest version of the package?

@freekmurze unsure about @oad-dwarf, but I've been able to replicate this when using s3.

Changing from fread() to fgets() resolved the issue (Windows).

Closing because we can't reproduce it.

@oad-dwarf Did you figure this one out? I'm getting the same issue. Also Alpine-Linux & S3. (in a K8 setup)
Seems to be all png's.

jpg's are also failing for me but with no error. The POST request just sort of stops (no response). Its weird.

I have been getting this error on all PNG images when uploading to s3. Very weird. Uploads are being performed with addMediaFromBase64. And yes, changing from fread() to fgets() resolves it for me.

Environment: Windows
Php version: Php 7.1
Server: Nginx

I think its an Alpine issue. We swapped to an Ubuntu image(no other changes) and all working perfectly now.

@tobz-nz No I still have my patch in place. Haven't tested since then if the problem is still there. Perhaps I'll check it again when we switch to Alpine 3.8 and PHP 7.2

Having same issue, the original image is uploaded successfully but conversions simply fails for all png files when using s3 driver, same error as mentioned above.

on Windows + PHP 7.2.8

Having same issue Too T.T
when using s3 driver, same error as mentioned above.

on Windows + PHP 7.1.2

I receive also the error on S3 with any image. Will check further to see why is happening

Hi, i was testing for a whole day and apparently this error happens only when the filesystem is set with S3, i tried with the "media" filesystem (as you can see on the readme of laravel-medialibrary) and there all worked perfectly.

So i still dont understand why the error happens but the solution i did was to work with the media filesystem and run a command that moves all the files from media to the s3 filesystems.

And worked great, you can see the code for the command on here to implement it yourself: https://gist.github.com/skalero01/fffb760e818c5a6b1adb899dfd534c58

Same problem
CentOS Linux release 7.6.1810 (Core)
PHP 7.2.20
Web Server: Litespeed

Same problem on Windows. Solution is to directly modify file vendor/spatie/laravel-medialibrary/src/Filesystem/Filesystem.php. Replace fread with fgets as such:

    public function copyFromMediaLibrary(Media $media, string $targetFile): string
    {
        touch($targetFile);

        $stream = $this->getStream($media);

        $targetFileStream = fopen($targetFile, 'a');

        while (! feof($stream)) {
            //$chunk = fread($stream, 1024);
            $chunk = fgets($stream, 1024);
            fwrite($targetFileStream, $chunk);
        }

        fclose($stream);

        fclose($targetFileStream);

        return $targetFile;
    }

This is still an issue for me, which is totally fixable by changing fread to fgets. Will create a PR

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ideadx picture ideadx  路  4Comments

mohammad6006 picture mohammad6006  路  4Comments

intrepidws picture intrepidws  路  3Comments

stayallive picture stayallive  路  4Comments

brendt picture brendt  路  4Comments