//图片批量处
$src="100/2020/12";
list_file($src);
function list_file($date){
//1、首先先读取文件夹
$temp=scandir($date);
//遍历文件夹
foreach($temp as $v){
$a=$date.'/'.$v;
if(is_dir($a)){//如果是文件夹则执行
if($v=='.' || $v=='..'){//判断是否为系统隐藏的文件.和.. 如果是则跳过否则就继续往下走,防止无限循环再这里。
continue;
}
//echo "<font color='red'>$a</font>","<br/>"; //把文件夹红名输出
list_file($a);//因为是文件夹所以再次调用自己这个函数,把这个文件夹下的文件遍历出来
}else{
echo $a,"<br/>";
$b=str_replace('100/','48/',$a);
$dir=preg_replace("/\d*.(jpg|gif|bmp|bnp|png)/",'',$b);
if (!file_exists($dir))
{
mkdir($dir,0777,true);
}
makethumb($a,$dir,100,100);
}
}
}
function makethumb($srcfile,$dir,$thumbwidth,$thumbheight,$ratio=1)
{
//判断文件是否存在
if (!file_exists($srcfile))return false;
//生成新的同名文件,但目录不同
$imgname=explode('/',$srcfile);
$arrcount=count($imgname);
$dstfile = $dir.$imgname[$arrcount-1];
//缩略图大小
$tow = $thumbwidth;
$toh = $thumbheight;
if($tow < 40) $tow = 40;
if($toh < 45) $toh = 45;
//获取图片信息
$im ='';
if($data = getimagesize($srcfile)) {
if($data[2] == 1) {
$make_max = 0;//gif不处理
if(function_exists("imagecreatefromgif")) {
$im = imagecreatefromgif($srcfile);
}
} elseif($data[2] == 2) {
if(function_exists("imagecreatefromjpeg")) {
$im = imagecreatefromjpeg($srcfile);
}
} elseif($data[2] == 3) {
if(function_exists("imagecreatefrompng")) {
$im = imagecreatefrompng($srcfile);
//$im = imagecreatetruecolor(200, 200);
// $im = imagecreatetruecolor(200, 200);
// //2.上色
// $color=imagecolorallocate($im,255,255,255);
// //3.设置透明
// imagecolortransparent($im,$color);
// imagefill($im,0,0,$color);
}
}
}
if(!$im) return '';
$srcw = imagesx($im);
$srch = imagesy($im);
$towh = $tow/$toh;
$srcwh = $srcw/$srch;
if($towh <= $srcwh){
$ftow = $tow;
$ftoh = $ftow*($srch/$srcw);
} else {
$ftoh = $toh;
$ftow = $ftoh*($srcw/$srch);
}
if($ratio){
$ftow = $tow;
$ftoh = $toh;
}
//缩小图片
if($srcw > $tow || $srch > $toh || $ratio) {
if(function_exists("imagecreatetruecolor") && function_exists("imagecopyresampled") && @$ni = imagecreatetruecolor($ftow, $ftoh)) {
imagecopyresampled($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
} elseif(function_exists("imagecreate") && function_exists("imagecopyresized") && @$ni = imagecreate($ftow, $ftoh)) {
imagecopyresized($ni, $im, 0, 0, 0, 0, $ftow, $ftoh, $srcw, $srch);
} else {
return '';
}
if(function_exists('imagejpeg')) {
imagejpeg($ni, $dstfile);
} elseif(function_exists('imagepng')) {
imagepng($ni, $dstfile);
}
}else {
//小于尺寸直接复制
copy($srcfile,$dstfile);
}
imagedestroy($im);
if(!file_exists($dstfile)) {
return '';
} else {
return $dstfile;
}
}