/** * 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')); } }
/** * 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]; }
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'])]]); } }