Пример #1
0
 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;
 }