php bmp-常用图片处理成圆角
时间:2010-08-17 来源:旋风
<?php
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;
};
/*
图片处理类:缩略,裁剪,圆角,倾斜
*/
class resizeimage
{
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//是否裁图
var $cut;
//源图象
var $srcimg;
//目标图象地址
var $dstimg;
//圆角源
var $corner;
var $im;
function resizeimage($img, $corner, $wid, $hei,$c, $corner_radius, $angle)
{
$this->srcimg = $img;
$this->corner = $corner;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
$this->corner_radius = $corner_radius;
$this->angle = !empty($angle)?$angle:0;
//图片的类型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img();
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy ($this->im);
}
function newimg()
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//创建一个画布,长为resize_width,高为resize_height
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, $this->width , $this->height);
//说明 int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
//imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。dst_im 和 src_im 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如 果 dst_im 和 src_im 相同的话)区域,但如果区域交迭的话则结果不可预知。
$tmp = $this->rounded_corner($newimg,array("topleft"=>true,"bottomleft"=>true,"bottomright"=>true,"topright"=>true));//切哪个方向的角
imagepng ($tmp,$this->dstimg);//$tmp输出png图像到$this->dstimg
}
//初始化图象
function initi_img(){
switch($this->type){
case "jpg": $this->im = @imagecreatefromjpeg($this->srcimg); if($this->im) return $this->im;
case "gif": $this->im = @imagecreatefromgif($this->srcimg); if($this->im) return $this->im;
case "png": $this->im = @imagecreatefrompng($this->srcimg); if($this->im) return $this->im;
case "bmp": $this->im = @imagecreatefrombmp($this->srcimg); if($this->im) return $this->im;
default:$this->im = @imagecreatefromjpeg($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromgif($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefrompng($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromwbmp($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromgd($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromgd2($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefrombmp($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromxbm($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromxpm($this->srcimg); if($this->im) return $this->im;
$this->im=imagecreate(100,100);
}
}
//处理圆角
function rounded_corner($image,$ar=array("topleft"=>true,"bottomleft"=>true,"bottomright"=>true,"topright"=>true))
{
$topleft =$ar['topleft'];
$bottomleft =$ar['bottomleft'];
$bottomright =$ar['bottomright'];
$topright =$ar['topright'];
$corner_source = imagecreatefrompng('rounded_corner.png');//png资源id
$corner_width = imagesx($corner_source); //获
$corner_height = imagesy($corner_source);
$corner_resized = ImageCreateTrueColor($this->corner_radius, $this->corner_radius);
ImageCopyResampled($corner_resized, $corner_source, 0, 0, 0, 0, $this->corner_radius, $this->corner_radius, $corner_width, $corner_height);
$white = ImageColorAllocate($image,255,255,255);
$black = ImageColorAllocate($image,0,0,0);
//顶部左圆角
if ($topleft == true) {
$dest_x = 0;
$dest_y = 0;
imagecolortransparent($corner_resized, $black); //本函数用来指定某色为透明背景。参数 im 为使用 imagecreate() 打开图形的 handle。参数 col 为 ImageColorAllocate() 所匹配的颜色。ImageColorTransparent返回值为新的透明背景色。
imagecopymerge($image, $corner_resized, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
}
//下部左圆角
if ($bottomleft == true) {
$dest_x = 0;
$dest_y = $this->resize_height - $this->corner_radius;
$rotated = imagerotate($corner_resized, 90, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
//下部右圆角
if ($bottomright == true) {
$dest_x = $this->resize_width -$this->corner_radius;
$dest_y = $this->resize_height - $this->corner_radius;
$rotated = imagerotate($corner_resized, 180, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
//顶部右圆角
if ($topright == true) {
$dest_x = $this->resize_width - $this->corner_radius;
$dest_y = 0;
$rotated = imagerotate($corner_resized, 270, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
$image = imagerotate($image, $this->angle, $white);//逆时针,旋转$this->angle度
return $image;
}
//图象目标地址
function dst_img()
{
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name= substr($this->srcimg,0,$name_length-1);
$this->dstimg = $name."_small.png";
}
}
//resizeimage("图片地址", "处理后的宽度", "处理后的高度", "是否裁剪", "圆角度数", "倾斜度");
$img_file = 'ccc.bmp';
$corner = 'rounded_corner.png';
new resizeimage($img_file, $corner, "150", "300", 0, "6", "0");
//在ccc.bmp 相同的目录下生成 ccc_small.png 图像
?>
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;
};
/*
图片处理类:缩略,裁剪,圆角,倾斜
*/
class resizeimage
{
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//是否裁图
var $cut;
//源图象
var $srcimg;
//目标图象地址
var $dstimg;
//圆角源
var $corner;
var $im;
function resizeimage($img, $corner, $wid, $hei,$c, $corner_radius, $angle)
{
$this->srcimg = $img;
$this->corner = $corner;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
$this->corner_radius = $corner_radius;
$this->angle = !empty($angle)?$angle:0;
//图片的类型
$this->type = substr(strrchr($this->srcimg,"."),1);
//初始化图象
$this->initi_img();
//目标图象地址
$this -> dst_img();
//--
$this->width = imagesx($this->im);
$this->height = imagesy($this->im);
//生成图象
$this->newimg();
ImageDestroy ($this->im);
}
function newimg()
{
$newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);//创建一个画布,长为resize_width,高为resize_height
imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, $this->width , $this->height);
//说明 int imagecopyresampled ( resource dst_im, resource src_im, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH)
//imagecopyresampled() 将一幅图像中的一块正方形区域拷贝到另一个图像中,平滑地插入像素值,因此,尤其是,减小了图像的大小而仍然保持了极大的清晰度。dst_im 和 src_im 分别是目标图像和源图像的标识符。如果源和目标的宽度和高度不同,则会进行相应的图像收缩和拉伸。坐标指的是左上角。本函数可用来在同一幅图内部拷贝(如 果 dst_im 和 src_im 相同的话)区域,但如果区域交迭的话则结果不可预知。
$tmp = $this->rounded_corner($newimg,array("topleft"=>true,"bottomleft"=>true,"bottomright"=>true,"topright"=>true));//切哪个方向的角
imagepng ($tmp,$this->dstimg);//$tmp输出png图像到$this->dstimg
}
//初始化图象
function initi_img(){
switch($this->type){
case "jpg": $this->im = @imagecreatefromjpeg($this->srcimg); if($this->im) return $this->im;
case "gif": $this->im = @imagecreatefromgif($this->srcimg); if($this->im) return $this->im;
case "png": $this->im = @imagecreatefrompng($this->srcimg); if($this->im) return $this->im;
case "bmp": $this->im = @imagecreatefrombmp($this->srcimg); if($this->im) return $this->im;
default:$this->im = @imagecreatefromjpeg($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromgif($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefrompng($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromwbmp($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromgd($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromgd2($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefrombmp($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromxbm($this->srcimg); if($this->im) return $this->im;
$this->im = @imagecreatefromxpm($this->srcimg); if($this->im) return $this->im;
$this->im=imagecreate(100,100);
}
}
//处理圆角
function rounded_corner($image,$ar=array("topleft"=>true,"bottomleft"=>true,"bottomright"=>true,"topright"=>true))
{
$topleft =$ar['topleft'];
$bottomleft =$ar['bottomleft'];
$bottomright =$ar['bottomright'];
$topright =$ar['topright'];
$corner_source = imagecreatefrompng('rounded_corner.png');//png资源id
$corner_width = imagesx($corner_source); //获
$corner_height = imagesy($corner_source);
$corner_resized = ImageCreateTrueColor($this->corner_radius, $this->corner_radius);
ImageCopyResampled($corner_resized, $corner_source, 0, 0, 0, 0, $this->corner_radius, $this->corner_radius, $corner_width, $corner_height);
$white = ImageColorAllocate($image,255,255,255);
$black = ImageColorAllocate($image,0,0,0);
//顶部左圆角
if ($topleft == true) {
$dest_x = 0;
$dest_y = 0;
imagecolortransparent($corner_resized, $black); //本函数用来指定某色为透明背景。参数 im 为使用 imagecreate() 打开图形的 handle。参数 col 为 ImageColorAllocate() 所匹配的颜色。ImageColorTransparent返回值为新的透明背景色。
imagecopymerge($image, $corner_resized, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);//将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
}
//下部左圆角
if ($bottomleft == true) {
$dest_x = 0;
$dest_y = $this->resize_height - $this->corner_radius;
$rotated = imagerotate($corner_resized, 90, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
//下部右圆角
if ($bottomright == true) {
$dest_x = $this->resize_width -$this->corner_radius;
$dest_y = $this->resize_height - $this->corner_radius;
$rotated = imagerotate($corner_resized, 180, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
//顶部右圆角
if ($topright == true) {
$dest_x = $this->resize_width - $this->corner_radius;
$dest_y = 0;
$rotated = imagerotate($corner_resized, 270, 0);
imagecolortransparent($rotated, $black);
imagecopymerge($image, $rotated, $dest_x, $dest_y, 0, 0, $corner_width, $corner_height, 100);
}
$image = imagerotate($image, $this->angle, $white);//逆时针,旋转$this->angle度
return $image;
}
//图象目标地址
function dst_img()
{
$full_length = strlen($this->srcimg);
$type_length = strlen($this->type);
$name_length = $full_length-$type_length;
$name= substr($this->srcimg,0,$name_length-1);
$this->dstimg = $name."_small.png";
}
}
//resizeimage("图片地址", "处理后的宽度", "处理后的高度", "是否裁剪", "圆角度数", "倾斜度");
$img_file = 'ccc.bmp';
$corner = 'rounded_corner.png';
new resizeimage($img_file, $corner, "150", "300", 0, "6", "0");
//在ccc.bmp 相同的目录下生成 ccc_small.png 图像
?>
相关阅读 更多 +