PHPimagecreatefrombmp creates an image from a BMP file or URL
Last Update:2018-04-03
Source: Internet
Author: User
We all know that the phpGD library can easily create an image from the URL. the GD contains imagecreatefromjpeg (), imagecreatefromPNG ().... as we all know, the php GD library can easily create an image from the URL. in GD, imagecreatefromjpeg () and imagecreatefromPNG ().... some functions such as this can sometimes be read from the URL to cut the BMP image, but what is hateful is that there is no imageCreateFromBMP () in GD2, although there is imagecreatefromWBMP (), the difference is still very far!
You can use the following FUNCTION to conveniently solve the problem.
The code is as follows:
Function imagecreatefrombmp ($ file)
{
Global $ CurrentBit, $ echoMode;
$ F = fopen ($ file, "r ");
$ Header = fread ($ f, 2 );
If ($ Header = "BM ")
{
$ Size = freaddword ($ f );
$ Reserved1 = freadword ($ f );
$ Reserved2 = freadword ($ f );
$ FirstByteOfImage = freaddword ($ f );
$ SizeBITMAPINFOHEADER = freaddword ($ f );
$ Width = freaddword ($ f );
$ Height = freaddword ($ f );
$ BiPlanes = freadword ($ f );
$ BiBitCount = freadword ($ f );
$ RLECompression = freaddword ($ f );
$ WidthxHeight = freaddword ($ f );
$ BiXPelsPerMeter = freaddword ($ f );
$ BiYPelsPerMeter = freaddword ($ f );
$ NumberOfPalettesUsed = freaddword ($ f );
$ NumberOfImportantColors = freaddword ($ f );
If ($ biBitCount <24)
{
$ Img = imagecreate ($ Width, $ Height );
$ Colors = pow (2, $ biBitCount );
For ($ p = 0; $ p <$ Colors; $ p ++)
{
$ B = freadbyte ($ f );
$ G = freadbyte ($ f );
$ R = freadbyte ($ f );
$ Reserved = freadbyte ($ f );
$ Palette [] = imagecolorallocate ($ img, $ R, $ G, $ B );
}
If ($ RLECompression = 0)
{
$ Zbytek = (4-ceil ($ Width/(8/$ biBitCount) % 4) % 4;
For ($ y = $ Height-1; $ y> = 0; $ y --)
{
$ CurrentBit = 0;
For ($ x = 0; $ x <$ Width; $ x ++)
{
$ C = freadbits ($ f, $ biBitCount );
Imagesetpixel ($ img, $ x, $ y, $ Palette [$ C]);
}
If ($ CurrentBit! = 0) {freadbyte ($ f );}
For ($ g = 0; $ g <$ Zbytek; $ g ++)
Freadbyte ($ f );
}
}
}
If ($ RLECompression = 1) // $ BI_RLE8
{
$ Y = $ Height;
$ Pocetb = 0;
While (true)
{
$ Y --;
$ Prefix = freadbyte ($ f );
$ Suffix = freadbyte ($ f );
$ Pocetb + = 2;
$ Echoit = false;
If ($ echoit) echo "Prefix: $ prefix Suffix: $ suffix
";
If ($ prefix = 0) and ($ suffix = 1) break;
If (feof ($ f) break;
While (! ($ Prefix = 0) and ($ suffix = 0 )))
{
If ($ prefix = 0)
{
$ Pocet = $ suffix;
$ Data. = fread ($ f, $ pocet );
$ Pocetb + = $ pocet;
If ($ pocetb % 2 = 1) {freadbyte ($ f); $ pocetb ++ ;}
}
If ($ prefix> 0)
{
$ Pocet = $ prefix;
For ($ r = 0; $ r <$ pocet; $ r ++)
$ Data. = chr ($ suffix );
}
$ Prefix = freadbyte ($ f );
$ Suffix = freadbyte ($ f );
$ Pocetb + = 2;
If ($ echoit) echo "Prefix: $ prefix Suffix: $ suffix
";
}
For ($ x = 0; $ x {
Imagesetpixel ($ img, $ x, $ y, $ Palette [ord ($ Data [$ x]);
}
$ Data = "";
}
}
If ($ RLECompression = 2) // $ BI_RLE4
{
$ Y = $ Height;
$ Pocetb = 0;
/* While (! Feof ($ f ))
Echo freadbyte ($ f). "_". freadbyte ($ f )."
";*/
While (true)
{
// Break;
$ Y --;
$ Prefix = freadbyte ($ f );
$ Suffix = freadbyte ($ f );
$ Pocetb + = 2;
$ Echoit = false;
If ($ echoit) echo "Prefix: $ prefix Suffix: $ suffix
";
If ($ prefix = 0) and ($ suffix = 1) break;
If (feof ($ f) break;
While (! ($ Prefix = 0) and ($ suffix = 0 )))
{
If ($ prefix = 0)
{
$ Pocet = $ suffix;
$ CurrentBit = 0;
For ($ h = 0; $ h <$ pocet; $ h ++)
$ Data. = chr (freadbits ($ f, 4 ));
If ($ CurrentBit! = 0) freadbits ($ f, 4 );
$ Pocetb + = ceil ($ pocet/2 ));
If ($ pocetb % 2 = 1) {freadbyte ($ f); $ pocetb ++ ;}
}
If ($ prefix> 0)
{
$ Pocet = $ prefix;
$ I = 0;
For ($ r = 0; $ r <$ pocet; $ r ++)
{
If ($ I % 2 = 0)
{
$ Data. = chr ($ suffix % 16 );
}
Else
{
$ Data. = chr (floor ($ suffix/16 ));
}
$ I ++;
}
}
$ Prefix = freadbyte ($ f );
$ Suffix = freadbyte ($ f );
$ Pocetb + = 2;
If ($ echoit) echo "Prefix: $ prefix Suffix: $ suffix
";
}
For ($ x = 0; $ x {
Imagesetpixel ($ img, $ x, $ y, $ Palette [ord ($ Data [$ x]);
}
$ Data = "";
}
}
If ($ biBitCount = 24)
{
$ Img = imagecreatetruecolor ($ Width, $ Height );
$ Zbytek = $ Width % 4;
For ($ y = $ Height-1; $ y> = 0; $ y --)
{
For ($ x = 0; $ x <$ Width; $ x ++)
{
$ B = freadbyte ($ f );
$ G = freadbyte ($ f );
$ R = freadbyte ($ f );
$ Color = imagecolorexact ($ img, $ R, $ G, $ B );
If ($ color =-1) $ color = imagecolorallocate ($ img, $ R, $ G, $ B );
Imagesetpixel ($ img, $ x, $ y, $ color );
}
For ($ z = 0; $ z <$ Zbytek; $ z ++)
Freadbyte ($ f );
}
}
Return $ img;
}
Fclose ($ f );
}
Function freadbyte ($ f)
{
Return ord (fread ($ f, 1 ));
}
Function freadword ($ f)
{
$ B1 = freadbyte ($ f );
$ B2 = freadbyte ($ f );
Return $ b2 * 256 + $ b1;
}
Function freaddword ($ f)
{
$ B1 = freadword ($ f );
$ B2 = freadword ($ f );
Return $ b2 * 65536 + $ b1;
}