/** * Resize user avatar from uploaded picture * @param \Symfony\Component\HttpFoundation\File\UploadedFile|\Symfony\Component\HttpFoundation\File\File $original * @param int $user_id * @param string $size * @throws \Exception * @return null */ public function resizeAndSave($original, $user_id, $size = 'small') { $sizeConvert = ['big' => [400, 400], 'medium' => [200, 200], 'small' => [100, 100]]; if (!array_key_exists($size, $sizeConvert)) { return null; } $image = new Image(); $image->setCacheDir(root . '/Private/Cache/images'); $image->open($original->getPathname())->cropResize($sizeConvert[$size][0], $sizeConvert[$size][1])->save(root . '/upload/user/avatar/' . $size . '/' . $user_id . '.jpg', 'jpg', static::COMPRESS_QUALITY); return null; }
public function register() { $di = $this->getContainer(); $config = []; if (isset($di->get('config')['gregwarImage']) && is_array($di->get('config')['gregwarImage'])) { $config = $di->get('config')['gregwarImage']; } $di->add('Gregwar\\Image\\Image', function ($filepath = null, $w = null, $h = null) use($config) { $image = new Image($filepath, $w, $h); if (isset($config['cache_dir'])) { $image->setCacheDir($config['cache_dir']); } return $image; }); }
/** * Save input data to database */ public function make() { // save data to db $this->_record->title = $this->title; $this->_record->text = $this->text; $this->_record->path = $this->path; $this->_record->category_id = (int) $this->categoryId; $this->_record->display = 0; // set to premoderation $this->_record->author_id = (int) $this->authorId; if ($this->_new === true) { $this->_record->comment_hash = $this->generateCommentHash(); } $this->_record->save(); // work with poster data if ($this->poster !== null) { // lets move poster from tmp to gallery $originDir = '/upload/gallery/' . $this->_record->id . '/orig/'; $thumbDir = '/upload/gallery/' . $this->_record->id . '/thumb/'; if (!Directory::exist($originDir)) { Directory::create($originDir); } if (!Directory::exist($thumbDir)) { Directory::create($thumbDir); } $fileName = App::$Security->simpleHash($this->poster->getClientOriginalName() . $this->poster->getSize()); $newFullName = $fileName . '.' . $this->poster->guessExtension(); // move poster to upload gallery directory $this->poster->move(Normalize::diskFullPath($originDir), $newFullName); // initialize image resizer $thumb = new Image(); $thumb->setCacheDir(root . '/Private/Cache/images'); // open original file, resize it and save $thumbSaveName = Normalize::diskFullPath($thumbDir) . '/' . $fileName . '.jpg'; $thumb->open(Normalize::diskFullPath($originDir) . DIRECTORY_SEPARATOR . $newFullName)->cropResize($this->_configs['galleryResize'])->save($thumbSaveName, 'jpg', 90); $thumb = null; // update poster in database $this->_record->poster = $newFullName; $this->_record->save(); } }
/** * Upload new files to content item gallery * @param int $id * @return string * @throws ForbiddenException * @throws NativeException * @throws \Exception */ public function actionGalleryupload($id) { // check if id is passed if (Str::likeEmpty($id)) { throw new NativeException('Wrong input data'); } // check if user have permission to access there if (!App::$User->isAuth() || !App::$User->identity()->getRole()->can('global/file')) { throw new NativeException(__('Permissions to upload is denied')); } // check if directory exist if (!Directory::exist('/upload/gallery/' . $id)) { Directory::create('/upload/gallery/' . $id); } // get file object /** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */ $file = $this->request->files->get('file'); if ($file === null || $file->getError() !== 0) { throw new NativeException(__('Unexpected error in upload process')); } // check file size if ($file->getSize() < 1 || $file->getSize() > $this->maxSize) { throw new ForbiddenException(__('File size is too big. Max size: %size%kb', ['size' => (int) ($this->maxSize / 1024)])); } // check file extension if (!Arr::in($file->guessExtension(), $this->allowedExt)) { throw new ForbiddenException(__('File extension is not allowed to upload. Allowed: %s%', ['s' => implode(', ', $this->allowedExt)])); } // create origin directory $originPath = '/upload/gallery/' . $id . '/orig/'; if (!Directory::exist($originPath)) { Directory::create($originPath); } // lets make a new file name $fileName = App::$Security->simpleHash($file->getClientOriginalName() . $file->getSize()); $fileNewName = $fileName . '.' . $file->guessExtension(); // check if image is already loaded if (File::exist($originPath . $fileNewName)) { throw new ForbiddenException(__('File is always exists!')); } // save file from tmp to gallery origin directory $file->move(Normalize::diskFullPath($originPath), $fileNewName); // lets resize preview image for it $thumbPath = '/upload/gallery/' . $id . '/thumb/'; if (!Directory::exist($thumbPath)) { Directory::create($thumbPath); } $thumb = new Image(); $thumb->setCacheDir(root . '/Private/Cache/images'); // open original file, resize it and save $thumbSaveName = Normalize::diskFullPath($thumbPath) . '/' . $fileName . '.jpg'; $thumb->open(Normalize::diskFullPath($originPath) . DIRECTORY_SEPARATOR . $fileNewName)->cropResize($this->maxResize)->save($thumbSaveName, 'jpg', 90); $thumb = null; $this->setJsonHeader(); return json_encode(['status' => 1, 'file' => ['thumbnailUrl' => '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg', 'url' => '/upload/gallery/' . $id . '/orig/' . $fileNewName, 'name' => $fileNewName]]); }