TP 3.2 圖片處理類
阿新 • • 發佈:2018-12-31
arr dir 3.2 oot 一個數 logon sset message static
TP 3.2 圖片處理類
<?php namespace Common\Common; class ImageEdit{ /** * [上傳圖片並生成縮略圖] * @param [type] $imgName [圖片提交的表單名稱] * @param [type] $dirName [圖片上傳的二級目錄] * @param array $thumb * [縮略圖生成,一個數組,傳幾個值生成幾個圖片] * @return [type] [description] */ static public function uploadOne($imgName, $dirName, $thumb = array()) { // 上傳LOGO if(isset($_FILES[$imgName]) && $_FILES[$imgName][‘error‘] == 0) { $ic = C(‘IMAGE_CONFIG‘); $upload = new \Think\Upload(array( ‘rootPath‘ => $ic[‘rootPath‘], ‘maxSize‘ => $ic[‘maxSize‘], ‘exts‘ => $ic[‘exts‘], ));// 實例化上傳類 $upload->savePath = $dirName . ‘/‘; // 圖片二級目錄的名稱 // 上傳時指定一個要上傳的圖片的名稱,否則會把表單中所有的圖片都處理,之後再想其他圖片時就再找不到圖片了 $info = $upload->upload(array($imgName=>$_FILES[$imgName])); if(!$info) { return array( ‘ok‘ => 0, ‘error‘ => $upload->getError(), ); } else { $ret[‘ok‘] = 1; $ret[‘images‘][0] = $logoName = $info[$imgName][‘savepath‘] . $info[$imgName][‘savename‘]; // 判斷是否生成縮略圖 if($thumb) { $image = new \Think\Image(); // 循環生成縮略圖 foreach ($thumb as $k => $v) { $ret[‘images‘][$k+1] = $info[$imgName][‘savepath‘] . ‘thumb_‘.$k.‘_‘ .$info[$imgName][‘savename‘]; // 打開要處理的圖片 $image->open($ic[‘rootPath‘].$logoName); $image->thumb($v[0], $v[1])->save($ic[‘rootPath‘].$ret[‘images‘][$k+1]); } } return $ret; } } } /******************************************************* * 上傳圖片並生成縮略圖使用方法 * 用法: * $ret=uploadOne(‘logo‘,‘Goods‘,array( * array(600,600), * array(300,300), * array(100,100), * * )); * 返回值: * if($ret[‘ok‘]==1){ * $ret[‘images‘][0]; //原圖地址 * $ret[‘images‘][1]; //第一個縮略圖地址 * $ret[‘images‘][2]; //第二個縮略圖地址 * $ret[‘images‘][3]; //第三個縮略圖地址 * }else{ * $this->error=$ret[‘error‘]; * return false; * } ****************************************************/ /** * [delImage 刪除圖片函數] * @param [type] $img [圖片文件名數組] * @return [type] [description] */ static public function delImage($img){ foreach ($img as $k => $v) { unlink(C(‘IMAGE_CONFIG‘)[‘rootPath‘].$v); } } /** * [showImage 顯示圖片函數] * @param [type] $url [圖片名稱] * @param string $width [顯示寬度] * @param string $heigt [顯示高度] * @return [type] [description] */ static public function showImage($url,$width=‘‘,$height=‘‘){ $pt=C(‘IMAGE_CONFIG‘); if($width) $width="width=‘{$width}‘"; if($height) $height="height=‘{$height}‘"; echo "<img $width $height src=‘{$pt[‘viewPath‘]}$url‘ />"; } } /******************************************************************** * 圖片上傳類,多圖片上傳 * @param [type] $formname [file表單名稱] * @param [type] $dirname [圖片上傳二級目錄] * @return [type] [description] */ static public function uploadM($formname,$dirname){ if(isset($_FILES[$formname])){ $imgconf=C(‘IMAGE_CONFIG‘); $conf=[ ‘rootPath‘=>$imgconf[‘rootPath‘], ‘maxSize‘=>$imgconf[‘maxSize‘], ‘exts‘=>$imgconf[‘exts‘], ]; $dir=$imgconf[‘rootPath‘].$dirname; is_dir($dir)||mkdir($dir,0777,true); $img=new \Think\Upload($conf); $img->savePath =$dirname.‘/‘; $info=$img->upload(); if(!$info){ return [‘status‘=>‘error‘,‘message‘=>$img->getError()]; }else{ return $res=[‘status‘=>"error",‘message‘=>‘上傳成功‘,‘data‘=>$info]; } } }
TP 3.2 圖片處理類