欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP imagecreatefrombmp 从BMP文件或URL新建一图像

程序员文章站 2022-03-20 12:18:56
大家都知道php gd库可方便的从url新建一图像, gd中有imagecreatefromjpeg(),imagecreatefrompng()....等之类的funct...
大家都知道php gd库可方便的从url新建一图像, gd中有imagecreatefromjpeg(),imagecreatefrompng()....等之类的function 可有时从url中读取的切bmp图像而 可恨的是 gd2中切偏偏没有imagecreatefrombmp() 虽然有imagecreatefromwbmp() 但还是相差还是很远!
用下面function可以方便解决
复制代码 代码如下:

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<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)
{
$b1=freadbyte($f);
$b2=freadbyte($f);
return $b2*256+$b1;
}
function freaddword($f)
{
$b1=freadword($f);
$b2=freadword($f);
return $b2*65536+$b1;
}