From 2ee54df1660a62403e063d703403e0fe9468add4 Mon Sep 17 00:00:00 2001 From: banditsmile Date: Fri, 15 Mar 2019 11:34:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8E=8B=E7=BC=A9=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=87=8D=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/controllers/Compress.php | 53 ---------------------------- application/controllers/Deal.php | 46 +++--------------------- application/libraries/Image.php | 10 ++++-- 3 files changed, 11 insertions(+), 98 deletions(-) diff --git a/application/controllers/Compress.php b/application/controllers/Compress.php index f4f82a9..9c4d72a 100644 --- a/application/controllers/Compress.php +++ b/application/controllers/Compress.php @@ -50,59 +50,6 @@ $info = json_encode($info); echo $info; } - - - } - //请求tinypng压缩接口,传入图片完整路径 - protected function tinypng($path){ - - //tinypng API地址 - $api_url = "https://api.tinify.com/shrink"; - $data = file_get_contents($path); - //$post_data = array ("username" => "bob","key" => "12345"); - //$ch = curl_init(); - $ch = curl_init(); - $user = "api"; - $pass = "F8rNr5lh25WYcOECQvAqvcilBMAkhtIM"; - curl_setopt($ch, CURLOPT_URL, $api_url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$pass}"); - // post数据 - curl_setopt($ch, CURLOPT_POST, 1); - // post的变量 - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - //https - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - $output = curl_exec($ch); - curl_close($ch); - //打印获得的数据 - $data = json_decode($output); - //获取图片压缩后的URL - $url = $data->output->url; - //保存图片 - $this->save($url,$path); - } - //传递图片URL,并保存文件 - protected function save($url,$path){ - //获取图片数据并保存 - $curl = curl_init($url); - - curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"); - curl_setopt($curl, CURLOPT_FAILONERROR, true); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); - #设置超时时间,最小为1s(可选) - #curl_setopt($curl , CURLOPT_TIMEOUT, 1); - - $filedata = curl_exec($curl); - curl_close($curl); - - //将图片数据覆盖源文件 - file_put_contents($path,$filedata); } } ?> \ No newline at end of file diff --git a/application/controllers/Deal.php b/application/controllers/Deal.php index abcfc57..ed06604 100644 --- a/application/controllers/Deal.php +++ b/application/controllers/Deal.php @@ -48,51 +48,13 @@ $kyes = json_decode($kyes); $i = 'api'.rand(1,2); $key = $kyes->$i; - - $url = "https://api.tinify.com/shrink"; - $data = file_get_contents($path); - - $ch = curl_init(); - $user = "api"; - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); - curl_setopt($ch, CURLOPT_USERPWD, "{$user}:{$key}"); - // post数据 - curl_setopt($ch, CURLOPT_POST, 1); - // post的变量 - curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - //https - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); - $output = curl_exec($ch); - curl_close($ch); - $output = json_decode($output); - //获取压缩后的图片链接 - $outurl = $output->output->url; - //先判断是否是正常的URL,万一请求接口失败了呢 - if(!filter_var($outurl, FILTER_VALIDATE_URL)){ - //糟糕,没有验证通过,那就结束了 - $this->err_msg('请求接口失败!'); + $this->load->library('image'); + $ret = $this->image->compress($path); + if(empty($ret)) { + $this->err_msg('压缩失败,请稍后重试!'); } - //下载图片并保存覆盖 - $curl = curl_init($outurl); - curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"); - curl_setopt($curl, CURLOPT_FAILONERROR, true); - curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); - #设置超时时间,最小为1s(可选) - - $data = curl_exec($curl); - curl_close($curl); - - //重新保存图片 - file_put_contents($path,$data); - //最后别忘了更新数据库呀 $sql = "UPDATE img_images SET compression = 1 WHERE `id` = $id"; if($this->db->query($sql)){ diff --git a/application/libraries/Image.php b/application/libraries/Image.php index efc9c72..c74cd61 100644 --- a/application/libraries/Image.php +++ b/application/libraries/Image.php @@ -38,14 +38,14 @@ $image->clear(); } //压缩图片 - public function compress($source,$output='',$channel='tinypng'){ + public function compress($source, $output='', $channel='tinypng') { if (empty($output)) { $output = $source; } switch ($channel) { case 'tinypng': $key = "F8rNr5lh25WYcOECQvAqvcilBMAkhtIM"; - $this->tinypng($source,$output, $key); + return $this->tinypng($source,$output, $key); default : return $source; } @@ -82,8 +82,12 @@ $data = json_decode($output); //获取图片压缩后的URL $url = $data->output->url; + //先判断是否是正常的URL,万一请求接口失败了呢 + if (!filter_var($url, FILTER_VALIDATE_URL)) { + return false; + } //保存图片 - $this->download($url, $outputPath); + return $this->download($url, $outputPath); } //传递图片URL,并保存文件