public function create(Product $product, FileUpload $fileUpload) { switch ($fileUpload->getContentType()) { case 'image/jpeg': $suffix = 'jpg'; break; case 'image/png': $suffix = 'png'; break; case 'image/gif': $suffix = 'gif'; break; default: throw new EntityInvalidArgumentException(sprintf('File is of an unknown type %s.', $fileUpload->getContentType())); } $baseName = sprintf('%s-%%s.%s', Strings::webalize($product->getName()), $suffix); do { $fileName = sprintf($baseName, Random::generate(5, '0-9a-zA-Z')); $path = sprintf('%s/%s', $this->imagesDir, $fileName); } while (file_exists($path)); $fileUpload->move($path); $image = new ProductImage($product, $fileName); $this->createEntity($image); $product->addImage($image); return $image; }
/** * Ověří mimetype předaného souboru. * @param \Nette\Http\FileUpload $file Nahraný soubor k ověření. * @return bool Má soubor správný mimetype? */ public function checkType(\Nette\Http\FileUpload $file) { if (\Nette\Utils\Arrays::searchKey($this->getMimeTypes(), $file->getContentType()) !== FALSE) { return TRUE; } else { // Pokud se nepodaří ověřit mimetype, ověříme alespoň koncovku. if (array_search($this->getExtension($file->getName()), array_unique($this->getMimeTypes())) !== FALSE) { return TRUE; } else { return FALSE; } } }
/** * * @param \Nette\Http\FileUpload $file * @return string */ private function getImageBase64(\Nette\Http\FileUpload $file) { $type = $file->getContentType(); $img = Image::fromFile($file); $img->resize(100, 100, Image::EXACT); $img_string = ""; switch ($type) { case 'image/png': $img_string = $img->toString(Image::PNG); break; case 'image/jpeg': $img_string = $img->toString(Image::JPEG); break; case 'image/gif': $img_string = $img->toString(Image::GIF); break; } return 'data:' . $type . ';base64,' . base64_encode($img_string); }
/** * @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; }
/** * @param Nette\Http\FileUpload $fileUpload * @return self */ public static function createFromNetteFileUpload(Nette\Http\FileUpload $fileUpload) { $new = new self(['name' => Strings::webalize($fileUpload->getName(), '_.'), 'type' => $fileUpload->getContentType(), 'size' => $fileUpload->getSize(), 'tmp_name' => $fileUpload->getTemporaryFile(), 'error' => $fileUpload->getError()]); return $new; }