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

Corrupt JPEG data #178

Open
failingbirthrate opened this issue Feb 23, 2022 · 1 comment
Open

Corrupt JPEG data #178

failingbirthrate opened this issue Feb 23, 2022 · 1 comment

Comments

@failingbirthrate
Copy link

failingbirthrate commented Feb 23, 2022

I am receiving "corrupt JPEG" errors when I POST a base64 payload to the server. The bizarre thing is that I can paste the same base64 string into a script, performing the same logic and it works just fine.

Here is my code:

            preg_match('/(data:.*;base64,)(.*)/', $imageBase64, $output_array);

            if (isset($output_array[1]) && isset($output_array[2])) {

                ini_set("gd.jpeg_ignore_warning", 1);
                $imageType = $output_array[1]; // data:image/jpeg;base64,
//                $output_array[2]; // base64 data

// fails here
                $image = ImageResize::createFromString(base64_decode($output_array[2]));
                $w = $image->getSourceWidth();
                $h = $image->getSourceHeight();

                if ($w > 100 || $h > 100) {
                    $image->resize(100, 100);
                }

                $resized = base64_encode( $image->getImageAsString() );
            }

So I am stripping out data:image/jpeg;base64, before processing, then I receive this error.

PHP Warning: getimagesize(): Corrupt JPEG data: 214 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: getimagesize(): Corrupt JPEG data: 214 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: getimagesize(): Corrupt JPEG data: 7 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: getimagesize(): Corrupt JPEG data: 7 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: getimagesize(): Corrupt JPEG data: 103 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: getimagesize(): Corrupt JPEG data: 103 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: getimagesize(): Corrupt JPEG data: 327 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: getimagesize(): Corrupt JPEG data: 327 extraneous bytes before marker in /H:/path/ImageResize.php on line 126
PHP Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: Unsupported marker type 0x68 in /H:/path/ImageResize.php on line 179
PHP Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error: Unsupported marker type 0x68 in /H:/path/ImageResize.php on line 179
PHP Warning: imagecreatefromjpeg(): "data://application/octet-stream;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wB.....9KmcrRsRJpH/9g==" is not a valid JPEG file in /H:/path/ImageResize.php on line 179
PHP Warning: imagecreatefromjpeg(): "data://application/octet-stream;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wB.....9KmcrRsRJpH/9g==" is not a valid JPEG file in /H:/path/ImageResize.php on line 179
PHP Fatal error: Uncaught TypeError: imagesx(): Argument #1 ($image) must be of type GdImage, bool given in /H:/path/ImageResize.php on line 149
PHP Fatal error: Uncaught TypeError: imagesx(): Argument #1 ($image) must be of type GdImage, bool given in /H:/path/ImageResize.php on line 149

The length of the base64 payload is the same if I copy directly into a script or if I POST it, so it isn't a truncation issue, and I have no problems creating image from a string and resizing when the base64 is pasted in. Any ideas of what could be going on?

Server stats

PHP Version 8.0.13

PHP API | 20200930
PHP Extension	| 20200930

[gd]
GD Version | bundled (2.1.0 compatible)
FreeType Support | enabled
FreeType Linkage | with freetype
FreeType Version | 2.9.1
GIF Read Support | enabled
GIF Create Support | enabled
JPEG Support | enabled
libJPEG Version | 9 compatible
PNG Support | enabled
libPNG Version | 1.6.34
WBMP Support | enabled
XPM Support | enabled
libXpm Version | 30512
XBM Support | enabled
WebP Support | enabled
BMP Support | enabled
TGA Read Support | enabled
@peter279k
Copy link
Contributor

It seems to be the internal PHP error and it's same as this bug.

According to the error log, the getimagesize cannot identify your encoded string and the imagecreatefromjpeg function cannot create the correct binary image resource with specific argument.

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