/*
* 缩略图
* @param background 原图
* @param width 缩略图的宽度
* @param height 缩略图的高度
* @param newfile 新图片的名称
* @param object $geo_info 翻转角度 由函数geo_info 获得 主要针对苹果手机上传图片问题
*/
function thumb($background, $width, $height, $newfile, $geo_info='') {
if( isWap() && isset($geo_info['orientation'])){ //手机上传并且图片翻转
$orientation = $geo_info['orientation'];
if($orientation == 1){
$degrees = 0;
}else if($orientation == 3){
$degrees = 180;
}else if($orientation == 6){
$degrees = 270;
}else if($orientation == 8){
$degrees = 90;
}
}
//按比例获取缩略图的宽高
$temp = getimagesize($background);
$s_type = $temp['mime'];
$s_w = $temp[0];
$s_h = $temp[1];
//90度的奇数倍时要宽高互换
if($orientation == 6 || $orientation==8){
$tmp_th = $s_w;
$s_w = $s_h;
$s_h = $tmp_th;
}
if ($width && ($s_w < $s_h)) {
$width = ($height / $s_h) * $s_w;
} else {
$height = ($width / $s_w) * $s_h;
}
$new = imagecreatetruecolor($width, $height);
switch($s_type){
case 'image/png' :
$img = imagecreatefrompng($background);
break;
case 'image/gif':
$img = imagecreatefromgif($background);
break;
case 'image/jpg':
$img = imagecreatefromjpeg($background);
break;
case 'image/jpeg':
$img = imagecreatefromjpeg($background);
break;
default :
return false;
}
if($degrees>0)$img = imagerotate($img, $degrees, 0xffffff); //翻转图片
$otsc = imagecolortransparent($img);
if($otsc >=0 && $otsc < imagecolorstotal($img)){
$tran=imagecolorsforindex($img, $otsc);
$newt=imagecolorallocate($new, $tran["red"], $tran["green"], $tran["blue"]);
imagefill($new, 0, 0, $newt);
imagecolortransparent($new, $newt);
}
imagecopyresized($new, $img, 0, 0, 0, 0, $width, $height, $s_w, $s_h);
switch($s_type){
case 'image/png' :
imagepng($new, $newfile);
break;
case 'image/gif':
imagegif($new, $newfile);
break;
case 'image/jpg':
imagejpeg($new, $newfile);
break;
case 'image/jpeg':
imagejpeg($new, $newfile);
break;
default :
return false;
}
imagedestroy($new);
imagedestroy($img);
return true;
}