Ejemplo n.º 1
0
 public function actionAdd()
 {
     $request = \Yii::$app->request;
     $response = \Yii::$app->response;
     if ($request->get('subaction') == 'upload') {
         \app\helpers\Upload::upload(['cbSave' => function ($file, $key, $path, &$result) {
             $ex = strtolower($file->getExtension());
             if (!in_array($ex, ['jpg', 'jpeg', 'gif', 'png'])) {
                 $result[$key]['error'] = 'File extension error! Expected: jpg, jpeg, gif, png';
                 return;
             }
             $path_mid = Model::getTmpPath('tmp', $ex);
             $path_mid_abs = \Yii::getAlias("@webroot/{$path_mid}");
             Model::resizeImage('mid', $path, $path_mid_abs);
             $result[$key]['mid_src'] = \Yii::getAlias("@web/{$path_mid}");
             $sizes = Model::getPathSizes($path_mid_abs);
             $result[$key]['mid_width'] = $sizes['width'];
             $result[$key]['mid_height'] = $sizes['height'];
         }]);
     }
     $idModel = $request->get('id');
     $formModel = new Form();
     $formModel->initModel($idModel);
     $this->tryAjaxValidateModel($formModel);
     if ($request->isPost && $formModel->load($request->post()) && $formModel->validate() && $formModel->save()) {
         return $this->redirect([static::URL_TO_INDEX]);
     }
     return $this->render('add', ['formModel' => $formModel]);
 }
Ejemplo n.º 2
0
 public static function file(UploadedFile $fileInstance, $dir = '', $namePostfix = true)
 {
     $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance, $namePostfix);
     if (!$fileInstance->saveAs($fileName)) {
         throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.');
     }
     return Upload::getLink($fileName);
 }
Ejemplo n.º 3
0
 public static function upload(UploadedFile $fileInstance, $dir = '', $resizeWidth = null, $resizeHeight = null, $resizeCrop = false)
 {
     $fileName = Upload::getUploadPath($dir) . DIRECTORY_SEPARATOR . Upload::getFileName($fileInstance);
     $uploaded = $resizeWidth ? self::copyResizedImage($fileInstance->tempName, $fileName, $resizeWidth, $resizeHeight, $resizeCrop) : $fileInstance->saveAs($fileName);
     if (!$uploaded) {
         throw new HttpException(500, 'Cannot upload file "' . $fileName . '". Please check write permissions.');
     }
     return Upload::getLink($fileName);
 }
Ejemplo n.º 4
0
Archivo: Image.php Proyecto: semnt/tp01
 public function save()
 {
     $model = parent::save();
     if (strlen($this->upload_key)) {
         if ($this->upload_key == '__clear__') {
             $model->deleteImage();
             $model->save();
         } else {
             $path = \app\helpers\Upload::getTmpUploadPath($this->upload_key);
             if (is_file($path)) {
                 $model->pullImage($path);
                 $model->save();
                 FileHelper::unlink($path);
             }
         }
     }
     return $model;
 }
Ejemplo n.º 5
0
 /**
  * Upload and resize product image
  *
  * 1. Get path
  * 2. Generate file name
  * 3. Upload
  * 4. Resize
  * 5. Delete old temporary image(s)
  *
  * @param \Symfony\Component\HttpFoundation\File\UploadedFile|array $file
  * @param string                                                    $currentImage
  *
  * @return array
  */
 protected function _uploadProductImage($file, $currentImage)
 {
     // 1
     $tempPath = config('front.temp_path');
     // 2
     $filename = new FileName($tempPath, $file->getClientOriginalExtension());
     $filename->setPrefix(_const('PRODUCT_PREFIX'))->product()->generate();
     $filename->group(['big' => ['width' => _const('PRODUCT_BIG'), 'height' => _const('PRODUCT_BIG')], 'medium' => ['width' => _const('PRODUCT_MEDIUM'), 'height' => _const('PRODUCT_MEDIUM')], 'thumb' => ['width' => _const('PRODUCT_THUMB'), 'height' => _const('PRODUCT_THUMB')]], false);
     // 3
     $upload = new Upload($file);
     $upload->setDirectory($tempPath)->setName($filename->getName())->move();
     // 4
     $image = new Image($tempPath . $filename->getName());
     $image->setDirectory($tempPath)->resizeGroup($filename->getGroup());
     // 5
     foreach ($this->_productImgSizes as $size) {
         $nameBySize = str_replace(_const('TOBEREPLACED'), "_{$size}", $currentImage);
         delete_file($tempPath . $nameBySize);
     }
     delete_file($tempPath . $currentImage);
     return ['image' => $image, 'temp_path' => $tempPath, 'filename' => $filename];
 }
