Beispiel #1
0
 /**
  * Resize image to this size
  * 
  * @param string $thumb @see $this->previews
  */
 public function resize($thumb)
 {
     Yii::import('ext.iwi.Iwi');
     $folder = Yii::getPathOfAlias('webroot') . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'photos';
     $file = $this->id . '.' . $this->extension;
     //$folder = DIRECTORY_SEPARATOR . trim($folder, DIRECTORY_SEPARATOR . ' ');
     if (!isset($this->previews[$thumb])) {
         return 0;
     }
     $size = $this->previews[$thumb];
     if (!is_file($folder . DIRECTORY_SEPARATOR . $file)) {
         return 0;
     }
     $out_file = $folder . DIRECTORY_SEPARATOR . $thumb . DIRECTORY_SEPARATOR . $file;
     //output file
     if (is_file($out_file)) {
         return $out_file;
     }
     $image = new Iwi($folder . DIRECTORY_SEPARATOR . $file);
     if ($size[$this->WIDTH] == 0) {
         $image->resize(0, $size[$this->HEIGHT], Image::HEIGHT);
     } elseif ($size[$this->HEIGHT] == 0) {
         $image->resize($size[$this->WIDTH], 0, Image::WIDTH);
     } else {
         $image->adaptive($size[$this->WIDTH], $size[$this->HEIGHT])->crop($size[$this->WIDTH], $size[$this->HEIGHT], 'top', 'center')->sharpen(10);
     }
     if (!file_exists($folder . DIRECTORY_SEPARATOR . $thumb)) {
         mkdir($folder . DIRECTORY_SEPARATOR . $thumb, 777);
     }
     $image->save($out_file);
     return $out_file;
 }
 public function actionEditBackground()
 {
     Yii::import('ext.iwi.Iwi');
     $id = Yii::app()->user->id;
     if (!$id > 0) {
         throw new CException('Not Found', 404);
     }
     Yii::app()->log->routes[3]->enabled = false;
     if (isset($_GET['ajax'])) {
         $upload = Files::model()->uploadFile('avatar');
         if ($upload !== false) {
             echo CJSON::encode(array('message' => $upload['filename'] . '.' . $upload['extension'], 'status' => 'OK'));
         } else {
             echo CJSON::encode(array('message' => 'Ошибка загрузки файла', 'status' => 'ERROR'));
         }
         exit;
     }
     if (Yii::app()->request->getPost('height') > 0) {
         $post = array('height' => (int) Yii::app()->request->getPost('height'), 'width' => (int) Yii::app()->request->getPost('width'), 'left' => (int) Yii::app()->request->getPost('left'), 'top' => (int) Yii::app()->request->getPost('top'), 'image' => trim(Yii::app()->request->getPost('image', ''), '/'));
         $filename = pathinfo($post['image'], PATHINFO_FILENAME);
         $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
         $file = Files::model()->findByPk($filename);
         $userpath = $path . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'photos' . DIRECTORY_SEPARATOR;
         $image = new Iwi($userpath . $file->id . '.' . $file->extension);
         $image->resize($post['width'], $post['height']);
         if ($image->save($userpath . 'cover' . DIRECTORY_SEPARATOR . $file->id . '.' . $file->extension)) {
             $user = UserProfile::model()->find("user_id=:uid", array('uid' => $id));
             $oldavatar = false;
             if ($user->background != $filename) {
                 $oldavatar = $user->background;
             }
             $user->background = $filename;
             $user->bgposition = $post['left'] . ' ' . $post['top'];
             if ($user->save()) {
                 //                    if($oldavatar!=false && $oldavatar != 'userTestBG.png' && is_file($userpath . $oldavatar))
                 //                        FileHelper::cleanUpload('UserProfile', $oldavatar);
                 echo CJSON::encode(array('message' => $filename, 'status' => 'OK'));
             } else {
                 echo CJSON::encode(array('message' => $user->getErrors(), 'status' => 'ERROR'));
             }
             exit;
         } else {
             //System error must be handled by administration.
             Yii::log("Ошибка при создании файла аватара '{$post['image']}'. Пользователь #{$id}", 'email');
             echo CJSON::encode(array('status' => 'ERROR', 'message' => 'Ну удалось сохранить аватар. Попробуйте позже.'));
         }
     }
     //else
     //    echo CJSON::encode(array('status'=>'ERROR','message'=>'Не хватает параметров.'));
     //if(isset($_GET['ajax']))
     exit;
     //$this->render('edit_avatar');
 }
 public function actionEditBackground()
 {
     Yii::import('ext.iwi.Iwi');
     $id = Yii::app()->user->id;
     if (!$id > 0) {
         throw new CException('Not Found', 404);
     }
     $filev = new FileValidator();
     $filev->types = array('jpg', 'jpeg', 'png', 'gif');
     $filev->sizes = array('min' => 962, 250);
     $upload = Images::model()->uploadFile('avatar', true, $filev);
     if ($upload !== false && !isset($upload['error'])) {
         //$size = getimagesize(Yii::getPathOfAlias('webroot').'/upload/photos/'.$upload['filename'].'.'.$upload['extension']);
         //if($size[0]<962 || $size[1]<250){
         //    echo CJSON::encode(array('message'=>'Размер изображения должен быть не меньше 962 в ширине на 250 по высоте пикселей.','status'=>'ERROR'));
         //    exit();
         //}
         echo CJSON::encode(array('message' => $upload['filename'] . '.' . $upload['extension'], 'status' => 'OK'));
     } else {
         if (isset($upload['error'])) {
             echo CJSON::encode(array('message' => $upload['error'], 'status' => 'ERROR'));
         }
     }
     if (Yii::app()->request->getPost('height') > 0) {
         $post = array('height' => (int) Yii::app()->request->getPost('height'), 'width' => (int) Yii::app()->request->getPost('width'), 'left' => (int) Yii::app()->request->getPost('left'), 'top' => (int) Yii::app()->request->getPost('top'), 'image' => trim(Yii::app()->request->getPost('image', ''), '/'));
         $filename = basename($post['image']);
         $path = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
         $userpath = $path . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'UserProfile' . DIRECTORY_SEPARATOR;
         $image = new Iwi($path . DIRECTORY_SEPARATOR . $post['image']);
         $image->resize($post['width'], $post['height'])->crop(962, 250, $post['top'], $post['left']);
         if ($image->save($userpath . $filename)) {
             $user = UserProfile::model()->find("user_id=:uid", array('uid' => $id));
             $oldavatar = false;
             if ($user->background != $filename) {
                 $oldavatar = $user->background;
             }
             $user->background = $filename;
             if ($user->save()) {
                 if ($oldavatar != false && $oldavatar != 'userTestBG.png' && is_file($userpath . $oldavatar)) {
                     FileHelper::cleanUpload('UserProfile', $oldavatar);
                 }
                 echo CJSON::encode(array('message' => $filename, 'status' => 'OK'));
             } else {
                 echo CJSON::encode(array('message' => $user->getErrors(), 'status' => 'ERROR'));
             }
             exit;
         } else {
             //System error must be handled by administration.
             Yii::log("Ошибка при создании файла аватара '{$post['image']}'. Пользователь #{$id}", 'email');
             echo CJSON::encode(array('status' => 'ERROR', 'message' => 'Ну удалось сохранить аватар. Попробуйте позже.'));
         }
     }
     //else
     //    echo CJSON::encode(array('status'=>'ERROR','message'=>'Не хватает параметров.'));
     //if(isset($_GET['ajax']))
     exit;
     //$this->render('edit_avatar');
 }
 public function actionResize()
 {
     $image = Yii::app()->request->getPost('image', false);
     $type = Yii::app()->request->getPost('type', false);
     if ($image !== false && in_array($type, array('avatar'))) {
         $file = pathinfo($image, PATHINFO_FILENAME);
         $file = Files::model()->findByPk($file);
         if ($file != NULL) {
             $tmp = TemporaryFiles::model()->find('md5file=:md5f', array(':md5f' => $md5file));
             if ($tmp == null) {
                 $to = realpath(Yii::app()->basePath . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR . 'TemporaryFiles') . DIRECTORY_SEPARATOR . $filename;
                 if ($to && @copy($image, $to)) {
                     $image = $to;
                     $size = getimagesize($image);
                     if ($size[0] > 656 || $size[1] > 520) {
                         Yii::import('ext.iwi.Iwi');
                         $img = new Iwi($image);
                         $img->resize(656, 520, 2);
                         $img->save();
                     }
                     $tmp = new TemporaryFiles();
                     $tmp->md5file = $md5file;
                     $tmp->filename = $filename;
                     $tmp->upload_date = time();
                     $tmp->save();
                 } else {
                     if (!is_file($to)) {
                         echo CJSON::encode(array('message' => 'Ошибка копирования файла!', 'status' => 'ERROR'));
                         exit;
                     } else {
                         echo CJSON::encode(array('message' => basename($to), 'status' => 'OK'));
                         exit;
                     }
                 }
             }
             echo CJSON::encode(array('message' => $tmp->filename, 'status' => 'OK'));
         }
         echo CJSON::encode(array('message' => 'Файл не найден', 'status' => 'ERROR'));
     }
     exit;
 }
 public function resize($folder, $file, $sizes)
 {
     $previews = $this->previews;
     if (is_string($sizes)) {
         $previews = isset($this->previews[$sizes]) ? $this->previews[$sizes] : $this->previews;
     } else {
         if (is_array($sizes)) {
             $previews = array_intersect_key($this->previews, array_fill_keys($sizes, '1'));
         }
     }
     if ($folder === null) {
         $folder = Yii::getPathOfAlias('webroot') . '/upload/photos';
     }
     if ($file === null) {
         $file = $this->id . '.' . $this->extension;
     }
     $folder = '/' . trim($folder, '/ ');
     var_dump($previews);
     exit;
     foreach ($this->previews as $sub => $size) {
         if (is_file($folder . '/' . $sub . '/' . $file)) {
             continue;
         }
         $image = new Iwi($folder . '/' . $file);
         if ($size[$this->WIDTH] == 0) {
             $image->resize(0, $size[$this->HEIGHT], Image::HEIGHT);
         } elseif ($size[$this->HEIGHT] == 0) {
             $image->resize($size[$this->WIDTH], 0, Image::WIDTH);
         } else {
             $image->adaptive($size[$this->WIDTH], $size[$this->HEIGHT])->crop($size[$this->WIDTH], $size[$this->HEIGHT], 'top', 'center')->sharpen(10);
         }
         if (!file_exists($folder . '/' . $sub)) {
             mkdir($folder . '/' . $sub, 777);
         }
         $image->save($folder . '/' . $sub . '/' . $file);
     }
 }