/** * @param FileUpload $file * @return Image * @throws NotImageUploadedException * @throws FileSizeException * @throws DBALException * @throws InvalidStateException */ public function processImage(FileUpload $file) { if (!$file->isImage()) { throw new NotImageUploadedException(); } if (\filesize($file->getTemporaryFile()) > Image::MAX_FILE_SIZE) { throw new FileSizeException(); } try { $this->em->beginTransaction(); $image = new Image($file); $this->em->persist($image)->flush(); $file->move($this->composeImageLocation($image)); $this->em->commit(); } catch (InvalidStateException $is) { $this->em->rollback(); $this->em->close(); $this->logger->addError('Error occurs while moving temp. image file to new location.'); throw $is; } catch (DBALException $e) { $this->em->rollback(); $this->em->close(); $this->logger->addError('Image error'); // todo err message throw $e; } return $image; }
/** * @param FileUpload $file * @return array|void */ public function save(FileUpload $file) { if ($file->isImage()) { return $this->imageSaver->save($file); } else { return $this->fileSaver->save($file); } }
/** * @param FileUpload $file * @return Image * @throws \Nette\InvalidArgumentException */ public function upload(FileUpload $file) { if (!$file->isOk() || !$file->isImage()) { throw new Nette\InvalidArgumentException(); } do { $name = Random::generate() . '.' . $file->getSanitizedName(); } while (file_exists($path = $this->imagesDir . "/" . $this->namespace . $this->originalPrefix . "/" . $name)); $file->move($path); $this->onUploadImage($path, $this->namespace); $this->namespace = NULL; return new Image($path); }
/** * @param FileUpload $fileUpload * @param string $namespace * @param callable $callback * @return string Absolute name */ public function saveUpload(FileUpload $fileUpload, $namespace = NULL, callable $callback = NULL) { if (!$fileUpload->isOk() || !$fileUpload->isImage()) { return NULL; } $image = $this->createImage(); $image->setNamespace($namespace); $image->setName($fileUpload->getSanitizedName()); if ($callback) { $callback($image); } $image->saveUpload($fileUpload); return (string) $image; }
public function savaOriginImage($namespace, FileUpload $file) { if (!$file->isOk() || !$file->isImage()) { throw new ImageStorageException("invalid image"); } $i = 1; $name = $file->getSanitizedName(); $path = $this->getPath($namespace, $name); while (file_exists($path)) { $name = $i . '-' . $file->getSanitizedName(); $path = $this->getPath($namespace, $name); $i++; } $file->move($path); return $name; }
public function __construct(FileUpload $file) { if (!$file->isImage()) { throw new NotImageUploadedException(); } $this->id = Uuid::uuid4(); $pathInfo = pathinfo($file->getSanitizedName()); if (!isset($pathInfo['extension'])) { throw new FileNameException('Filename must have extension'); } $this->extension = $pathInfo['extension']; $this->originalName = $pathInfo['filename']; $imgSize = $file->getImageSize(); $this->width = $imgSize[0]; $this->height = $imgSize[1]; $this->fileSize = $file->getSize(); $this->uploadedAt = new \DateTime('now'); }
/** * @param FileUpload $file */ public function process(FileUpload $file) { // check if ($file->isImage() && $file->isOk()) { // lets rock foreach ($this->thumbs as $thumb) { $thumb->setOriginalName($file->sanitizedName); $this->onProcess($file, $thumb); } // Fire complete handlers.. $this->onComplete($this); } if (count($this->errors) > 0) { // Fire error handlers.. $this->onError($this, $this->errors); } else { // Fire sucess handlers.. $this->onSuccess($this, $this->images); } }
/** * @param FileUpload * @param string|NULL * @param array|string|NULL * @return string filepath (namespace/file.ext) * @throws ImageStorageException */ public function upload(FileUpload $image, $namespace = NULL, $mimeTypes = NULL) { if (!$image->isOk()) { throw new ImageStorageException('File is broken'); } if (!$image->isImage()) { $contentType = $image->getContentType(); $isValid = FALSE; if (isset($mimeTypes)) { $mimeTypes = is_array($mimeTypes) ? $mimeTypes : explode(',', $mimeTypes); $isValid = in_array($contentType, $mimeTypes, TRUE); } if (!$isValid) { throw new ImageStorageException('File must be image, ' . $contentType . ' given'); } } $sanitizedName = $image->getSanitizedName(); $ext = pathinfo($sanitizedName, PATHINFO_EXTENSION); $sanitizedName = pathinfo($sanitizedName, PATHINFO_FILENAME) . '.' . Strings::lower($ext); $file = $this->generateFilePath($sanitizedName, $namespace); $path = $this->getPath($file); $image->move($path); return $file; }
public function save(FileUpload $file) { if ($file->isImage()) { return parent::save($file); } }
/** * @param FileUpload $fileUpload * @param NULL|string $dir * @return SplFileInfo * @throws InvalidArgumentException */ public function upload(FileUpload $fileUpload, $dir = NULL) { if (!$fileUpload->isImage()) { throw new InvalidArgumentException('This is not an image!'); } $path = $this->basePath . '/' . $this->relativePath; if ($dir !== NULL) { $path .= '/' . $dir; } $path = Utils::normalizePath($path); Utils::makeDirectoryRecursive($path); $filename = Utils::sanitizeFileName($fileUpload); /** @var \Nette\Utils\Image */ $image = $fileUpload->toImage(); if ($this->saveOriginal) { $image->save($path . '/orig_' . $filename); } if ($this->type !== NULL) { $filename = str_replace('.' . Utils::getSuffix($filename), '.' . $this->suffix, $filename); } $image->resize($this->maxSize[0], $this->maxSize[1], Image::SHRINK_ONLY); $image->save($path . '/' . $filename, $this->quality, $this->type); foreach ($this->dimensions as $prefix => $p) { $image->resize($p[0][0], $p[0][1], $p[1]); $image->save($path . '/' . $prefix . '_' . $filename, $this->quality, $this->type); } return new SplFileInfo($path . '/' . $filename); }
/** * @param FileUpload $fileUpload * @param NULL|string $dir * @return SplFileInfo * @throws UploadErrorException */ public function singleFileToDir(FileUpload $fileUpload, $dir = NULL) { if ($error = $fileUpload->getError()) { throw new UploadErrorException($error); } $name = $fileUpload->isImage() ? 'imageManager' : 'fileManager'; /** @var IUploadManager $usedManager */ $usedManager = $this->{$name}; $path = Utils::normalizePath($usedManager->getRelativePath() . '/' . $dir); $this->onFileBegin($fileUpload, $path); $uploadedFile = $usedManager->upload($fileUpload, $dir); $this->onFileComplete($fileUpload, $uploadedFile, $path); return $uploadedFile; }
/** * @param string $namespace * @param FileUpload $fileUpload * @return SplFileInfo */ public function upload($namespace, FileUpload $fileUpload) { if (!$fileUpload->isImage()) { throw new InvalidArgumentException('This is not an image!'); } if (!is_dir($this->tempDir)) { Utils::makeDirectoryRecursive($this->tempDir); } $filename = Utils::sanitizeFileName($fileUpload); /** @var \Nette\Utils\Image */ $image = $fileUpload->toImage(); if ($this->saveOriginal) { $image->save($this->tempDir . '/orig_' . $filename); $savedOriginal = ["{$this->tempDir}/orig_{$filename}", "{$namespace}/orig_{$filename}"]; } if ($this->type !== NULL) { $filename = str_replace('.' . Utils::getSuffix($filename), '.' . $this->suffix, $filename); } $image->resize($this->maxSize[0], $this->maxSize[1], Image::SHRINK_ONLY); $image->save("{$this->tempDir}/{$filename}", $this->quality, $this->type); $filesToSave = []; // has to be first $filesToSave[] = ["{$this->tempDir}/{$filename}", "{$namespace}/{$filename}"]; // intently saved at the second position if (isset($savedOriginal)) { $filesToSave[] = $savedOriginal; } foreach ($this->dimensions as $prefix => $p) { $image->resize($p[0][0], $p[0][1], $p[1]); $image->save("{$this->tempDir}/{$prefix}_{$filename}", $this->quality, $this->type); $filesToSave[] = ["{$this->tempDir}/{$prefix}_{$filename}", "{$namespace}/{$prefix}_{$filename}"]; } $results = $this->storage->bulkSave($filesToSave); // cleanup temp files foreach ($filesToSave as $file) { if (is_file($file[0])) { FileSystem::delete($file[0]); } } // remove complete directory if (is_dir($this->tempDir)) { FileSystem::delete($this->tempDir); } return new SplFileInfo($results[0]); }