Skip to content

Fix: bitmaps with BITMAPV3INFOHEADER cannot be imported #11027

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

Draft
wants to merge 2 commits into
base: PHP-8.1
Choose a base branch
from

Conversation

Gymnasiast
Copy link

@Gymnasiast Gymnasiast commented Apr 6, 2023

These files are reportedly written by Photoshop and GIMP and based on a specification by Microsoft. More information can be found here:

Here is a file using this header: TESTRED1.bmp.zip
Besides the uncommon header, it also writes bitfields, which aren’t necessary at 24 or 32 bits.

This file can be opened correctly by GIMP, Eye of Gnome and Chromium.

I used the following script to test:

<?php
$file = 'TESTRED1.bmp';

$gd = imagecreatefrombmp($file);
var_dump($gd);

Output without the fix:

PHP Warning:  imagecreatefrombmp(): "/home/michael/Bureaublad/TESTRED1.bmp" is not a valid BMP file in /home/michael/testgd.php on line 4
PHP Stack trace:
PHP   1. {main}() /home/michael/testgd.php:0
PHP   2. imagecreatefrombmp($filename = '/home/michael/Bureaublad/TESTRED1.bmp') /home/michael/testgd.php:4
/home/michael/testgd.php:5:
bool(false)

Output with the fix:

object(GdImage)#1 (0) {
}

Additionally, this file now also works correctly whereas it did not before. It contains a regular V5 header, but it also contains bitfields, which as I mentioned as unnecessary at 24 or 32 bits: TESTRED2.bmp.zip

This is my first time contributing to PHP, I hope I followed all steps correctly.

@Gymnasiast
Copy link
Author

Gymnasiast commented Apr 6, 2023

Hm, looking at https://github.com/php/php-src/blob/ede8adb2e2013eafe67edb64fc8d5fa62f40cb7d/CONTRIBUTING.md#pull-requests , it seems like I should have created this against the PHP-8.1 branch, is that correct? If so, I will try and do that in the coming days.

@Girgias
Copy link
Member

Girgias commented Apr 7, 2023

Hm, looking at https://github.com/php/php-src/blob/ede8adb2e2013eafe67edb64fc8d5fa62f40cb7d/CONTRIBUTING.md#pull-requests , it seems like I should have created this against the PHP-8.1 branch, is that correct? If so, I will try and do that in the coming days.

For bug fixes yes, please target the lowest maintained branch (8.1) the merging up into 8.2 and master will be taken care off when we merge this.

Ideally, could you also add a regression test with a small image so that we don't break this again in the future.

Now, I am surprised that the failure without the fix is telling you the function is undefined, that looks wrong, are you sure you tested with a build that has support for this extension enabled?

@Gymnasiast
Copy link
Author

Now, I am surprised that the failure without the fix is telling you the function is undefined, that looks wrong, are you sure you tested with a build that has support for this extension enabled?

Initially I built without GD by accident, but I quickly noticed that and fixed it. I just copied the wrong output to my post. I had a few too many windows open :')

I will retarget the PHP-8.1 branch and also re-run my tests again, and update the output in my first post accordingly. I will also try and see if I can add a test - the image I used in the first post is only 16×21 and perfect for the purpose.

@Gymnasiast Gymnasiast changed the base branch from master to PHP-8.1 April 7, 2023 17:10
@Gymnasiast
Copy link
Author

Gymnasiast commented Apr 7, 2023

I think that should address everything. I included both test files that are in the first post. As both of these should evaluate to the exact same image (as they only differ in terms of their header), I used a single image for the expected result of both. I ran the test suite locally and it does list the new ones:

PASS imagecreatefrombmp() - test image with BITMAPV3INFOHEADER and (unnecessary) bitfields [ext/gd/tests/imagecreatefrombmp-v3info-32bpp-bitfields-11027.phpt] 
PASS imagecreatefrombmp() - test image with BITMAPV5HEADER, 32bpp and (unnecessary) bitfields [ext/gd/tests/imagecreatefrombmp-v5-32bpp-bitfields-11027.phpt] 
@cmb69
Copy link
Member

cmb69 commented Jul 18, 2024

Sorry, I'm a "bit" late, but it seems to me that this is change which is not in https://github/libgd/libgd, so it should be suggested to them first; we can still port that when accepted upstream.

@devnexen, thoughts?

@devnexen
Copy link
Member

devnexen commented Jul 18, 2024

agreed it should be upstreamed, the change looks fairly small so you may have your chance it get reviewed in a timely manner.

@Gymnasiast
Copy link
Author

Thank you for your answers, I will try to get it upstreamed in libgd first.

@cmb69
Copy link
Member

cmb69 commented Jul 18, 2024

I will try to get it upstreamed in libgd first.

Thank you! For the time being, I'm switching this PR to draft status.

@cmb69 cmb69 marked this pull request as draft July 18, 2024 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
4 participants