当前位置: 移动技术网 > IT编程>开发语言>PHP > 生成缩略图

生成缩略图

2019年05月22日  | 移动技术网IT编程  | 我要评论

生成缩略图 $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);
           // 如果需要其它格式的图片,改动最后一句就可以了
  }

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网