<?php /** * Created by PhpStorm. * User: xiaofeng * Date: 2018/4/10 * Time: 上午10:10 */ namespace backend\server; use Yii; //use Tinify\Tinify; use yii\log\FileTarget; use common\models\TinypngRecord; class TinyPng { /* * 处理压缩图片 * @params imgname 图片名称 可以是 string and array * @params url 图片路径,绝对路径 * @params uid 用户ID * @params source 来源 * */ public function CompressImg($data) { // if(TINYPNG == true) // { // try{ // // if(is_string($data['imgname'])) // { // $data['imgname'] = explode(',',$data['imgname']); // } // $this->SetKey(); // $tinyModel = new TinypngRecord(); // foreach ($data['imgname'] as &$val) // { // $path = $data['url'].$val; //// $imgSize = getimagesize($path); // $Filesize = filesize($path); // if($Filesize != false) // { // $source = \Tinify\fromFile($path); // $result = $source->toFile($path); //成功返回字节数 // if($result > 1) // { // $_model = clone $tinyModel; // $_model->img_name = $val; // $_model->path = $data['url']; // $_model->uid = $data['uid']; // $_model->source = $data['source']; // $_model->end_size = $result; // $_model->start_size = $Filesize; // $_model->insert(false); // } // } // } // }catch (\Exception $e){ // $log = new FileTarget(); // $log->logFile = Yii::$app->getRuntimePath().'/tinify/'.date('Ymd').'.log'; // $log->messages[] = [json_encode(Yii::$app->request->post().'错误信息:'.$e),1,'application',microtime(true)]; // $log->export(); // } // } } private function SetKey() { // \Tinify\setKey(Yii::$app->params['TinifyApiKey']); } }