真正好用的生成图片缩略图的函数
时间:2009-04-11
来源:互联网
工作中遇到不少需要生产缩略图的地方,以前用别人的函数,生成的缩略图不是对原图进行了裁剪就是宽度或者高度不够,写的这个函数能生成指定宽度跟高度的图片,原图不进行任何裁剪,可根据需要是否保留原图,缩略图的保存路径自定义。欢迎拍砖!
[php]
<?php
/*
生产缩略图主函数,返回缩略图路径
@src1:图片地址
@w,@h:缩略图宽高
@path:缩略图保存路径
@needDel:生产缩略图后是否删除原图
*/
function resize($src1,$w,$h,$path,$needDel)
{
$info=getimagesize($src1);
$width=$info[0];
$height=$info[1];
$imageType=$info[2];
$im=create($src1,$imageType);
$per1=round($width/$height,2);//计算原图长宽比
$per2=round($w/$h,2);//计算缩略图长宽比
if($per1>$per2)
{
$per=$w/$width;
}
if($per1<$per2)
{
$per=$h/$height;
}
if($per1==$per2)
{
$per=$w/$width;
}
$temp_w=intval($width*$per);//计算原图缩放后的宽度
$temp_h=intval($height*$per);//计算原图缩放后的高度
$temp_img=imagecreatetruecolor($temp_w,$temp_h);
if($per1>$per2)
{
//宽度优先
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
$newName=$path.rename2($src1,false);
imagejpeg($temp_img,$newName);
//在缩放之后高度不足的情况下补上背景
return addBg($newName,$w,$h,"w",$path,$needDel);
}
if($per1==$per2)
{
//等比缩放
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
$newName=$path.rename2($src1,false);
imagejpeg($temp_img,$newName);
imagedestroy($temp_img);
imagedestroy($im);
return $newName;
}
if($per1<$per2)
{
//高度优先
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
$newName=$path.rename2($src1,false);//一次生成
imagejpeg($temp_img,$newName);
//在缩放之后宽度不足的情况下补上背景
return addBg($newName,$w,$h,"h",$path,$needDel);
}
}
/*
重名名文件
*/
function rename2($file,$re=true)
{
$info=pathinfo($file);
if($re)
{
$filename=rtrim($info["basename"],".".$info["extension"]);
return $filename."_2.".$info["extension"];
}
else
{
return $info["basename"];
}
}
function create($src,$imagetype)
{
switch($imagetype)
{
case 1 :
$im = imagecreatefromgif($src);
break;
case 2 :
$im = imagecreatefromjpeg($src);
break;
case 3 :
$im = imagecreatefrompng($src);
break;
}
return $im;
}
/*
添加背景
@src_img:图片src
@w:宽
@h:高
@first:决定宽度还是高度优先
@path:缩略图保存的位置
@needDel:是否删除原图
*/
function addBg($src_img,$w,$h,$fisrt="w",$path,$needDel=false)
{
$bg=imagecreatetruecolor($w,$h);
$white = imagecolorallocate($bg,255,255,255);
imagefill($bg,0,0,$white);
$temp_info=getimagesize($src_img);
$im2=create($src_img,$temp_info[2]);
$width2=$temp_info[0];
$height2=$temp_info[1];
if($fisrt=="w")
{
//宽度优先,解决缩放后高度不足
$y=($h-$height2)/2;
imagecopymerge($bg,$im2,0,$y,0,0,$width2,$height2,100);
$file=$path.rename2($src_img,1);
imagejpeg($bg,$file);
imagedestroy($bg);
imagedestroy($im2);
if($needDel==true)
{
@unlink($src_img);
}
return $file;
}
if($fisrt=="wh")
{
//等比缩放
return $src_img;
}
if($fisrt=="h")
{
$x=($w-$width2)/2;
//高度优
imagecopymerge($bg,$im2,$x,0,0,0,$width2,$height2,100);
$file=$path.rename2($src_img,1);
imagejpeg($bg,$file);
imagedestroy($bg);
imagedestroy($im2);
if($needDel==true)
{
@unlink($src_img);
}
return $file;
}
}
/***********For example**********/
$pic=resize("images/1.jpg",210,140,"../upload/",true);
?>
[/php]
[php]
<?php
/*
生产缩略图主函数,返回缩略图路径
@src1:图片地址
@w,@h:缩略图宽高
@path:缩略图保存路径
@needDel:生产缩略图后是否删除原图
*/
function resize($src1,$w,$h,$path,$needDel)
{
$info=getimagesize($src1);
$width=$info[0];
$height=$info[1];
$imageType=$info[2];
$im=create($src1,$imageType);
$per1=round($width/$height,2);//计算原图长宽比
$per2=round($w/$h,2);//计算缩略图长宽比
if($per1>$per2)
{
$per=$w/$width;
}
if($per1<$per2)
{
$per=$h/$height;
}
if($per1==$per2)
{
$per=$w/$width;
}
$temp_w=intval($width*$per);//计算原图缩放后的宽度
$temp_h=intval($height*$per);//计算原图缩放后的高度
$temp_img=imagecreatetruecolor($temp_w,$temp_h);
if($per1>$per2)
{
//宽度优先
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
$newName=$path.rename2($src1,false);
imagejpeg($temp_img,$newName);
//在缩放之后高度不足的情况下补上背景
return addBg($newName,$w,$h,"w",$path,$needDel);
}
if($per1==$per2)
{
//等比缩放
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
$newName=$path.rename2($src1,false);
imagejpeg($temp_img,$newName);
imagedestroy($temp_img);
imagedestroy($im);
return $newName;
}
if($per1<$per2)
{
//高度优先
imagecopyresampled($temp_img,$im,0,0,0,0,$temp_w,$temp_h,$width,$height);
$newName=$path.rename2($src1,false);//一次生成
imagejpeg($temp_img,$newName);
//在缩放之后宽度不足的情况下补上背景
return addBg($newName,$w,$h,"h",$path,$needDel);
}
}
/*
重名名文件
*/
function rename2($file,$re=true)
{
$info=pathinfo($file);
if($re)
{
$filename=rtrim($info["basename"],".".$info["extension"]);
return $filename."_2.".$info["extension"];
}
else
{
return $info["basename"];
}
}
function create($src,$imagetype)
{
switch($imagetype)
{
case 1 :
$im = imagecreatefromgif($src);
break;
case 2 :
$im = imagecreatefromjpeg($src);
break;
case 3 :
$im = imagecreatefrompng($src);
break;
}
return $im;
}
/*
添加背景
@src_img:图片src
@w:宽
@h:高
@first:决定宽度还是高度优先
@path:缩略图保存的位置
@needDel:是否删除原图
*/
function addBg($src_img,$w,$h,$fisrt="w",$path,$needDel=false)
{
$bg=imagecreatetruecolor($w,$h);
$white = imagecolorallocate($bg,255,255,255);
imagefill($bg,0,0,$white);
$temp_info=getimagesize($src_img);
$im2=create($src_img,$temp_info[2]);
$width2=$temp_info[0];
$height2=$temp_info[1];
if($fisrt=="w")
{
//宽度优先,解决缩放后高度不足
$y=($h-$height2)/2;
imagecopymerge($bg,$im2,0,$y,0,0,$width2,$height2,100);
$file=$path.rename2($src_img,1);
imagejpeg($bg,$file);
imagedestroy($bg);
imagedestroy($im2);
if($needDel==true)
{
@unlink($src_img);
}
return $file;
}
if($fisrt=="wh")
{
//等比缩放
return $src_img;
}
if($fisrt=="h")
{
$x=($w-$width2)/2;
//高度优
imagecopymerge($bg,$im2,$x,0,0,0,$width2,$height2,100);
$file=$path.rename2($src_img,1);
imagejpeg($bg,$file);
imagedestroy($bg);
imagedestroy($im2);
if($needDel==true)
{
@unlink($src_img);
}
return $file;
}
}
/***********For example**********/
$pic=resize("images/1.jpg",210,140,"../upload/",true);
?>
[/php]
作者: dongxin1390008 发布时间: 2009-04-11


作者: fengying 发布时间: 2009-04-13
不错。。。。顶了。。。
不过3个函数,又有相互关联,建议封装成类,更易维护。。。。
不过3个函数,又有相互关联,建议封装成类,更易维护。。。。
作者: kakashilw 发布时间: 2009-04-13



作者: wlxm007 发布时间: 2009-04-14
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28