博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php中使用curl发送请求和图片压缩
阅读量:7077 次
发布时间:2019-06-28

本文共 3468 字,大约阅读时间需要 11 分钟。

简单自用封装:在helper文件下新建curl_helper.php, 如果系统存在则命名前需加MY_,autoload中设置自动加载,则可以直接使用:echo send_get($url)

// 使用curl发送请求function send_get($url){    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_HEADER, false);    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);  // 返回数据,不直接输出,否则数据会追加一个布尔值    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求不验证证书    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // https请求不验证host    $data = curl_exec($ch);    curl_close($ch);    return $data;}

 图片压缩

/* 图片等比压缩函数 * @param $maxwidth 最大宽度   * @param $maxheight 最大高度   * @param $name 压缩图片名   * @param $filetype 图片类型   */  function resizeImage($orgin_file,$maxwidth,$maxheight,$name,$filetype)  {      $im = '';      switch ($filetype){          case '.jpg':              $im=imagecreatefromjpeg($orgin_file);              break;          case '.png':              $im=imagecreatefrompng($orgin_file);              break;          case '.gif':              $im=imagecreatefromgif($orgin_file);              break;          default:              $im=imagecreatefromstring(file_get_contents($orgin_file));      }      $pic_width = imagesx($im);      $pic_height = imagesy($im);        if(($maxwidth && $pic_width > $maxwidth) || ($maxheight && $pic_height > $maxheight))      {          if($maxwidth && $pic_width>$maxwidth)          {              $widthratio = $maxwidth/$pic_width;              $resizewidth_tag = true;          }            if($maxheight && $pic_height>$maxheight)          {              $heightratio = $maxheight/$pic_height;              $resizeheight_tag = true;          }            if($resizewidth_tag && $resizeheight_tag)          {              if($widthratio<$heightratio)                  $ratio = $widthratio;              else                  $ratio = $heightratio;          }            if($resizewidth_tag && !$resizeheight_tag)              $ratio = $widthratio;          if($resizeheight_tag && !$resizewidth_tag)              $ratio = $heightratio;            $newwidth = $pic_width * $ratio;          $newheight = $pic_height * $ratio;            if(function_exists("imagecopyresampled"))          {              $newim = imagecreatetruecolor($newwidth,$newheight);//PHP系统函数              imagecopyresampled($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);//PHP系统函数          }          else          {              $newim = imagecreate($newwidth,$newheight);              imagecopyresized($newim,$im,0,0,0,0,$newwidth,$newheight,$pic_width,$pic_height);          }            $name = $name.$filetype;          switch ($filetype){              case '.jpg':                  imagejpeg($newim,$name);                  break;              case '.png':                  imagepng($newim,$name);                  break;              case '.gif':                  imagegif($newim,$name);                  break;              default:                  imagegd($newim,$name);          }            imagedestroy($newim);      }      else      {          $name = $name.$filetype;          switch ($filetype){              case '.jpg':                  imagejpeg($im,$name);                  break;              case '.png':                  imagepng($im,$name);                  break;              case '.gif':                  imagegif($im,$name);                  break;              default:                  imagegd($im,$name);          }      }  }

 

转载于:https://www.cnblogs.com/maoriaty/p/9093463.html

你可能感兴趣的文章
WINDOWS上数据灾难后专业的现场保护方法
查看>>
如何挖掘NAND Flash的IO性能
查看>>
微软私有云分享(R2)19Azure Pack与远程控制台
查看>>
探查 SQL Server 虚拟日志文件
查看>>
aix6 11G RAC 异机恢复至单实例测试
查看>>
购华为第1书,写书评赢大奖
查看>>
Tcpdump的详细用法
查看>>
Python学习笔记一(Python数据类型)
查看>>
咬定青山、立根破岩
查看>>
生产环境大于254台机器网段划分及路由解决方案详解01
查看>>
“春Phone计划”51CTO沙龙武汉站分享
查看>>
传统项目跟互联网结合的详细推广方法
查看>>
美团点评上市,王兴的野心和局限
查看>>
《基于插件的日志采集技术实践》幻灯片下载
查看>>
Exchange Server 2010 SP2 命令行部署
查看>>
用python和redis开发高性能监控平台及框架升级过程
查看>>
Idc服务都白菜价儿了,云化还是问题吗
查看>>
corosync+pacemaker+http高可用操作手记
查看>>
MySQL压力测试工具 mysqlslap
查看>>
QTP IDE中的垂直分割选取
查看>>