获取缩略图
function thumb($image,$thumbname,$type='',$maxWidth=200,$maxHeight='auto',$interlace=true)
{
// 获取原图信息
$info = Image::getImageInfo($image);
if($info !== false) {
$srcWidth = $info['width'];
$srcHeight = $info['height'];
$type = empty($type)?$info['type']:$type;
$type = strtolower($type);
$interlace = $interlace? 1:0;
unset($info);
if( $maxHeight=='auto' ){
$scale = $maxWidth/$srcWidth;
}else{
$scale = min($maxWidth/$srcWidth, $maxHeight/$srcHeight); // 计算缩放比例
}
if($scale>=1) {
// 超过原图大小不再缩略
$width = $srcWidth;
$height = $srcHeight;
}else{
// 缩略图尺寸
$width = (int)($srcWidth*$scale);
$height = (int)($srcHeight*$scale);
}
// 载入原图
$createFun = 'ImageCreateFrom'.($type=='jpg'?'jpeg':$type);
$srcImg = $createFun($image);
//创建缩略图
if($type!='gif' && function_exists('imagecreatetruecolor'))
$thumbImg = imagecreatetruecolor($width, $height);
else
$thumbImg = imagecreate($width, $height);
// 新建PNG缩略图通道透明处理
if('png'==$type) {
imagealphablending($thumbImg, false);//取消默认的混色模式
imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息
}elseif('gif'==$type) {
// 新建GIF缩略图预处理,保证透明效果不失效
$background_color = imagecolorallocate($thumbImg, 0,255,0); // 指派一个绿色
imagecolortransparent($thumbImg,$background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
// 复制图片
if(function_exists("ImageCopyResampled"))
imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
else
imagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth,$srcHeight);
// 对jpeg图形设置隔行扫描
if('jpg'==$type || 'jpeg'==$type) imageinterlace($thumbImg,$interlace);
//$gray=ImageColorAllocate($thumbImg,255,0,0);
// 生成图片
if($type == 'jpg' || $type =='jpeg'){
$imageFun = 'imagejpeg';
$imageFun($thumbImg,$thumbname,100);
}else{
$imageFun = 'image'.$type;
$imageFun($thumbImg,$thumbname);
}
imagedestroy($thumbImg);
imagedestroy($srcImg);
return $thumbname;
}
return false;
}截取任意尺寸图片大小
function cut($image,$cutfile,$cutWidth='',$cutHeight=''){
$cutHeight = intval($cutHeight);
$cutWidth = intval($cutWidth);
$info = Image::getImageInfo($image);
if($info){
if(empty($cutHeight) && empty($cutWidth)){
//将原图拷贝一份
copy($image,$cutfile);
return true;
}
$srcWidth = $info['width']; //原始宽
$srcHeight = $info['height']; //原始高
$ext = $info['type'];
//原始宽高比
$oWHP = $srcWidth/$srcHeight;
//要截取的图的宽高比
$cWHP = $cutWidth/$cutHeight;
//获取缩放比例
if($oWHP > $cWHP){
//以为高为基准缩放
$sp = $cutHeight/$srcHeight;
}else{
//以宽为基准缩放
$sp = $cutWidth/$srcWidth;
}
$_s_x = ($srcWidth - $cutWidth/$sp) / 2 ;
$_s_y = ($srcHeight - $cutHeight/$sp) / 2;
$func = ($ext != 'jpg' && $ext !='jpeg') ? 'imagecreatefrom' . $ext : 'imagecreatefromjpeg';
$img_r = call_user_func($func,$image);
$dst_r = ImageCreateTrueColor( $cutWidth, $cutHeight );
$back = ImageColorAllocate( $dst_r, 255, 255, 255 );
//将dst_r着色
ImageFilledRectangle( $dst_r, 0, 0, $cutWidth, $cutHeight, $back );
ImageCopyResampled( $dst_r, $img_r, 0, 0, $_s_x,$_s_y, $srcWidth*$sp, $srcHeight*$sp, $srcWidth, $srcHeight );
ImagePNG($dst_r, $cutfile);
imagedestroy($dst_r);
imagedestroy($img_r);
}else{
return false;
}
}其中最重要的一步为:
ImageCopyResampled( $dst_r, $img_r, 0, 0, $_s_x,$_s_y, $srcWidth*$sp, $srcHeight*$sp, $srcWidth, $srcHeight );
参数解释:
$dst_r : 需要生成的截图画布,在前面通过ImageCreateTrueColor生成,尺寸为截图需要的大小(这相当于是一块底布)
$img_r: 从原始图片中读取的图片数据
0,0 :忽略(应该是放到dst_r中的初始X,Y坐标)
$_s_x: 从原图中需要截图处起始的X坐标(左上角)
$_s_y:从原图中需要截取处起始的Y坐标(左上角)
$srcWidth*$sp:原图按照截取尺寸缩放后的宽度
$srcHeight*$sp: 原图按照截取尺寸缩放后的高度
$srcWidth: 对应 $img_r(原图)的宽度
$srcHeight:对应$img_r(原图)的高度
评论列表(0条)