We all know that the PHP GD library can easily create a new image from the URL, GD has Imagecreatefromjpeg (), Imagecreatefrompng () .... And so on function can sometimes read from the URL of the cut BMP images and hateful is GD2 imagecreatefrombmp () Although there are imagecreatefromwbmp () but still the difference is still very far!
Use the following function to facilitate the solution
Copy Code code as follows:
function Imagecreatefrombmp ($file)
{
Global $CurrentBit, $echoMode;
$f =fopen ($file, "R");
$Header =fread ($f, 2);
if ($Header = = "BM")
{
$Size =freaddword ($f);
$Reserved 1=freadword ($f);
$Reserved 2=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;
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 <BR>";
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 <BR>";
}
for ($x =0; $x <strlen ($Data); $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)." <BR> ";
while (true)
{
Break
$y--;
$prefix =freadbyte ($f);
$suffix =freadbyte ($f);
$pocetb +=2;
$echoit =false;
if ($echoit) echo "Prefix: $prefix Suffix: $suffix <BR>";
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 <BR>";
}
for ($x =0; $x <strlen ($Data); $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)
{
$b 1=freadbyte ($f);
$b 2=freadbyte ($f);
return $b 2*256+ $b 1;
}
function Freaddword ($f)
{
$b 1=freadword ($f);
$b 2=freadword ($f);
return $b 2*65536+ $b 1;
}