php bmp格式的图片生成缩略图
bmp格式的图片怎么生成缩略图?
//生成缩略图函数
function img_create_small($big_img,$width,$height,$phname_type){//大图文件地址,缩略宽,缩略高,小图地址
$imgage=getimagesize($big_img);//获取大图信息
switch ($imgage[2]){//判断图像类型
case 1:
$im=imagecreatefromgif($big_img);
break;
case 2:
$im=imagecreatefromjpeg($big_img);
break;
case 3:
$im=imagecreatefrompng($big_img);
break;
}
//echo '<pre>';print_r($im);die;
$src_W=imagesx($im);//获取大图宽
$src_H=imagesy($im);//获取大图高
$sizexy = $this->getScaleImage($big_img,$width,$height);
$width = $sizexy[0];
$height = $sizexy[1];
$tn=imagecreatetruecolor($width,$height);//创建小图
imagecopyresized($tn,$im,0,0,0,0,$width,$height,$src_W,$src_H);//复制图像并改变大小
//$type = strrchr($phname,".");
$arr = explode($phname_type,$big_img);
$small_img = $arr[0].'_small'.$phname_type;
imagejpeg($tn,$small_img);//输出图像
}
镜花水月丶
12 years, 9 months ago
Answers
<?
class resizeimage
{
//图片类型
var $type;
//实际宽度
var $width;
//实际高度
var $height;
//改变后的宽度
var $resize_width;
//改变后的高度
var $resize_height;
//是否裁图
var $cut;
//源图象
var $srcimg;
//目标图象地址
var $dstimg;
//临时建的图象
var $im;
//生成的文件名后缀
var $extstr;
function resizeimage($img, $wid, $hei,$extstr,$c=0)
{
$this->srcimg = $img;
$this->resize_width = $wid;
$this->resize_height = $hei;
$this->cut = $c;
$this->extstr = $extstr;
//图片的类型
$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()
{
//改变后的图象的比例
$resize_ratio = ($this->resize_width)/($this->resize_height);
//实际图象的比例
if($this->height>0)
$ratio = ($this->width)/($this->height);
if(($this->cut)=="1")
//裁图
{
if($ratio>=$resize_ratio)
//高度优先
{
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
@ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
//宽度优先
{
$newimg = @imagecreatetruecolor($this->resize_width,$this->resize_height);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
@ImageJpeg ($newimg,$this->dstimg);
}
}
else
//不裁图
{
if($ratio>=$resize_ratio)
{
$newimg = @imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
ImageJpeg ($newimg,$this->dstimg);
}
if($ratio<$resize_ratio)
{
$newimg = @imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
@imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
@ImageJpeg ($newimg,$this->dstimg);
}
}
}
//初始化图象
function initi_img()
{
$type=strtolower($this->type);//转换成小写,否则不写扩展名生成不了。
if($type=="jpg" || $type=="jpeg" || $type=="jpe")
{
$this->im = @imagecreatefromjpeg($this->srcimg);
}
if($type=="gif")
{
$this->im = @imagecreatefromgif($this->srcimg);
}
if($type=="png")
{
$this->im = @imagecreatefrompng($this->srcimg);
}
if($type=="bmp")
{
$this->im = $this->imagecreatefrombmp($this->srcimg);
}
}
function imagecreatefrombmp($p_sFile){
$file = fopen($p_sFile,"rb");
$read = fread($file,10);
while(!feof($file)&&($read<>""))
$read .= fread($file,1024);
$temp = unpack("H*",$read);
$hex = $temp[1];
$header = substr($hex,0,108);
if (substr($header,0,4)=="424d"){
$header_parts = str_split($header,2);
$width = hexdec($header_parts[19].$header_parts[18]);
$height = hexdec($header_parts[23].$header_parts[22]);
unset($header_parts);
}
$x = 0;
$y = 1;
$image = imagecreatetruecolor($width,$height);
$body = substr($hex,108);
$body_size = (strlen($body)/2);
$header_size = ($width*$height);
$usePadding = ($body_size>($header_size*3)+4);
for ($i=0;$i<$body_size;$i+=3){
if ($x>=$width){
if ($usePadding)
$i += $width%4;
$x = 0;
$y++;
if ($y>$height)
break;
}
$i_pos = $i*2;
$r = hexdec($body[$i_pos+4].$body[$i_pos+5]);
$g = hexdec($body[$i_pos+2].$body[$i_pos+3]);
$b = hexdec($body[$i_pos].$body[$i_pos+1]);
$color = imagecolorallocate($image,$r,$g,$b);
imagesetpixel($image,$x,$height-$y,$color);
$x++;
}
unset($body);
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.$this->extstr.'.'.$this->type;
}
static function get_url($img,$extstr){
$imgs = explode('.',$img);
$ext = end($imgs);
$full_length = strlen($img);
$type_length = strlen($ext);
$name_length = $full_length-$type_length;
$name = substr($img,0,$name_length-1);
return $name.$extstr.'.'.$ext;
}
}
?>
来自: http://zhujllove.blog.163.com/blog/static/113901825201122113930917/
好男人聂小帅
answered 12 years, 9 months ago