Example #1
0
 /** @param CUploadedFile $file **/
 public function upload($file, $resize_w = null, $resize_h = null)
 {
     $this->slot = date("Y") - 2000;
     $this->subslot = (int) date("m");
     $this->checkDir();
     $this->generateSeed();
     $R = new ImgResizer();
     list($this->w, $this->h) = $R->resize($file->tempName, $this->path, $resize_w, $resize_h);
     // 50x50 thumbnail
     list($th_w, $th_h) = $R->resize($file->tempName, $this->getPath("5050"), 50, 50);
     return true;
 }
Example #2
0
 protected function afterValidate()
 {
     // Аватар
     if ($this->rm_img) {
         $this->imgUnlink();
     }
     if ($this->new_img) {
         // стереть старую фотку, если есть
         $this->imgUnlink();
         $this->imgCheckDir();
         $this->img[0] = rand(1, 32000);
         $R = new ImgResizer();
         list($this->img[1], $this->img[2]) = $R->resize($this->new_img->tempName, $this->imgPath, 200, 500);
     }
     parent::afterValidate();
 }
Example #3
0
 public function actionUpic()
 {
     /** @var User $user */
     $user = User::model()->findByPk(Yii::app()->user->id);
     if ($_GET["do"] == "cancel" && is_array($_SESSION["upicEditor"])) {
         $tmp_path = $_SERVER["DOCUMENT_ROOT"] . "/i/tmp/upiccut/" . $_SESSION["upicEditor"]["img"]["name"];
         if (is_file($tmp_path)) {
             unlink($tmp_path);
         }
         unset($_SESSION["upicEditor"]);
         $this->redirect($user->url);
     }
     if (isset($_POST["x"]) && isset($_POST["y"]) && isset($_POST["w"]) && isset($_POST["h"])) {
         $tmp_path = $_SERVER["DOCUMENT_ROOT"] . "/i/tmp/upiccut/" . $_SESSION["upicEditor"]["img"]["name"];
         $user->upicUnlink();
         $user->upicCheckDir();
         $user->upic[0] = rand(1, 32000);
         list($user->upic[1], $user->upic[2], $otyp) = getimagesize($tmp_path);
         // resize
         $src = null;
         if ($otyp == 1) {
             $src = @ImageCreateFromGIF($tmp_path);
         } else {
             if ($otyp == 2) {
                 $src = @ImageCreateFromJPEG($tmp_path);
             } else {
                 if ($otyp == 3) {
                     $src = @ImageCreateFromPNG($tmp_path);
                 }
             }
         }
         $dst = ImageCreateTrueColor(50, 50);
         if (!$src || !$dst) {
             Yii::app()->user->setFlash("error", "При обработке изображения произошла ошибка. Попробуйте другую картинку.");
             $this->redirect($user->getUrl("upic"));
         }
         $srcx = (int) $_POST["x"];
         $srcy = (int) $_POST["y"];
         $srcw = (int) $_POST["w"];
         $srch = (int) $_POST["h"];
         ImageCopyResampled($dst, $src, 0, 0, $srcx, $srcy, 50, 50, $srcw, $srch);
         ImageJPEG($dst, $user->upicPath);
         // store big image
         rename($tmp_path, $user->upicPathBig);
         $user->save(false);
         unset($_SESSION["upicEditor"]);
         $this->redirect($user->url);
     }
     if ($_FILES["img"]["size"] == 0 && !isset($_SESSION["upicEditor"])) {
         $this->redirect($user->url);
     }
     if ($_FILES["img"]["size"] > 0) {
         list($w, $h) = getimagesize($_FILES["img"]["tmp_name"]);
         if ($w <= 0 || $h <= 0) {
             Yii::app()->user->setFlash("error", "Не удалось загрузить файл. Возможно, он сохранён в неправильном формате. Попробуйте открыть его любым графическим редактором и пересохранить в JPG.");
             $this->redirect($user->url);
         }
         $tmp_name = time() . "-" . $user->id . "-" . rand(1000, 9999);
         $tmp_path = $_SERVER["DOCUMENT_ROOT"] . "/i/tmp/upiccut/" . $tmp_name;
         if ($w >= 600 || $h > 800) {
             $R = new ImgResizer();
             list($w, $h) = $R->resize($_FILES["img"]["tmp_name"], $tmp_path, 600, 800);
         } else {
             move_uploaded_file($_FILES["img"]["tmp_name"], $tmp_path);
         }
         $_SESSION["upicEditor"]["img"] = ["name" => $tmp_name, "w" => $w, "h" => $h];
     }
     $p = ["user" => $user];
     $this->side_view = ["upic_side" => $p];
     $this->render("upic", $p);
 }