php 图片压缩 CURL 请求 删除文件夹目录和子文件 创建 目录

<?php

namespace App\Http\Controllers\Commons;

/**
 * 公共类
 * 樊佑剑
 * 20150505
 * 功能 :给该系统提供所有公共方法调用
 */

class Util
{


    /**
     *  清除 缓存文件
     *  f y j
     *  
     */
    public static function deleteStoreFile($file)
    {
        $filedata = explode('/', $file);

        $num = count($filedata);
        $newfile_tmp= '';
        $newfile = '';
        foreach ($filedata as $key => $value) {
            if(($key+2)<=$num)
            {
                $newfile .=empty($key)?$value:'/'.$value;
            }

            if(($key+1)<=$num-2)
            {
                $newfile_tmp .=empty($key)?$value:'/'.$value;
            }
        }
        if (file_exists($file)) {
            Util::deldir($newfile);
            rmdir($newfile_tmp);
        }
    }

    /**
     *  清空 文件夹下面所有 子文件夹
     * @param  [type] $dir [description]
     * @return [type]      [description]
     */
    public static function deldir($dir) {
          //先删除目录下的文件:
          $dh=opendir($dir);
          while ($file=readdir($dh)) {
            if($file!="." && $file!="..") {
              $fullpath=$dir."/".$file;

              if(!is_dir($fullpath)) {
                  unlink($fullpath);
              } else {
                  Util::deldir($fullpath);
              }
            }
          }
          closedir($dh);
          if(rmdir($dir)) {
            return true;
          }
          return false;
    }


  
    /** 
     * 创建 目录
     * @param  [type] $dir [description]
     * @return [type]      [description]
     */
    public static function createDir($dir)
    {
        return is_dir($dir) or (Util::createDir(dirname($dir)) and mkdir($dir, 0777));
    }

    /**
     * 等比例输小图片
     * type 1 直播 2 新闻 3 讲者
     * @param  [type] $oldfilepath  原始文件
     * @param  [type] $per       输小比例
     * @param  [type] $nfilepath 文件新路径
     * @param [type]  $extention 图片格式 png jpeg jpg
     */
    public static function resetImage($type,$oldfilepath,$per,$newfilename)
    {

      $thum_file_path='';
      switch ($type) {
        case '1':
          # code...
          $thum_file_path = config('msgconfig.UPLOAD_FILE_PATH').'/'.date("Ymd");
          break;
        case '2':
          $thum_file_path = config('msgconfig.UPLOAD_FILE_NEWS').'/'.date("Ymd");
          # code...
          break;
        case '3':
          # code...
          $thum_file_path = config('msgconfig.UPLOAD_FILE_HEADER_PATH').'/'.date("Ymd");
          break;
        default:
          # code...
          break;
      }
      

      list($width, $height)=getimagesize($oldfilepath);
      $n_w=$width*$per;
      $n_h=$height*$per;
      $new=imagecreatetruecolor($n_w, $n_h);
      
      $imginfo = getimagesize($oldfilepath);

      $type = $imginfo[2];

      switch ($type) {
        case '2':
          $img = imagecreatefromjpeg($oldfilepath);
          break;
        case '3':
          # code...
          $img = imagecreatefrompng($oldfilepath);
          break;
      }
      // $img=imagecreatefromjpeg($filename);
      //copy部分图像并调整
      imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
      //图像输出新图片、另存为
      imagejpeg($new, $thum_file_path.'/'.$newfilename);
      imagedestroy($new);
      imagedestroy($img);

      // return $nfilepath.'/'.$newfilename;
    }

   
  

   /**
    * 调用远程API
    * $type 1 登录验证  2 注册  3 更新
    * $param 提交数据
    * $param['username'=>eee,'password'=>'111']
    * @return json_decode();
    */
   public static function apiPostPush($type,$param)
   {
      $url='';
      switch ($type) {
         case '1':
            $url = config('msgconfig.API_LOGIN');
          
            break;
         case '2':
            $url = config('msgconfig.API_REGISTER');
            
            break;
      }
    
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//1 输出 html 
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS,$param);
      $result = curl_exec($ch);
      curl_close($ch);
      
      return json_decode($result);
   }
   /**
    * 远程调用 API
    * get 方式
    */
//   public static function apiGetPush($url)
//   {
//      $ch = curl_init($url);
//      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回
//      curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ; // 在启用 CURLOPT_RETURNTRANSFER 时候将获取数据返回
//      $this->result = curl_exec($ch) ;
//      curl_close($ch);
//      return $this->result;
//   }
  
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值