网上资料整理,并非原创,大家分享!
//-----------imagecheck.class.php------------------
/*
php缩略图函数:
等比例无损压缩,可填充补充色
主持格式:bmp 、jpg 、gif、png
param:
@srcimage : 要缩小的图片
@dstimage : 要保存的图片
@dst_width: 缩小宽
@dst_height: 缩小高
@backgroundcolor: 补充色 如:#ffffff 支持 6位 不支持3位
*/
class imagecheck{
public function imagezoom($srcimage,$dstimage,$dst_width,$dst_height,$backgroundcolor ){
// 中文件名乱码
if ( php_os == 'winnt' ) {
$srcimage = iconv('utf-8','gbk',$srcimage);
$dstimage = iconv('utf-8','gbk',$dstimage);
}
$dstimg = imagecreatetruecolor( $dst_width, $dst_height );
$color = imagecolorallocate($dstimg,hexdec(substr($backgroundcolor, 1, 2)),hexdec(substr($backgroundcolor, 3, 2)),hexdec(substr($backgroundcolor, 5, 2)));
//echo $srcimage;
imagefill($dstimg, 0, 0, $color);
//dump(getimagesize($srcimage));
if ( !$arr=getimagesize($srcimage) ) {
echo 要生成缩略图的文件不存在;
exit;
}
$src_width = $arr[0];
$src_height = $arr[1];
$srcimg =null;
$method =$this->getcreatemethod($srcimage);
if ( $method ) {
eval( '$srcimg ='.$method.';');
}
$dst_x = 0;
$dst_y = 0;
$dst_w = $dst_width;
$dst_h = $dst_height;
if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) {
$dst_w = $src_width * ( $dst_height / $src_height );
$dst_x = ( $dst_width - $dst_w ) / 2;
} elseif ( ($dst_width / $dst_height - $src_width / $src_height) $dst_h = $src_height * ( $dst_width / $src_width );
$dst_y = ( $dst_height - $dst_h ) / 2;
}
imagecopyresampled($dstimg,$srcimg,$dst_x,$dst_y,0,0,$dst_w,$dst_h,$src_width,$src_height);
// 保存格式
$arr = array(
'jpg' => 'imagejpeg',
'jpeg' => 'imagejpeg',
'png' => 'imagepng',
'gif' => 'imagegif',
'bmp' => 'imagebmp'
);
$suffix = strtolower(array_pop(explode('.', $dstimage ) ) );
if (!in_array($suffix,array_keys($arr))) {
echo 保存的文件名错误;
exit;
} else {
eval( $arr[$suffix] . '($dstimg, '.$dstimage.');' );
}
imagejpeg($dstimg,$dstimage);
imagedestroy($dstimg);
imagedestroy($srcimg);
}
//*************************************************************
public function getcreatemethod($file){
$arr = array(
'474946' => imagecreatefromgif('$file'),
'ffd8ff' => imagecreatefromjpeg('$file'),
'424d' => imagecreatefrombmp('$file'),
'89504e' => imagecreatefrompng('$file')
);
$fd = fopen( $file, rb );
$data = fread( $fd, 3 );
$data =$this->str2hex( $data );
if( array_key_exists( $data, $arr ) ) {
return $arr[$data];
}else if( array_key_exists( substr($data, 0, 4), $arr ) ) {
return $arr[substr($data, 0, 4)];
}else{
return false;
}
}
//**********************************************************
public function str2hex( $str ) {
$ret = ;
for( $i = 0; $i $ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) ):'0'.strval( dechex( ord($str[$i]) ) );
}
return strtoupper( $ret );
}
//***********************************************************
public function imagecreatefrombmp($filename){
if (! $f1 = fopen($filename,rb)) return false;
$file = unpack(vfile_type/vfile_size/vreserved/vbitmap_offset, fread($f1,14));
if ($file['file_type'] != 19778) return false;
$bmp = unpack('vheader_size/vwidth/vheight/vplanes/vbits_per_pixel'.'/vcompression/vsize_bitmap/vhoriz_resolution'.'/vvert_resolution/vcolors_used/vcolors_important', fread($f1,40));
$bmp['colors'] = pow(2,$bmp['bits_per_pixel']);
if ($bmp['size_bitmap'] == 0) $bmp['size_bitmap'] = $file['file_size'] - $file['bitmap_offset'];
$bmp['bytes_per_pixel'] = $bmp['bits_per_pixel']/8;
$bmp['bytes_per_pixel2'] = ceil($bmp['bytes_per_pixel']);
$bmp['decal'] = ($bmp['width']*$bmp['bytes_per_pixel']/4);
$bmp['decal'] -= floor($bmp['width']*$bmp['bytes_per_pixel']/4);
$bmp['decal'] = 4-(4*$bmp['decal']);
if ($bmp['decal'] == 4) $bmp['decal'] = 0;
$palette = array();
if ($bmp['colors'] {
$palette = unpack('v'.$bmp['colors'], fread($f1,$bmp['colors']*4));
}
$img = fread($f1,$bmp['size_bitmap']);
$vide = chr(0);
$res = imagecreatetruecolor($bmp['width'],$bmp['height']);
$p = 0;
$y = $bmp['height']-1;
while ($y >= 0)
{
$x=0;
while ($x {
if ($bmp['bits_per_pixel'] == 24)
$color = unpack(v,substr($img,$p,3).$vide);
else if ($bmp['bits_per_pixel'] == 16){
$color = unpack(n,substr($img,$p,2));
$color[1] = $palette[$color[1]+1];
}else if ($bmp['bits_per_pixel'] == 8){
$color = unpack(n,$vide.substr($img,$p,1));
$color[1] = $palette[$color[1]+1];
}else if ($bmp['bits_per_pixel'] == 4){
$color = unpack(n,$vide.substr($img,floor($p),1));
if (($p*2)%2 == 0) $color[1] = ($color[1] >> 4) ; else $color[1] = ($color[1] & 0x0f);
$color[1] = $palette[$color[1]+1];
}else if ($bmp['bits_per_pixel'] == 1){
$color = unpack(n,$vide.substr($img,floor($p),1));
if (($p*8)%8 == 0) $color[1] = $color[1] >>7;
elseif (($p*8)%8 == 1) $color[1] = ($color[1] & 0x40)>>6;
elseif (($p*8)%8 == 2) $color[1] = ($color[1] & 0x20)>>5;
elseif (($p*8)%8 == 3) $color[1] = ($color[1] & 0x10)>>4;
elseif (($p*8)%8 == 4) $color[1] = ($color[1] & 0x8)>>3;
elseif (($p*8)%8 == 5) $color[1] = ($color[1] & 0x4)>>2;
elseif (($p*8)%8 == 6) $color[1] = ($color[1] & 0x2)>>1;
elseif (($p*8)%8 == 7) $color[1] = ($color[1] & 0x1);
$color[1] = $palette[$color[1]+1];
}else
return false;
imagesetpixel($res,$x,$y,$color[1]);
$x++;
$p += $bmp['bytes_per_pixel'];
}
$y--;
$p+=$bmp['decal'];
}
fclose($f1);
return $res;
}
//*********************************bmp保存函数*************************************************
public function imagebmp ($im, $fn = false){
if (!$im) return false;
if ($fn === false) $fn = 'php://output';
$f = fopen ($fn, w);
if (!$f) return false;
$biwidth = imagesx ($im);
$biheight = imagesy ($im);
$bibpline = $biwidth * 3;
$bistride = ($bibpline + 3) & ~3;
$bisizeimage = $bistride * $biheight;
$bfoffbits = 54;
$bfsize = $bfoffbits + $bisizeimage;
fwrite ($f, 'bm', 2);
fwrite ($f, pack ('vvvv', $bfsize, 0, 0, $bfoffbits));
fwrite ($f, pack ('vvvvvvvvvvv',40,$biwidth, $biheight,1,24,0, $bisizeimage,0,0,0,0));
$numpad = $bistride - $bibpline;
for ($y = $biheight - 1; $y >= 0; --$y)
{
for ($x = 0; $x {
$col = imagecolorat ($im, $x, $y);
fwrite ($f, pack ('v', $col), 3);
}
for ($i = 0; $i {
fwrite ($f, pack ('c', 0));
}
fclose ($f);
return true;
}
}
}
?>
//----------调用-------------------------
$image=new imagecheck();
$image->imagezoom(./common/upload/news/20130712/liu.jpg,./common/upload/news/20130712/liu.jpg,100,100,#ffffff);
同名将覆盖
ad:真正免费,域名+虚机+企业邮箱=0元