zoukankan      html  css  js  c++  java
  • 【php】png 图片压缩 透明底色变黑

    需要使用gd库的方法 php需要引入gd扩展支持
    /*
     * 图片压缩
    ----------------------------------------------------------------------
    函数:调整图片尺寸或生成缩略图
    返回:True/False
    参数:
      $Image  需要调整的图片(含路径)
      $Dw=450  调整时最大宽度;缩略图时的绝对宽度
      $Dh=450  调整时最大高度;缩略图时的绝对高度
      $Type=1  1,调整尺寸; 2,生成缩略图
    $path='img/';//路径
    $phtypes=array(
      'img/gif',
      'img/jpg',
      'img/jpeg',
      'img/bmp',
      'img/pjpeg',
      'img/x-png'
        返回图片名称,不含路径
    );
    */
    public function Img($Image,$exif=0,$Dw=450,$Dh=450,$Type=1){
      if(!file_exists($Image)){
        return false;
      }
      //如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值
      IF($Type!=1){
          copy($Image,str_replace(".","_x.",$Image));
          $Image=str_replace(".","_x.",$Image);
      }
      //取得文件的类型,根据不同的类型建立不同的对象
      $ImgInfo=getimagesize($Image);
      switch($ImgInfo[2]){
          case 1:
          $Img = @imagecreatefromgif($Image);
          Break;
          case 2:
          $Img = @imagecreatefromjpeg($Image);
          Break;
          case 3:
          $Img = @imagecreatefrompng($Image);
          Break;
      }
      //如果对象没有创建成功,则说明非图片文件
      if(empty($Img)){
          //如果是生成缩略图的时候出错,则需要删掉已经复制的文件
          if($Type!=1){
              unlink($Image);
          }
          return false;
      }
      //如果是执行调整尺寸操作则
      $w=imagesx($Img);
      $h=imagesy($Img);
      $width = $w;
      $height = $h;
      if($Type==1){
          if($width>$Dw){
               $Par=$Dw/$width;
               $width=$Dw;
               $height=$height*$Par;
               if($height>$Dh){
                   $Par=$Dh/$height;
                   $height=$Dh;
                   $width=$width*$Par;
               }
          }elseif($height>$Dh){
               $Par=$Dh/$height;
               $height=$Dh;
               $width=$width*$Par;
          }else{
               $width=$width;
               $height=$height;
          }
          if($ImgInfo[2] == 3)
          {
              imagesavealpha($Img,true);
          }
          $nImg = imagecreatetruecolor($width,$height);   //新建一个真彩色画布
          if($ImgInfo[2] == 3)
          {
              imagealphablending($nImg,false);
              imagesavealpha($nImg,true);
          }
    
          imagecopyresampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);//重采样拷贝部分图像并调整大小
          //如果是执行生成缩略图操作则
      }else{
          $nImg = imagecreatetruecolor($Dw,$Dh);
          if($h/$w>$Dh/$Dw){ //高比较大
               $height=$h*$Dw/$w;
               $IntNH=$height-$Dh;
               imagecopyresampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
          }else{   //宽比较大
               $width=$w*$Dh/$h;
               $IntNW=$width-$Dw;
               imagecopyresampled($nImg, $Img, -$IntNW/1.8, 0, 0, 0, $width, $Dh, $w, $h);
          }
      }
        if(!empty($exif))
        {
            switch ($exif) {
                case 8:
                    $image = imagerotate($nImg, 90, 0);
                    break;
                case 3:
                    $image = imagerotate($nImg, 180, 0);
                    break;
                case 6:
                    $image = imagerotate($nImg, -90, 0);
                    break;
            }
        }
        switch($ImgInfo[2]){
            case 1:
                imagegif($nImg,$Image);
                Break;
            case 2:
                imagejpeg($nImg,$Image);
                Break;
            case 3:
                imagepng($nImg,$Image);
                Break;
        }
    
      $fileDirArr = explode('/',$Image);
      return end($fileDirArr);
    }
    
  • 相关阅读:
    不同主机的docker容器互相通信
    Understanding Docker
    Docker入门
    使用Docker在本地搭建Hadoop分布式集群
    Cassandra联手Spark 大数据分析将迎来哪些改变?
    Apache Spark技术实战之6 --Standalone部署模式下的临时文件清理
    大数据计算平台Spark内核全面解读
    Ubuntu下导入PySpark到Shell和Pycharm中(未整理)
    别老扯什么hadoop,你的数据根本不够大
    spark on yarn 集群部署
  • 原文地址:https://www.cnblogs.com/china-flint/p/9685289.html
Copyright © 2011-2022 走看看