Recently, we need to convert bmp images into jpg images, but what we can find on the internet is that bmp-to-gd classes or functions are more or less problematic. Most of the 16-bit bmp images are either completely black and completely powdered, or cannot be converted, while others do not support 32-bit bmp images at all. Finally, I found a solution on the php official website. The most widely circulated imagecreatefrombmp function on the Internet, similar to GD, is a problematic version and cannot correctly process 16-bit bmp images. However, some people have provided correction methods on the official website, the corrected code is as follows:
- /**
- * BMP creation function
- * @ Author simon
- * @ Modified
- * @ Param string $ filename path of bmp file
- * @ Example who use, who knows
- * @ Return resource of GD
- */
- Function imagecreatefrombmp ($ filename ){
- If (! $ F1 = fopen ($ filename, "rb "))
- Return FALSE;
- $ FILE = unpack ("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread ($ f1, 14 ));
- If ($ FILE ['File _ type']! = 19778)
- Return FALSE;
- $ BMP = unpack ('vheader _ size/Vwidth/Vheight/vplanes/vbits_per_pixel '. '/Vcompression/Vsize_bitmap/Vhoriz_resolution '. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread ($ f1, 40 ));
- $ BMP ['Colors '] = pow (2, $ BMP ['bits _ per_pixel']);
- If ($ BMP ['size _ bitmap'] = 0)
- $ BMP ['size _ bitmap'] = $ FILE ['File _ size']-$ file ['bitmap _ offset'];
- $ BMP ['bytes _ per_pixel '] = $ BMP ['bits _ per_pixel']/8;
- $ BMP ['bytes _ per_pixel2 '] = ceil ($ BMP ['bytes _ per_pixel']);
- $ BMP ['desc'] = ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 );
- $ BMP ['desc']-= floor ($ BMP ['width'] * $ BMP ['bytes _ per_pixel ']/4 );
- $ BMP ['demo'] = 4-(4 * $ BMP ['demo']);
- If ($ BMP ['desc'] = 4)
- $ BMP ['desc'] = 0;
- $ PALETTE = array ();
- If ($ BMP ['color'] <16777216 & $ BMP ['color']! = 65536)
- {
- $ PALETTE = unpack ('v'. $ BMP ['Colors '], fread ($ f1, $ BMP ['Colors'] * 4 ));
- }
- $ IMG = fread ($ f1, $ BMP ['size _ bitmap']);
- $ VIDE = chr (0 );
- $ Res = imagecreatetruecolor ($ BMP ['width'], $ BMP ['height']);
- $ P = 0;
- $ Y = $ BMP ['height']-1;
- While ($ Y> = 0 ){
- $ X = 0;
- While ($ X <$ BMP ['width']) {
- If ($ BMP ['bits _ per_pixel '] = 32 ){
- $ COLOR = unpack ("V", substr ($ IMG, $ P, 3 ));
- $ B = ord (substr ($ IMG, $ P, 1 ));
- $ G = ord (substr ($ IMG, $ P + 1, 1 ));
- $ R = ord (substr ($ IMG, $ P + 2, 1 ));
- $ Color = imagecolorexact ($ res, $ R, $ G, $ B );
- If ($ color =-1)
- $ Color = imagecolorallocate ($ res, $ R, $ G, $ B );
- $ COLOR [0] = $ R * 256*256 + $ G * 256 + $ B;
- $ COLOR [1] = $ color;
- } Elseif ($ BMP ['bits _ per_pixel '] = 24 ){
- $ COLOR = unpack ("V", substr ($ IMG, $ P, 3). $ VIDE );
- } Elseif ($ BMP ['bits _ per_pixel '] = 16 ){
- $ COLOR = unpack ("v", substr ($ IMG, $ P, 2 ));
- $ Blue = ($ COLOR [1] & 0x001f) <3) + 7;
- $ Green = ($ COLOR [1] & 0x03e0)> 2) + 7;
- $ Red = ($ COLOR [1] & 0xfc00)> 7) + 7;
- $ COLOR [1] = $ red * 65536 + $ green * 256 + $ blue;
- } Elseif ($ BMP ['bits _ per_pixel '] = 8 ){
- $ COLOR = unpack ("n", $ VIDE. substr ($ IMG, $ P, 1 ));
- $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
- } Elseif ($ BMP ['bits _ per_pixel '] = 4 ){
- $ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1 ));
- If ($ P * 2) % 2 = 0)
- $ COLOR [1] = ($ COLOR [1]> 4 );
- Else
- $ COLOR [1] = ($ COLOR [1] & 0x0F );
- $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
- } Elseif ($ BMP ['bits _ per_pixel '] = 1 ){
- $ COLOR = unpack ("n", $ VIDE. substr ($ IMG, floor ($ P), 1 ));
- If ($ P * 8) % 8 = 0)
- $ COLOR [1] = $ COLOR [1]> 7;
- Elseif ($ P * 8) % 8 = 1)
- $ COLOR [1] = ($ COLOR [1] & 0x40)> 6;
- Elseif ($ P * 8) % 8 = 2)
- $ COLOR [1] = ($ COLOR [1] & 0x20)> 5;
- Elseif ($ P * 8) % 8 = 3)
- $ COLOR [1] = ($ COLOR [1] & 0x10)> 4;
- Elseif ($ P * 8) % 8 = 4)
- $ COLOR [1] = ($ COLOR [1] & 0x8)> 3;
- Elseif ($ P * 8) % 8 = 5)
- $ COLOR [1] = ($ COLOR [1] & 0x4)> 2;
- Elseif ($ P * 8) % 8 = 6)
- $ COLOR [1] = ($ COLOR [1] & 0x2)> 1;
- Elseif ($ P * 8) % 8 = 7)
- $ COLOR [1] = ($ COLOR [1] & 0x1 );
- $ COLOR [1] = $ PALETTE [$ COLOR [1] + 1];
- } Else
- Return FALSE;
- Imagesetpixel ($ res, $ X, $ Y, $ COLOR [1]);
- $ X ++;
- $ P + = $ BMP ['bytes _ per_pixel '];
- }
- $ Y --;
- $ P + = $ BMP ['demo'];
- }
- Fclose ($ f1 );
- Return $ res;
- }
|