PHP完全自学手册(珍藏版) 中文pdf扫描版下载
函数代码如下
define('WEB_PUBLIC_PATH','../images/');
function thumb($f, $tw=300, $th=300 ,$autocat=0, $nopic = 'nopic.gif',$t=''){
define('WEB_PUBLIC_PATH','../images/');
function thumb($f, $tw=300, $th=300 ,$autocat=0, $nopic = 'nopic.gif',$t=''){
if(empty($f)) return WEB_PUBLIC_PATH.$nopic;
$pathinfo = pathinfo($f);
if(empty($t)){
$t = $pathinfo['dirname'].'/thumb_'.$tw.'_'.$th.'_'.$pathinfo['basename'];
if(is_file($t)){
return $t;
}
}
$temp = array(1=>'gif', 2=>'jpeg', 3=>'png');
list($fw, $fh, $tmp) = getimagesize($f);
if(!$temp[$tmp]){ return false; }
if($autocat){
if($fw/$tw > $fh/$th){
$fw = $tw * ($fh/$th);
}else{
$fh = $th * ($fw/$tw);
}
}else{
$scale = min($tw/$fw, $th/$fh); // 计算缩放比例
if($scale>=1) {
// 超过原图大小不再缩略
$tw = $fw;
$th = $fh;
}else{
// 缩略图尺寸
$tw = (int)($fw*$scale);
$th = (int)($fh*$scale);
}
}
$tmp = $temp[$tmp];
$infunc = "imagecreatefrom$tmp";
$outfunc = "image$tmp";
$fimg = $infunc($f);
if($tmp != 'gif' && function_exists('imagecreatetruecolor')){
$timg = imagecreatetruecolor($tw, $th);
}else{
$timg = imagecreate($tw, $th);
}
if(function_exists('imagecopyresampled'))
imagecopyresampled($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh);
else
imagecopyresized($timg, $fimg, 0,0, 0,0, $tw,$th, $fw,$fh);
if($tmp=='gif' || $tmp=='png') {
$background_color = imagecolorallocate($timg, 0, 255, 0); // 指派一个绿色
imagecolortransparent($timg, $background_color); // 设置为透明色,若注释掉该行则输出绿色的图
}
$outfunc($timg, $t);
imagedestroy($timg);
imagedestroy($fimg);
return $t;
}
转载请注明:谷谷点程序 » php等比缩图_图片缩图的函数