Exemplo n.º 1
0
 public function actionUpPortrait()
 {
     $isSuc = false;
     $userId = $_REQUEST['id'];
     $picDesc = $_REQUEST['desc'];
     //本地
     //$root = YiiBase::getPathOfAlias('webroot').Yii::app()->getBaseUrl();
     //服务器代码
     $root = YiiBase::getPathOfAlias('webroot');
     $folder = $root . '/images/images/users/' . $userId . '/';
     $desFilePath;
     $tmpFilePath;
     $relPath = Yii::app()->getBaseUrl() . '/images/images/users/' . $userId . '/';
     $this->mkDirIfNotExist($folder);
     if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/pjpeg") {
         if ($_FILES["file"]["error"] > 0) {
             $isSuc = false;
         } else {
             $tmpFilePath = $_FILES["file"]["tmp_name"];
             /*
             				$array = explode("/", $tmpFilePath); 
             				var_dump($array[ count($array) -1]);
             			    $tmpPath  = $tmpPath.$array[ count($array) -1];
             */
             $name = $this->getUploadImageFileName($_FILES["file"]["name"]);
             $desFilePath = $folder . $name;
             $desThumbPath = $folder . 'thumb/' . $name;
             $relPath = $relPath . $name;
             if (file_exists($desFilePath)) {
                 unlink($desFilePath);
                 //echo $_FILES["file"]["name"] . " already exists. ";
             } else {
                 move_uploaded_file($tmpFilePath, $desFilePath);
                 //生成缩略图
                 $im = null;
                 $imtp = null;
                 if ($_FILES["file"]["type"] == "image/gif") {
                     $im = imagecreatefromgif($desFilePath);
                     $imtp = 'gif';
                 } else {
                     if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg") {
                         $im = imagecreatefromjpeg($desFilePath);
                         $imtp = 'jpg';
                     } else {
                         if ($_FILES["file"]["type"] == "image/png") {
                             $im = imagecreatefrompng($desFilePath);
                             $imtp = 'png';
                         }
                     }
                 }
                 $this->mkDirIfNotExist($folder . 'thumb/');
                 CThumb::resizeImage($im, 100, 100, $desThumbPath, $imtp);
                 //------------生成缩略图
                 $isSuc = true;
             }
         }
     } else {
         $isSuc = false;
     }
     if ($isSuc) {
         $userModel = new UserModel();
         $userModel->_id = $userId;
         $userModel->image = $relPath;
         $userModel->updateByPk($userId, array('image' => $relPath, 'updatetime' => time()));
         $host = 'http://' . Yii::app()->request->getServerName();
         //本地调试
         $userModel->image = $host . $userModel->image;
         //服务器
         //$userModel->image = $host.Yii::app()->getBaseUrl().$userModel->image;
         echo json_encode(array('result' => 1, 'res' => array('id' => $userModel->_id, 'image' => $userModel->image)));
     } else {
         echo json_encode(array('result' => 0));
     }
 }
Exemplo n.º 2
0
 public function actionUpload()
 {
     if (!isset(Yii::app()->user->id)) {
         $this->redirect(array('style/index'));
     }
     $isSuc = false;
     $userId = isset($_REQUEST['id']) ? trim(strip_tags($_REQUEST['id'])) : null;
     //本地
     //$root = YiiBase::getPathOfAlias('webroot').Yii::app()->getBaseUrl();
     //服务器代码
     $root = YiiBase::getPathOfAlias('webroot');
     $folder = $root . '/images/images/styles/' . $userId . '/';
     $desFilePath;
     $tmpFilePath;
     $relPath = Yii::app()->getBaseUrl() . '/images/images/styles/' . $userId . '/';
     $this->mkDirIfNotExist($folder);
     if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/png" || $_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/pjpeg") {
         if ($_FILES["file"]["error"] > 0) {
             $isSuc = false;
         } else {
             $tmpFilePath = $_FILES["file"]["tmp_name"];
             /*
             				$array = explode("/", $tmpFilePath); 
             				var_dump($array[ count($array) -1]);
             			    $tmpPath  = $tmpPath.$array[ count($array) -1];
             */
             $name = $this->getUploadImageFileName($_FILES["file"]["name"]);
             $desFilePath = $folder . $name;
             $desThumbPath = $folder . 'thumb/' . $name;
             $relPath = $relPath . $name;
             if (file_exists($desFilePath)) {
                 unlink($desFilePath);
                 //echo $_FILES["file"]["name"] . " already exists. ";
             } else {
                 move_uploaded_file($tmpFilePath, $desFilePath);
                 //生成缩略图
                 $im = null;
                 $imtp = null;
                 if ($_FILES["file"]["type"] == "image/gif") {
                     $im = imagecreatefromgif($desFilePath);
                     $imtp = 'gif';
                 } else {
                     if ($_FILES["file"]["type"] == "image/jpg" || $_FILES["file"]["type"] == "image/jpeg") {
                         $im = imagecreatefromjpeg($desFilePath);
                         $imtp = 'jpg';
                     } else {
                         if ($_FILES["file"]["type"] == "image/png") {
                             $im = imagecreatefrompng($desFilePath);
                             $imtp = 'png';
                         }
                     }
                 }
                 $this->mkDirIfNotExist($folder . 'thumb/');
                 CThumb::resizeImage($im, 100, 100, $desThumbPath, $imtp);
                 //------------生成缩略图
                 $isSuc = true;
             }
         }
     } else {
         $isSuc = false;
     }
     if ($isSuc) {
         $styleModel = new StyleModel();
         $styleModel->image = $relPath;
         $styleModel->user_id = $userId;
         $styleModel->create_time = time();
         $styleModel->like_num = 0;
         $styleModel->recommand_val = 0;
         if (!$styleModel->save()) {
             echo "style saved fail!";
             return false;
         }
         $host = 'http://' . Yii::app()->request->getServerName();
         //本地调试
         $styleModel->image = $host . $styleModel->image;
         //服务器
         //$styleModel->image = $host.Yii::app()->getBaseUrl().$styleModel->image;
         $this->redirect(array('Upload2/index'));
     } else {
         echo "upload failed";
     }
 }