php生成缩略图

php生成缩略图,并把它输出到网页上,不过需在php文件头加一句话:header("Content-Type:image/jpg");
代码如下:

<?php 

  header("Content-Type:image/jpg");
  class resizeImg {
     private $type;
     private $width;
     private $height;
     private $resize_width;
     private $resize_height;
     private $srcimg; //图片源地址
     private $desimg; //图片目的地址
     private $img; //新建临时文件
     private $cut; //判断是否截图
     private $name; //文件名

    function __construct($imgpath,$width,$height,$despath,$cut) { //图片路径,新宽度,新高度,目的地址,是否裁剪
       $this->srcimg = $imgpath;
       $this->resize_width = $width;
       $this->resize_height = $height;
       $this->cut = $cut;
       $this->type = strtolower(substr(strrchr($this->srcimg,"."),1));
       $this->init_img();
       $this->name_img(); //获取文件名
       $this->des_img($despath); //将文件放到指定文件
       $this->width = imagesx($this->img); //获取图像宽度
       $this->height = imagesy($this->img); //获取图像高度
       $this->new_img();
       imagedestroy($this->img);
    }

    function init_img() { //初始化图像
      if($this->type == "jpg") {
           $this->img = imagecreatefromjpeg($this->srcimg);
     }elseif($this->type == "gif"){
           $this->img = imagecreatefromgif($this->srcimg);
     }
     elseif($this->type == "png") {
           $this->img = imagecreatefrompng($this->srcimg);
     }
     else{
           echo "文件类型不对!!!";
     }
   }
   function des_img($despath) { //图象目标地址
       $this->desimg = $despath.$this->name.'.'.$this->type; //把完整地址赋给$desimg
   }
   function name_img() {
      $full_length = strlen($this->srcimg);
      $type_length = strlen($this->type);
      $name_length = $full_length-$type_length;
      $this->name = substr($this->srcimg,0,$name_length-1); //获取文件名
   }
   function new_img() { //新图像
      $resize_ratio = ($this->resize_width)/($this->resize_height); //新图像比例
      $ratio = ($this->width)/($this->height); //源图像比例

      if($this->cut == 1) { //截图
      if($ratio >= $resize_ratio) {
           $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height);
           imagecopyresampled($newimg, $this->img, 0, 0, 0, 0, $this->resize_width, $this->resize_height, ($this->height)*$resize_ratio, $this->height);
          imagejpeg($newimg,$this->desimg); //把图片的内容输出到$this->desimg上
          imagejpeg($newimg); //将图片输出到网页上

    }else {
          $newimg = imagecreatetruecolor($this->resize_width, $this->resize_height);
          imagecopyresampled($newimg, $this->img, 0, 0, 0, 0, $this->resize_width, $this->resize_height, ($this->width)/$resize_ratio, $this->width);

         imagejpeg($newimg,$this->desimg);
         imagejpeg($newimg); 将图片输出到网页上
     }
   }
   else { //不截图
      if($ratio >= $resize_ratio) {
         $newimg = imagecreatetruecolor($this->resize_width, ($this->resize_width)/$ratio); 
         imagecopyresampled($newimg, $this->img, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
         imagejpeg($newimg,$this->desimg);
         imagejpeg($newimg); 将图片输出到网页上
  }
   else {
         $newimg = imagecreatetruecolor(($this->resize_height)*$ratio, $this->resize_height);
         imagecopyresampled($newimg, $this->img, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);

         imagejpeg($newimg,$this->desimg);
         imagejpeg($newimg); // //将图片输出到网页上
      }
     }
   } 
 } 

 $resizeimg = new resizeImg("./1.jpg",200,200,"work/",0);
?>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值