Пример #1
0
 public function actionImage()
 {
     if (substr($this->url, 0, 7) === '_cache/') {
         $this->cached = TRUE;
         $this->url = substr($this->url, 7);
     }
     if (($entity = $this->fileRepository->findOneBy(array('path' => $this->url))) === NULL) {
         throw new \Nette\Application\BadRequestException("File '{$this->url}' does not exist.");
     }
     $image = Image::fromFile($entity->getFilePath());
     // resize
     if ($this->size && $this->size !== 'default') {
         if (strpos($this->size, 'x') !== FALSE) {
             $format = explode('x', $this->size);
             $width = $format[0] !== '?' ? $format[0] : NULL;
             $height = $format[1] !== '?' ? $format[1] : NULL;
             $image->resize($width, $height, $this->format !== 'default' ? $this->format : Image::FIT);
         }
     }
     if (!$this->type) {
         $this->type = substr($entity->getName(), strrpos($entity->getName(), '.'));
     }
     $type = $this->type === 'jpg' ? Image::JPEG : $this->type === 'gif' ? Image::GIF : Image::PNG;
     $file = $this->context->parameters['wwwDir'] . "/public/media/_cache/{$this->size}/{$this->format}/{$this->type}/{$entity->getPath()}";
     $dir = dirname($file);
     umask(00);
     @mkdir($dir, 0777, TRUE);
     $image->save($file, 90, $type);
     $image->send($type, 90);
 }
 /**
  * @param AjaxFileUploaderControl $control
  * @param FileUpload $file
  */
 public function handleFileUpload(AjaxFileUploaderControl $control, $fileName)
 {
     /** @var FileEntity $fileEntity */
     $fileEntity = $this->fileRepository->createNew();
     $fileEntity->setFile(new \SplFileInfo($this->ajaxDir . '/' . $fileName));
     $fileEntity->setParent($this->parentDirectory);
     $fileEntity->setAuthor($control->presenter->user->identity instanceof \CmsModule\Pages\Users\UserEntity ? $control->presenter->user->identity : NULL);
     $fileEntity->copyPermission();
     $this->fileRepository->save($fileEntity);
 }