public function actionSite($power = false) { $model = new SiteForm(); $model->getSetting(); if (isset($_POST['SiteForm'])) { $model->attributes = $_POST['SiteForm']; $uploadFile = CUploadedFile::getInstance($model, 'logo'); if ($uploadFile !== null) { $uploadFileName = "logo_" . time() . '.' . $uploadFile->getExtensionName(); if (file_exists($model->logo)) { unlink(Yii::app()->basePath . "/../" . $model->logo); } if (!is_dir(Yii::app()->basePath . "/../uploads/setting/site")) { DxdUtil::createFolders(Yii::app()->basePath . "/../uploads/setting/site"); } $model->logo = 'uploads/setting/site/' . $uploadFileName; $uploadFile->saveAs(Yii::app()->basePath . "/../" . $model->logo); } // var_dump($_POST['SiteForm']); if ($model->saveSetting()) { Yii::app()->user->setFlash('success', '保存成功!'); } else { Yii::app()->user->setFlash('error', '保存失败!'); } } if (!$power) { $this->render('site', array('model' => $model)); } else { $this->render('power', array('model' => $model)); } }
/** * 设置用户头像设置 * @param integer $id */ public function actionCropFace($id) { $model = $this->loadModel($id); if ($model->face == $model->defaultFace) { $this->redirect(array('uploadFace', 'id' => $id)); Yii::app()->end(); } if (isset($_POST['imageId_x'])) { Yii::import('ext.jcrop.EJCropper'); $jcropper = new EJCropper(); $jcropper->thumbPath = dirname($model->face) . "/thumbs"; if (!file_exists($jcropper->thumbPath)) { DxdUtil::createFolders($jcropper->thumbPath); } // get the image cropping coordinates (or implement your own method) $coords = $jcropper->getCoordsFromPost('imageId'); // returns the path of the cropped image, source must be an absolute path. $thumbnail = $jcropper->crop($model->face, $coords); if ($thumbnail) { unlink($model->face); $model->face = $thumbnail; $model->save(); } $this->redirect(array('uploadFace', 'id' => $id)); } $this->render('crop_face', array('model' => $model)); }
/** * 关注或取消关注 * @param unknown_type $id */ public function run($id) { $model = $this->controller->loadModel($id); if (isset($_POST['imageId_x'])) { Yii::import('ext.jcrop.EJCropper'); $jcropper = new EJCropper(); $jcropper->thumbPath = dirname($model->{$this->attribute}) . "/thumbs"; if (!file_exists($jcropper->thumbPath)) { DxdUtil::createFolders($jcropper->thumbPath); } // get the image cropping coordinates (or implement your own method) $coords = $jcropper->getCoordsFromPost('imageId'); // returns the path of the cropped image, source must be an absolute path. $thumbnail = $jcropper->crop($model->face, $coords); if ($thumbnail) { unlink($model->face); $model->face = $thumbnail; $model->save(); } $this->controller->redirect($this->returnUrl); } $this->controller->render($this->cropViewFile, array('model' => $model)); }
/** * 裁剪封面图页面和处理方法 * @param integer $id 文章ID */ public function actionCropFace($id) { $model = $this->loadModel($id); if (isset($_POST['imageId_x'])) { Yii::import('ext.jcrop.EJCropper'); $jcropper = new EJCropper(); $jcropper->thumbPath = $this->facePath; if (!file_exists($jcropper->thumbPath)) { DxdUtil::createFolders($jcropper->thumbPath); } $coords = $jcropper->getCoordsFromPost('imageId'); $thumbnail = $jcropper->crop($model->face, $coords); if ($thumbnail) { $oldFace = $model->face; $model->face = $thumbnail; if ($model->save()) { if ($model->face != $oldFace) { @unlink($oldFace); } Yii::app()->user->setFlash('success', '更新成功'); $this->redirect(array('index')); } else { Yii::app()->user->setFlash('error', '更新失败'); } } } $this->render('crop_face', array('model' => $model)); }
/** * 按比例产生缩略图 * @param unknown_type $fileName 相对于Yii::app()->uploads * @param unknown_type $width * @param unknown_type $height */ public static function xImage($fileName, $width, $height) { //文件是否存在 $sourceFile = Yii::app()->basePath . "/../" . $fileName; if (!file_exists($sourceFile)) { return false; } $ext = DxdUtil::fileExt($fileName); if (!in_array(strtolower($ext), array('jpg', 'png', 'jpeg'))) { return false; } //新文件名,形如 filename_120_100.jpg $newFileName = substr($fileName, 0, strrpos($fileName, ".")) . "_" . $width . "_" . $height . "." . $ext; $targetFile = Yii::app()->basePath . "/../caches/" . $newFileName; //文件是否已经被缩略过 if (file_exists($targetFile)) { return "caches/" . $newFileName; } //开始压缩 Yii::import('application.extensions.EWideImage.EWideImage'); $dir = dirname($targetFile); if (!file_exists($dir)) { DxdUtil::createFolders($dir); } try { EWideImage::load(Yii::app()->basePath . "/../" . $fileName)->resize($width, $height)->saveToFile($targetFile); } catch (Exception $e) { return false; } return "caches/" . $newFileName; }
public function actionCropFace() { $user = UserInfo::model()->findByAttributes(array('id' => Yii::app()->user->id)); if (!$user->face || $user->face == $user->defaultFace) { Yii::app()->user->setFlash('success', Yii::t('app', "欢迎{name}加入!", array('{name}' => $user->name))); $this->redirect(Yii::app()->baseUrl . "/"); Yii::app()->end(); } if (isset($_POST['imageId_x'])) { Yii::import('ext.jcrop.EJCropper'); $jcropper = new EJCropper(); $jcropper->thumbPath = dirname($user->face) . "/thumbs"; if (!file_exists($jcropper->thumbPath)) { DxdUtil::createFolders($jcropper->thumbPath); } // get the image cropping coordinates (or implement your own method) $coords = $jcropper->getCoordsFromPost('imageId'); // returns the path of the cropped image, source must be an absolute path. $thumbnail = $jcropper->crop($user->face, $coords); if ($thumbnail) { unlink($user->face); $user->face = $thumbnail; $user->save(); } Yii::app()->user->setFlash('success', Yii::t('app', "欢迎{name}加入!", array('{name}' => $user->name))); $this->redirect(Yii::app()->baseUrl . "/"); // echo $jcropper->thumbPath; // echo $thumbnail; } $this->render('register_crop_face', array('user' => $user)); }
public function actionUpload($dir = "image") { function alert($msg) { header('Content-type: text/html; charset=UTF-8'); // $json = new Services_JSON(); echo json_encode(array('error' => 1, 'message' => $msg)); exit; } /** * KindEditor PHP * * 本PHP程序是演示程序,建议不要直接在实际项目中使用。 * 如果您确定直接使用本程序,使用之前请仔细确认相关安全设置。 * */ Yii::import('ext.kindeditor'); // require_once('extensions/kindeditor/JSON.php'); //$base_path = $_SERVER['DOCUMENT_ROOT'] . '/'; //$host_url = $_SERVER['HTTP_HOST'] . '/'; //$base_url = substr($host_url,strpos($host_url,'/')); //文件保存目录路径 $save_path = Yii::app()->basePath . '/../uploads/attached/'; if (!file_exists($save_path)) { DxdUtil::createFolders($save_path); } //文件保存目录URL $save_url = Yii::app()->baseUrl . '/uploads/attached/'; //if(!file_exists($save_url)) DxdUtil::createFolders($save_url); //定义允许上传的文件扩展名 $ext_arr = array('image' => array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'flash' => array('swf', 'flv'), 'media' => array('swf', 'flv', 'mp3', 'mp4', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'), 'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2', 'mp4', 'mp3')); //最大文件大小 $max_size = 10000000; //PHP上传失败 if (!empty($_FILES['imgFile']['error'])) { switch ($_FILES['imgFile']['error']) { case '1': $error = '超过php.ini允许的大小。'; break; case '2': $error = '超过表单允许的大小。'; break; case '3': $error = '图片只有部分被上传。'; break; case '4': $error = '请选择图片。'; break; case '6': $error = '找不到临时目录。'; break; case '7': $error = '写文件到硬盘出错。'; break; case '8': $error = 'File upload stopped by extension。'; break; case '999': default: $error = '未知错误。'; } alert($error); } //有上传文件时 if (empty($_FILES) === false) { //原文件名 $file_name = $_FILES['imgFile']['name']; //服务器上临时文件名 $tmp_name = $_FILES['imgFile']['tmp_name']; //文件大小 $file_size = $_FILES['imgFile']['size']; //检查文件名 if (!$file_name) { alert("请选择文件。"); } //检查目录 if (@is_dir($save_path) === false) { alert("上传目录不存在。"); } //检查目录写权限 if (@is_writable($save_path) === false) { alert("上传目录没有写权限。"); } //检查是否已上传 if (@is_uploaded_file($tmp_name) === false) { alert("上传失败。"); } //检查文件大小 if ($file_size > $max_size) { alert("上传文件大小超过限制。"); } //检查目录名 $dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']); if (empty($ext_arr[$dir_name])) { alert("目录名不正确。"); } //获得文件扩展名 $temp_arr = explode(".", $file_name); $file_ext = array_pop($temp_arr); $file_ext = trim($file_ext); $file_ext = strtolower($file_ext); //检查扩展名 if (in_array($file_ext, $ext_arr[$dir_name]) === false) { alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。"); } //创建文件夹 if ($dir_name !== '') { $save_path .= $dir_name . "/"; $save_url .= $dir_name . "/"; if (!file_exists($save_path)) { mkdir($save_path); } } $ymd = date("Ymd"); $save_path .= $ymd . "/"; $save_url .= $ymd . "/"; if (!file_exists($save_path)) { mkdir($save_path); } //新文件名 $new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext; //移动文件 $file_path = $save_path . $new_file_name; if (move_uploaded_file($tmp_name, $file_path) === false) { alert("上传文件失败。"); } @chmod($file_path, 0644); $file_url = $save_url . $new_file_name; header('Content-type: text/html; charset=UTF-8'); // $json = new Services_JSON(); echo json_encode(array('error' => 0, 'url' => $file_url)); exit; } }