Ejemplo n.º 6
0
 /**
  * Save user
  *
  * @param Illuminate\Http\Request $request
  *
  * @return response
  *
  * @throws \Exception
  */
 public function save(Request $request)
 {
     if ($request->isMethod('POST')) {
         $edit = $request->has('id');
         $user = $edit ? $this->_getUserById($request->get('id')) : $this->user;
         $rules = $this->user->rules();
         $messages = $this->user->messages();
         if ($edit && str_equal($user->username, $request->get('username'))) {
             $rules = remove_rules($rules, ['username.unique:users,username']);
         }
         if ($edit && str_equal($user->email, $request->get('email'))) {
             $rules = remove_rules($rules, ['email.unique:users,email']);
         }
         if ($edit) {
             $rules = remove_rules($rules, ['password.required']);
         }
         $validator = Validator::make($request->all(), $rules, $messages);
         if ($validator->fails()) {
             return back()->withInput()->withErrors($validator, 'all');
         }
         try {
             $except = [];
             if ($request->password === '') {
                 $except = ['password'];
             }
             $user = $this->bind($user, $request->all(), $except);
             if (!$edit) {
                 $user->created_at = new \DateTime();
             }
             $user->updated_at = new \DateTime();
             //Upload avatar
             if ($request->hasFile('avatar')) {
                 $avatarPath = config('back.avatar_path');
                 $file = $request->file('avatar');
                 $filename = new FileName($avatarPath, $file->getClientOriginalExtension());
                 $filename->avatar()->generate();
                 $filename->setPrefix(_const('AVATAR_PREFIX'));
                 $filename->avatar()->group($this->_getAvatarGroup(), false);
                 $upload = new Upload($file);
                 $upload->setDirectory($avatarPath)->setName($filename->getName())->move();
                 $image = new Image($avatarPath . $upload->getName());
                 $image->setDirectory($avatarPath)->resizeGroup($filename->getGroup());
                 delete_file($avatarPath . $upload->getName());
                 $resizes = $image->getResizes();
                 $user->avatar = $resizes['small'];
             }
             $user->save();
         } catch (Exception $ex) {
             throw new \Exception(_t('backend_common_opp') . $ex->getMessage());
         }
         return redirect(route('backend_users'))->with('success', _t('backend_common_saved'));
     }
 }
Ejemplo n.º 7
0
 public function ajaxChangeCover(Request $request)
 {
     if ($request->isMethod('POST')) {
         $rules = $this->_getCoverRules();
         $messages = $this->_getCoverMessages();
         $validator = Validator::make($request->all(), $rules, $messages);
         if ($validator->fails()) {
             return file_pong(['status' => _const('AJAX_ERROR'), 'messages' => $validator->errors()->first()], 403);
         }
         /**
          * 1. Get file, path and info
          * 2. Generate file name
          * 3. Upload
          * 4. Resize
          * 5. Delete old cover images and upload image
          * 6. Save new info
          */
         try {
             // 1
             $store = store();
             $coverPath = config('front.cover_path');
             $file = $request->file('__file');
             // 2
             $filename = new FileName($coverPath, $file->getClientOriginalExtension());
             $filename->cover()->generate();
             $filename->setPrefix(_const('COVER_PREFIX'));
             $filename->cover()->group($this->_getCoverGroup(), true);
             // 3
             $upload = new Upload($file);
             $upload->setDirectory($coverPath)->setName($filename->getName())->move();
             // 4
             $image = new Image($coverPath . $upload->getName());
             $image->setDirectory($coverPath)->resizeGroup($filename->getGroup());
             // 5
             delete_file([$coverPath . $upload->getName(), $coverPath . $store->cover_original, $coverPath . $store->cover_big, $coverPath . $store->cover_medium, $coverPath . $store->cover_small]);
             // 5
             $resizes = $image->getResizes();
             $store->cover_original = $resizes['original'];
             $store->cover_big = $resizes['big'];
             $store->cover_medium = $resizes['medium'];
             $store->cover_small = $resizes['small'];
             $store->update();
         } catch (Exception $ex) {
             $validator->errors()->add('__file', _t('opp'));
             return file_pong(['status' => _const('AJAX_OK'), 'messages' => $validator->errors()->first()], 500);
         }
         return file_pong(['status' => _const('AJAX_OK'), 'messages' => _t('saved_info'), 'data' => ['big' => asset($coverPath . $resizes['big']), 'medium' => asset($coverPath . $resizes['medium']), 'small' => asset($coverPath . $resizes['small'])]]);
     }
 }