public function actionUpload() { $uptype = zmf::filterInput($_GET['type'], 't', 1); $logid = zmf::filterInput($_GET['id']); //所属对象 $reImgsize = zmf::filterInput($_GET['imgsize']); //返回图片的尺寸 $fileholder = zmf::filterInput($_GET['fileholder'], 't', 1); //上传控件的ID //将ads替换为flash if (!isset($uptype) or !in_array($uptype, array('siteinfo', 'post'))) { $this->jsonOutPut(0, '请设置上传所属类型' . $uptype); } if (Yii::app()->request->getParam('PHPSESSID')) { Yii::app()->session->close(); $res = Yii::app()->session->setSessionID(Yii::app()->request->getParam('PHPSESSID')); Yii::app()->session->open(); } if (Yii::app()->user->isGuest) { $this->jsonOutPut(0, Yii::t('default', 'loginfirst')); } if (!$fileholder) { $fileholder = 'filedata'; } if (!isset($_FILES[$fileholder]) || !is_uploaded_file($_FILES[$fileholder]["tmp_name"]) || $_FILES[$fileholder]["error"] != 0) { $this->jsonOutPut(0, '无效上传,请重试'); } $model = new Attachments(); $img = CUploadedFile::getInstanceByName($fileholder); $ext = $img->getExtensionName(); $size = $img->getSize(); if ($size > zmf::config('imgMaxSize')) { $this->jsonOutPut(0, '上传文件最大尺寸为:' . tools::formatBytes(zmf::config('imgMaxSize'))); } $upExt = zmf::config("imgAllowTypes"); if (!preg_match('/^(' . str_replace('*.', '|', str_replace(';', '', $upExt)) . ')$/i', $ext)) { $this->jsonOutPut(0, '上传文件扩展名必需为:' . $upExt); } $sizeinfo = getimagesize($_FILES[$fileholder]["tmp_name"]); if ($sizeinfo['0'] < zmf::config('imgMinWidth') or $sizeinfo[1] < zmf::config('imgMinHeight')) { $this->jsonOutPut(0, "要求上传的图片尺寸,宽不能不小于" . zmf::config('imgMinWidth') . "px,高不能小于" . zmf::config('imgMinHeight') . "px."); } $ctime = zmf::now(); $dirs = zmf::uploadDirs($ctime, 'app', $uptype, null, true); $fileName = uniqid() . '.' . $ext; $origin = $dirs['origin']; unset($dirs['origin']); $uploadedFiles = array(); $uploadedFiles[] = array('from' => $origin . $fileName, 'to' => zmf::ftpPath($ctime, $uptype, 'origin') . $fileName); if (move_uploaded_file($_FILES[$fileholder]["tmp_name"], $origin . $fileName)) { $data = array(); $status = Posts::STATUS_PASSED; $data['uid'] = Yii::app()->user->id; $data['logid'] = $logid; $data['filePath'] = $fileName; $data['fileDesc'] = $fileName; $data['classify'] = $uptype; $data['covered'] = '0'; $data['cTime'] = time(); $data['status'] = $status; $data['width'] = $sizeinfo[0]; $data['height'] = $sizeinfo[1]; $data['size'] = $size; $model->attributes = $data; if ($model->validate()) { if ($model->save()) { $image = Yii::app()->image->load($origin . $fileName); $_quality = zmf::config('imgQuality'); $quality = isset($quality) ? $quality : 100; foreach ($dirs as $dk => $_dir) { if ($dk < 600) { $image->smart_resize($dk, $dk * 0.75)->quality($quality); } else { $image->resize($dk, $dk)->quality($quality); } $image->save($_dir . $fileName); $_todir = zmf::ftpPath($ctime, $uptype, $dk); $uploadedFiles[] = array('id' => $model->id, 'from' => $_dir . $fileName, 'to' => $_todir . $fileName); } $imgsize = $reImgsize > 0 ? $reImgsize : 170; $returnimg = zmf::uploadDirs($ctime, 'site', $uptype, $imgsize) . $fileName; $outPutData = array('status' => 1, 'attachid' => $model->id, 'imgsrc' => $returnimg); $json = CJSON::encode($outPutData); echo $json; } else { $this->jsonOutPut(0, '写入数据库错误'); } } else { $this->jsonOutPut(0, '数据验证错误'); } } }
public function actionUpload() { $uptype = zmf::filterInput($_GET['type'], 't', 1); $logid = zmf::filterInput($_GET['id']); //所属对象 $reImgsize = zmf::filterInput($_GET['imgsize']); //返回图片的尺寸 $fileholder = zmf::filterInput($_GET['fileholder'], 't', 1); //上传控件的ID //将ads替换为flash if (!isset($uptype) or !in_array($uptype, array('columns', 'coverimg', 'flash', 'link', 'album', 'posts', 'poi', 'poipost', 'answer', 'question', 'siteinfo', 'goods', 'group', 'avatar'))) { $this->jsonOutPut(0, '请设置上传所属类型' . $uptype); } if (Yii::app()->request->getParam('PHPSESSID')) { Yii::app()->session->close(); $res = Yii::app()->session->setSessionID(Yii::app()->request->getParam('PHPSESSID')); Yii::app()->session->open(); } if (Yii::app()->user->isGuest) { $this->jsonOutPut(0, Yii::t('default', 'loginfirst')); } $checkInfo = UserPower::check('addImage', true); if (!$checkInfo['status']) { $this->jsonOutPut(0, $checkInfo['msg']); } if ($uptype == 'poi') { if (!$logid || !is_numeric($logid)) { $this->jsonOutPut(0, '无效上传,请重试'); } else { $poiInfo = Position::model()->findByPk($logid); if (!$poiInfo || $poiInfo['status'] != Posts::STATUS_PASSED) { $this->jsonOutPut(0, '无效上传,请重试'); } } } if (!$fileholder) { $fileholder = 'filedata'; } if (!isset($_FILES[$fileholder]) || !is_uploaded_file($_FILES[$fileholder]["tmp_name"]) || $_FILES[$fileholder]["error"] != 0) { $this->jsonOutPut(0, '无效上传,请重试'); } $model = new Attachments(); $img = CUploadedFile::getInstanceByName($fileholder); $ext = $img->getExtensionName(); $size = $img->getSize(); if ($size > zmf::config('imgMaxSize')) { $this->jsonOutPut(0, '上传文件最大尺寸为:' . tools::formatBytes(zmf::config('imgMaxSize'))); } $upExt = zmf::config("imgAllowTypes"); if (!preg_match('/^(' . str_replace('*.', '|', str_replace(';', '', $upExt)) . ')$/i', $ext)) { $this->jsonOutPut(0, '上传文件扩展名必需为:' . $upExt); } $sizeinfo = getimagesize($_FILES[$fileholder]["tmp_name"]); if ($sizeinfo['0'] < zmf::config('imgMinWidth') or $sizeinfo[1] < zmf::config('imgMinHeight')) { $this->jsonOutPut(0, "要求上传的图片尺寸,宽不能不小于" . zmf::config('imgMinWidth') . "px,高不能小于" . zmf::config('imgMinHeight') . "px."); } $ctime = zmf::now(); $uid = zmf::uid(); $dirs = zmf::uploadDirs($ctime, 'app', $uptype, null, true); $fileName = uniqid() . '.' . $ext; $origin = $dirs['origin']; unset($dirs['origin']); $uploadedFiles = array(); $uploadedFiles[] = array('from' => $origin . $fileName, 'to' => zmf::ftpPath($ctime, $uptype, 'origin') . $fileName); if (move_uploaded_file($_FILES[$fileholder]["tmp_name"], $origin . $fileName)) { $data = array(); if ($uptype == 'posts') { $status = Posts::STATUS_DELED; } else { $status = Posts::STATUS_PASSED; } $data['uid'] = $uid; $data['logid'] = $logid; $data['filePath'] = $fileName; $data['fileDesc'] = $fileName; $data['classify'] = $uptype; $data['covered'] = '0'; $data['cTime'] = time(); $data['status'] = $status; $data['width'] = $sizeinfo[0]; $data['height'] = $sizeinfo[1]; $data['size'] = $size; if ($uptype == 'poi') { $data['areaid'] = $poiInfo['areaid']; } $model->attributes = $data; if ($model->validate()) { if ($model->save()) { if ($uptype == 'poi') { Posts::updateCount($logid, 'Position', 1, 'attach'); } elseif ($uptype == 'avatar') { Users::model()->updateByPk($uid, array('avatar' => $model->id)); } // $image = Yii::app()->image->load($origin . $fileName); // $_quality = zmf::config('imgQuality'); // $quality = isset($quality) ? $quality : 100; Yii::import('application.vendors.thinkphp.*'); require_once 'ImageGd.php'; if (in_array($uptype, array('group', 'avatar'))) { $rate = 1; } else { $rate = 0.75; } foreach ($dirs as $dk => $_dir) { $image = new ImageGd($origin . $fileName); if ($dk < 600) { $image->thumb($dk, $dk * $rate, 'center'); } else { $image->thumb($dk, 10000); //$image->water(Yii::app()->basePath . '/../common/images/water.png',2); } $image->save($_dir . $fileName); $_todir = zmf::ftpPath($ctime, $uptype, $dk); $uploadedFiles[] = array('id' => $model->id, 'from' => $_dir . $fileName, 'to' => $_todir . $fileName); } $imgsize = $reImgsize > 0 ? $reImgsize : 170; $returnimg = zmf::uploadDirs($ctime, 'site', $uptype, $imgsize) . $fileName; $outPutData = array('status' => 1, 'attachid' => $model->id, 'imgsrc' => $returnimg); $json = CJSON::encode($outPutData); if (zmf::config('ftpon')) { $asyncdata = "method=ftpupload&json=" . zmf::jiaMi(CJSON::encode($uploadedFiles)); AsyncController::Async($asyncdata); } if ($uptype == 'avatar') { zmf::delFCache("userInfo-{$uid}"); } echo $json; } else { $this->jsonOutPut(0, '写入数据库错误'); } } else { $this->jsonOutPut(0, '数据验证错误'); } } else { $this->jsonOutPut(0, '移动到指定文件夹出错'); } }