/**
* 创建缩略图
* @param String $filename 原图地址
* @param String $path 新图保存路径
* @param Int $width 新图缩放宽度
* @param Int $height 新图缩放高度
* @param bool $forceReset 强制改变 or 定位中心线按比例改变大小(默认后者)
*/
function ImageResize($filename, $path, $width, $height, $forceReset = false) {
//获取原图信息
$img_info = getimagesize($filename);
$w = $img_info[0];//取得原始图片的宽
$h = $img_info[1];//取得原始图片的高
//生成缩略图名称
$newImage = 'thumb_' . date('YmdHis') . rand(1000,9999);
//根据原图类型加载原图
switch($img_info[2]){
case 1:
$imgCreate = imagecreatefromgif($filename);
$newImage = $newImage . '.gif';
break;
case 2:
$imgCreate = imagecreatefromjpeg($filename);
$newImage = $newImage . '.jpg';
break;
case 3:
$imgCreate = imagecreatefrompng($filename);
$newImage = $newImage . '.png';
break;
default:
return false;
}
//创建缩略图画布
$thumb = imagecreatetruecolor($width, $height);
if($forceReset){
//将原图按比例复制到缩略图上
imagecopyresampled($thumb, $imgCreate, 0, 0, 0, 0, $width, $height, $w, $h);
}else{
$p = $width / $height;
if($w > $h * $p){
$s_x = ($w - $h * $p)/2;
$s_y = 0;
$w = $h * $p;
}else if($w < $h * $p){
$s_x = 0;
$s_y = ($h - $w / $p)/2;
$h = $w / $p;
}else{
$s_x = 0;
$s_y = 0;
}
//缩放图片到新图上,并将多余部分裁剪掉
imagecopyresampled($thumb, $imgCreate, 0, 0, $s_x, $s_y, $width, $height, $w, $h);
}
switch($img_info[2]){
case 1:
imagegif($thumb, $path.$newImage);
break;
case 2:
imagejpeg($thumb, $path.$newImage);
break;
case 3:
imagepng($thumb, $path.$newImage);
break;
}
return $newImage;
}
* 创建缩略图
* @param String $filename 原图地址
* @param String $path 新图保存路径
* @param Int $width 新图缩放宽度
* @param Int $height 新图缩放高度
* @param bool $forceReset 强制改变 or 定位中心线按比例改变大小(默认后者)
*/
function ImageResize($filename, $path, $width, $height, $forceReset = false) {
//获取原图信息
$img_info = getimagesize($filename);
$w = $img_info[0];//取得原始图片的宽
$h = $img_info[1];//取得原始图片的高
//生成缩略图名称
$newImage = 'thumb_' . date('YmdHis') . rand(1000,9999);
//根据原图类型加载原图
switch($img_info[2]){
case 1:
$imgCreate = imagecreatefromgif($filename);
$newImage = $newImage . '.gif';
break;
case 2:
$imgCreate = imagecreatefromjpeg($filename);
$newImage = $newImage . '.jpg';
break;
case 3:
$imgCreate = imagecreatefrompng($filename);
$newImage = $newImage . '.png';
break;
default:
return false;
}
//创建缩略图画布
$thumb = imagecreatetruecolor($width, $height);
if($forceReset){
//将原图按比例复制到缩略图上
imagecopyresampled($thumb, $imgCreate, 0, 0, 0, 0, $width, $height, $w, $h);
}else{
$p = $width / $height;
if($w > $h * $p){
$s_x = ($w - $h * $p)/2;
$s_y = 0;
$w = $h * $p;
}else if($w < $h * $p){
$s_x = 0;
$s_y = ($h - $w / $p)/2;
$h = $w / $p;
}else{
$s_x = 0;
$s_y = 0;
}
//缩放图片到新图上,并将多余部分裁剪掉
imagecopyresampled($thumb, $imgCreate, 0, 0, $s_x, $s_y, $width, $height, $w, $h);
}
switch($img_info[2]){
case 1:
imagegif($thumb, $path.$newImage);
break;
case 2:
imagejpeg($thumb, $path.$newImage);
break;
case 3:
imagepng($thumb, $path.$newImage);
break;
}
return $newImage;
}