php生成缩略图

来源:互联网 发布:鲁荣渔2682惨案 知乎 编辑:程序博客网 时间:2024/06/06 16:37

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);?>



原创粉丝点击