当前位置:首页 > PHP教程 > PHP基础知识

生成缩略图


生成缩略图 $tx=getimagesize($sample);
  if($tx[0]<=$tx[1] and $tx[1]>=120){
     $height=120;
     $width=intval($height*$tx[0]/$tx[1]);
  }
  if($tx[0]>=$tx[1] and $tx[0]>=100){
     $width=100;
     $height=intval($width*$tx[1]/$tx[0]);
  }
  if($tx[0]<100 and $tx[1]<120){
     $width=$tx[0];
     $height=$tx[1];
  }

  makethumb2($sample,$target,$width,$height);

  // $srcfile: 源文件
  // $dstfile: 目标文件
  // $dstw: 目标图片宽度
  // $dsth: 目标文件高度
  function makethumb2($srcfile,$dstfile,$dstw,$dsth){
           $data=getimagesize($srcfile,&$info);
           switch($data[2]){
                  case 1:
                       $im=@imagecreatefromgif($srcfile);
                       break;
                  case 2:
                       $im=@imagecreatefromjpeg($srcfile);
                       break;
                  case 3:
                       $im=@imagecreatefrompng($srcfile);
                       break;
           }
           $srcw=imagesx($im);
           $srch=imagesy($im);
           $ni=imagecreate($dstw,$dsth);
           imagecopyresized($ni,$im,0,0,0,0,$dstw,$dsth,$srcw,$srch);
           imagejpeg($ni,$dstfile);
           // 如果需要输出到浏览器,那么将上一句改为imagejpeg($ni);
           // 如果需要其它格式的图片,改动最后一句就可以了
  }

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!