<?php
$path = "/opt/case/img";
scan( $path."/swfjpg4/" );
scan( $path."/swfjpg3/" );
scan( $path."/swfjpg2/" );
scan( $path."/swfjpg/" );

function scan($path)
{
 $arr = scandir($path);
 foreach( $arr as $img )
 {
  if(strpos($img,".gif"))
  {
   //echo $img;
   $bImg = $path.$img;
   $sImg = $path.str_replace("gif","jpg",$img);
   if(!is_file($sImg))
    thumbImg( $bImg , $sImg , 100 , 75 );
  }
 }   
}

function thumbImg( $fullImg , $thumbImg , $width=100 , $height=75 , $fix=1 )
{
 if(!is_file($fullImg)){echo $fullImg ."-不存在<br>\n";return;}
 $imgInfo=getimagesize($fullImg);
 if($imgInfo[2] != 2 && !$gif ){echo("图片格式错误..");echo $fullImg."\n"; print_R($imgInfo); return;}
 if($imgInfo[2]==1)$gif=true;
 if($fix)
 {
  list($dstW,$dstH) = getImgWH( $fullImg, $width , $height , $fix );
 }
 else
 {
  $dstW = $width;
  $dstH = $height;
 }
 if($gif)
  $im = imagecreatefromgif($fullImg);
 else
  $im = imagecreatefromjpeg($fullImg);
 $ni = imagecreatetruecolor($dstW,$dstH);
 imagecopyresampled($ni,$im,0,0,0,0,$dstW,$dstH,$imgInfo['0'],$imgInfo['1']);
 if($gif)imagegif($ni,$thumbImg);
 else imagejpeg($ni,$thumbImg,80);
}

//等比换算图片宽高
function getImgWH($Img,$simgW,$simgH,$fixType=1)
{
 $imgInfo=getimagesize($Img);
 $iw = $imgInfo[0];
 $ih = $imgInfo[1];

 if($fixType==1)
 {//both
  $w = min( $iw , $simgW ); //只缩略,而不扩大。 根据宽度
  $h = floor($ih*($w/$iw));
 }
 elseif($fixType==2)
 {//width
  $w = $simgW;
  $h = floor($ih*($w/$iw));
 }
 elseif($fixType==3)
 {//height
  $h = $simgH;
  $w = floor($iw*($h/$ih));
 }
 else
 {
     $w = $simgW;
     $h = $simgH;
 }
 return array($w,$h);
}

 

?>

Posted on 2009-05-06 02:58  古代  阅读(321)  评论(0编辑  收藏  举报