网上资料整理,并非原创,大家分享!//-----------ImageCheck.class.php------------------<br>
<br>
/*<br>
php缩略图函数:<br>
等比例无损压缩,可填充补充色 <br>
主持格式:bmp 、jpg 、gif、png<br>
param:<br>
@srcimage : 要缩小的图片<br>
@dstimage : 要保存的图片<br>
@dst_width: 缩小宽<br>
@dst_height: 缩小高<br>
@backgroundcolor: 补充色 如:#FFFFFF 支持 6位 不支持3位<br>
*/<br>
class ImageCheck{<br>
public function imagezoom($srcimage,$dstimage,$dst_width,$dst_height,$backgroundcolor ){<br>
// 中文件名乱码<br>
if ( PHP_OS == 'WINNT' ) {<br>
$srcimage = iconv('UTF-8','GBK',$srcimage);<br>
$dstimage = iconv('UTF-8','GBK',$dstimage);<br>
}<br>
$dstimg = imagecreatetruecolor( $dst_width, $dst_height );<br>
$color = imagecolorallocate($dstimg,hexdec(substr($backgroundcolor, 1, 2)),hexdec(substr($backgroundcolor, 3, 2)),hexdec(substr($backgroundcolor, 5, 2)));<br>
//echo $srcimage;<br>
imagefill($dstimg, 0, 0, $color);<br>
//dump(getimagesize($srcimage));<br>
if ( !$arr=getimagesize($srcimage) ) {<br>
echo "要生成缩略图的文件不存在";<br>
exit;<br>
}<br>
$src_width = $arr[0];<br>
$src_height = $arr[1];<br>
$srcimg =null;<br>
$method =$this->getcreatemethod($srcimage);<br>
if ( $method ) {<br>
eval( '$srcimg ='.$method.';');<br>
}<br>
$dst_x = 0;<br>
$dst_y = 0;<br>
$dst_w = $dst_width;<br>
$dst_h = $dst_height;<br>
if ( ($dst_width / $dst_height - $src_width / $src_height) > 0 ) {<br>
$dst_w = $src_width * ( $dst_height / $src_height );<br>
$dst_x = ( $dst_width - $dst_w ) / 2;<br>
} elseif ( ($dst_width / $dst_height - $src_width / $src_height)
$dst_h = $src_height * ( $dst_width / $src_width );<br>
$dst_y = ( $dst_height - $dst_h ) / 2;<br>
}<br>
imagecopyresampled($dstimg,$srcimg,$dst_x,$dst_y,0,0,$dst_w,$dst_h,$src_width,$src_height);<br>
// 保存格式<br>
$arr = array(<br>
'jpg' => 'imagejpeg', <br>
'jpeg' => 'imagejpeg', <br>
'png' => 'imagepng', <br>
'gif' => 'imagegif', <br>
'bmp' => 'imagebmp'<br>
);<br>
$suffix = strtolower(array_pop(explode('.', $dstimage ) ) );<br>
if (!in_array($suffix,array_keys($arr))) {<br>
echo "保存的文件名错误";<br>
exit;<br>
} else {<br>
eval( $arr[$suffix] . '($dstimg, "'.$dstimage.'");' );<br>
}<br>
imagejpeg($dstimg,$dstimage);<br>
imagedestroy($dstimg);<br>
imagedestroy($srcimg);<br>
}<br>
//*************************************************************<br>
public function getcreatemethod($file){<br>
$arr = array(<br>
'474946' => "imagecreatefromgif('$file')",<br>
'FFD8FF' => "imagecreatefromjpeg('$file')",<br>
'424D' => "imagecreatefrombmp('$file')",<br>
'89504E' => "imagecreatefrompng('$file')"<br>
);<br>
$fd = fopen( $file, "rb" );<br>
$data = fread( $fd, 3 );<br>
$data =$this->str2hex( $data );<br>
if( array_key_exists( $data, $arr ) ) {<br>
return $arr[$data];<br>
}else if( array_key_exists( substr($data, 0, 4), $arr ) ) {<br>
return $arr[substr($data, 0, 4)];<br>
}else{<br>
return false;<br>
}<br>
}<br>
//**********************************************************<br>
public function str2hex( $str ) {<br>
$ret = "";<br>
for( $i = 0; $i
$ret .= ord($str[$i]) >= 16 ? strval( dechex( ord($str[$i]) ) ):'0'.strval( dechex( ord($str[$i]) ) );<br>
}<br>
return strtoupper( $ret );<br>
} <br>
//***********************************************************<br>
public function imagecreatefrombmp($filename){<br>
if (! $f1 = fopen($filename,"rb")) return FALSE;<br>
$FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1,14));<br>
if ($FILE['file_type'] != 19778) return FALSE;<br>
$BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.'/Vcompression/Vsize_bitmap/Vhoriz_resolution'.'/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1,40));<br>
$BMP['colors'] = pow(2,$BMP['bits_per_pixel']);<br>
if ($BMP['size_bitmap'] == 0) $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];<br>
$BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;<br>
$BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);<br>
$BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);<br>
$BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);<br>
$BMP['decal'] = 4-(4*$BMP['decal']);<br>
if ($BMP['decal'] == 4) $BMP['decal'] = 0;<br>
$PALETTE = array();<br>
if ($BMP['colors']
{<br>
$PALETTE = unpack('V'.$BMP['colors'], fread($f1,$BMP['colors']*4));<br>
}<br>
$IMG = fread($f1,$BMP['size_bitmap']);<br>
$VIDE = chr(0);<br>
$res = imagecreatetruecolor($BMP['width'],$BMP['height']);<br>
$P = 0;<br>
$Y = $BMP['height']-1;<br>
while ($Y >= 0)<br>
{<br>
$X=0;<br>
while ($X
{<br>
if ($BMP['bits_per_pixel'] == 24)<br>
$COLOR = unpack("V",substr($IMG,$P,3).$VIDE);<br>
else if ($BMP['bits_per_pixel'] == 16){<br>
$COLOR = unpack("n",substr($IMG,$P,2));<br>
$COLOR[1] = $PALETTE[$COLOR[1]+1];<br>
}else if ($BMP['bits_per_pixel'] == 8){<br>
$COLOR = unpack("n",$VIDE.substr($IMG,$P,1));<br>
$COLOR[1] = $PALETTE[$COLOR[1]+1];<br>
}else if ($BMP['bits_per_pixel'] == 4){<br>
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));<br>
if (($P*2)%2 == 0) $COLOR[1] = ($COLOR[1] >> 4) ; else $COLOR[1] = ($COLOR[1] & 0x0F);<br>
$COLOR[1] = $PALETTE[$COLOR[1]+1];<br>
}else if ($BMP['bits_per_pixel'] == 1){<br>
$COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));<br>
if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;<br>
elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;<br>
elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;<br>
elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;<br>
elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;<br>
elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;<br>
elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;<br>
elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);<br>
$COLOR[1] = $PALETTE[$COLOR[1]+1];<br>
}else<br>
return FALSE;<br>
imagesetpixel($res,$X,$Y,$COLOR[1]);<br>
$X++;<br>
$P += $BMP['bytes_per_pixel'];<br>
}<br>
$Y--;<br>
$P+=$BMP['decal'];<br>
}<br>
fclose($f1);<br>
return $res;<br>
} <br>
//*********************************BMP保存函数*************************************************<br>
public function imagebmp ($im, $fn = false){<br>
if (!$im) return false;<br>
if ($fn === false) $fn = 'php://output';<br>
$f = fopen ($fn, "w");<br>
if (!$f) return false;<br>
$biWidth = imagesx ($im);<br>
$biHeight = imagesy ($im);<br>
$biBPLine = $biWidth * 3;<br>
$biStride = ($biBPLine + 3) & ~3;<br>
$biSizeImage = $biStride * $biHeight;<br>
$bfOffBits = 54;<br>
$bfSize = $bfOffBits + $biSizeImage;<br>
fwrite ($f, 'BM', 2);<br>
fwrite ($f, pack ('VvvV', $bfSize, 0, 0, $bfOffBits));<br>
fwrite ($f, pack ('VVVvvVVVVVV',40,$biWidth, $biHeight,1,24,0, $biSizeImage,0,0,0,0));<br>
$numpad = $biStride - $biBPLine;<br>
for ($y = $biHeight - 1; $y >= 0; --$y)<br>
{<br>
for ($x = 0; $x
{<br>
$col = imagecolorat ($im, $x, $y);<br>
fwrite ($f, pack ('V', $col), 3);<br>
}<br>
for ($i = 0; $i
{<br>
fwrite ($f, pack ('C', 0));<br>
}<br>
fclose ($f);<br>
return true;<br>
} <br>
} <br>
}<br>
?> <br>
//----------调用-------------------------<br>
$image=new ImageCheck();<br>
$image->imagezoom("./Common/upload/news/20130712/liu.jpg","./Common/upload/news/20130712/liu.jpg",100,100,"#FFFFFF");<br>
同名将覆盖
AD:真正免费,域名+虚机+企业邮箱=0元