/** * @param FileUpload|NULL $file * @param string $folder * * @return array|bool */ protected function saveImage(FileUpload $file = NULL, $folder = '') { if ($this->mainManager instanceof MagicManager) { $folder = $this->mainManager->getImageFolder(); } else { if ($folder == '') { $folder = 'misc'; } } // Main image $image = NULL; if (isset($file) && $file->isOk()) { // Save ... $year = date('Y'); $month = date('m'); $namespace = "{$folder}/{$year}/{$month}"; $this->imageStorage->setNamespace($namespace); $image = $this->imageStorage->save($file->getContents(), $file->getName()); $filename = pathinfo($image->getFile(), PATHINFO_BASENAME); // Prepare thumbnail $this->imgPipe->setNamespace($namespace); $this->imgPipe->request($filename, '200x200', 'exact'); $this->imgPipe->setNamespace($namespace); $this->imgPipe->request($filename, '100x100', 'exact'); $data = ['namespace' => $namespace, 'filename' => $filename]; return $data; } else { return FALSE; } }
/** * @param Nette\Http\FileUpload $file * @param string $path * @param array $enabledExt * @return bool */ protected function saveFile($file, $path, $enabledExt = array('jpg', 'jpeg')) { if ($file->error) { return false; } $filename = $file->getName(); if (!in_array(self::getExtensionByName($filename), $enabledExt)) { return false; } try { $src = imagecreatefromjpeg($file->getTemporaryFile()); list($width, $height) = getimagesize($file->getTemporaryFile()); $aspectRatio = $width / $height; if ($aspectRatio > 1) { $targetWidth = self::MAX_DIMENSION; $targetHeight = round(self::MAX_DIMENSION / $aspectRatio); } else { $targetWidth = round(self::MAX_DIMENSION * $aspectRatio); $targetHeight = self::MAX_DIMENSION; } $bigThumbnail = imagecreatetruecolor($targetWidth, $targetHeight); imagecopyresampled($bigThumbnail, $src, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height); imagejpeg($bigThumbnail, self::SAVE_DIR . $path, 75); } catch (\Exception $e) { return false; } return true; }
/** * 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 * @param $path */ public function upload(Nette\Http\FileUpload $file, $path) { try { $tempFile = $file->getTemporaryFile(); $uploadPath = $this->wwwDir . $path; $mainFile = $uploadPath . time() . '-' . $file->getName(); move_uploaded_file($tempFile, $mainFile); return $mainFile; } catch (\Exception $e) { header('HTTP/1.1 500 Internal Server Error'); header('Content-type: text/plain'); exit($e->getMessage()); } }
/** * Save file into filesystem and insert information into database. * Checks extension and gives file mime type. * @param FileUpload $file * @param int $goods_id * @param string $name * @param string $description * @return bool success */ public function insertPhoto($file, $goods_id, $name = '', $description = '') { $photo = new Photo($this->db); $filename = $file->getName(); $ext = self::getExtensionByName($filename); // povolene pripony $enabledExt = explode(' ', self::ENABLED_EXTENSION); if (!in_array($ext, $enabledExt)) { return false; } $lastPhoto_id = $photo->insert(array('name' => $name ? $name : $filename, 'goods_id' => $goods_id, 'description' => $description)); $photoFileName = $file->getTemporaryFile(); $photoFileNameBig = self::SAVE_DIR . $lastPhoto_id . '.big.jpg'; $photoFileNameSmall = self::SAVE_DIR . $lastPhoto_id . '.small.jpg'; try { $src = imagecreatefromjpeg($photoFileName); list($width, $height) = getimagesize($photoFileName); $aspectRatio = $width / $height; if ($aspectRatio > 1) { $targetWidth = 800; $targetHeight = round(800 / $aspectRatio); } else { $targetWidth = round(800 * $aspectRatio); $targetHeight = 800; } $bigThumbnail = imagecreatetruecolor($targetWidth, $targetHeight); imagecopyresampled($bigThumbnail, $src, 0, 0, 0, 0, $targetWidth, $targetHeight, $width, $height); imagejpeg($bigThumbnail, $photoFileNameBig, 75); $smallThumbnail = imagecreatetruecolor(round($targetWidth / 2.5), round($targetHeight / 2.5)); imagecopyresampled($smallThumbnail, $src, 0, 0, 0, 0, round($targetWidth / 2.5), round($targetHeight / 2.5), $width, $height); imagejpeg($smallThumbnail, $photoFileNameSmall, 50); } catch (\Exception $e) { $photo = new Photo($this->db); $photo->where('id', $lastPhoto_id)->delete(); return false; } return true; }
/** * Take a FileUpload, save it and return new Image * @param Nette\Http\FileUpload $upload * @param string $namespace * @param string $checksum * @return Image */ public function saveUpload(Nette\Http\FileUpload $upload, $namespace, $checksum = NULL) { if (!$checksum) { $checksum = call_user_func_array($this->algorithm_file, [$upload->getTemporaryFile()]); } list($path, $identifier) = $this->getSavePath(self::fixName($upload->getName()), $namespace, $checksum); $upload->move($path); $image = new Image($this->friendly_url, $this->data_dir, $this->data_path, $identifier, ['sha' => $checksum, 'name' => self::fixName($upload->getName())]); return $image; }
public static function fromFileUpload(FileUpload $fileUpload) { return new static($fileUpload->getTemporaryFile(), $fileUpload->getName()); }
/** * Return file upload error message. * @param FileUpload $file * @param UI\Presenter $presenter * @return bool */ public static function hasFileError(FileUpload $file, UI\Presenter &$presenter, $noFileError = TRUE) { switch ($file->getError()) { case UPLOAD_ERR_INI_SIZE: $presenter->flashMessage('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'error'); $return = FALSE; break; case UPLOAD_ERR_FORM_SIZE: $presenter->flashMessage('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'error'); $return = FALSE; break; case UPLOAD_ERR_PARTIAL: $presenter->flashMessage('The uploaded file was only partially uploaded.', 'error'); $return = FALSE; break; case UPLOAD_ERR_NO_FILE: if ($noFileError === FALSE) { $presenter->flashMessage('No file was uploaded.', 'error'); } $return = $noFileError; break; case UPLOAD_ERR_NO_TMP_DIR: $presenter->flashMessage('Missing a temporary folder.', 'error'); $return = FALSE; break; case UPLOAD_ERR_CANT_WRITE: $this->flashMessage('Failed to write file to disk.', 'error'); $return = FALSE; break; case UPLOAD_ERR_EXTENSION: $presenter->flashMessage('File upload stopped by extension.', 'error'); $return = FALSE; break; default: $presenter->flashMessage('Chyba při zpracování souboru ' . $file->getName(), 'error'); $return = FALSE; break; } return $return; }
/** * @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; }
/** * @param \Nette\Http\FileUpload * @param string|NULL * @return string */ public function generatePath(\Nette\Http\FileUpload $upload, $path = "") { $ext = pathinfo($upload->getName(), PATHINFO_EXTENSION); $path .= "/" . time() . "_" . \Nette\Utils\Strings::random() . "." . $ext; return $path; }
/** * @param Http\FileUpload $fileUpload * @param string|NULL $path * @return Image */ public function saveFileUpload(Http\FileUpload $fileUpload, $path = NULL) { return $this->saveImage($fileUpload->toImage(), $path, pathinfo($fileUpload->getName(), PATHINFO_EXTENSION)); }
/** * @param Nette\Http\FileUpload $file * @return string|NULL */ public function saveUpload(Nette\Http\FileUpload $file) { if ($file->isOk()) { $fileName = $this->getFileNameForSave($file->getName()); $origPath = $this->getOrigPath($fileName); $file->move($origPath); if ($this->createImage($origPath, $this->getCompressionPath($fileName))) { return basename($origPath); } } return NULL; }
/** * Store file from nette form * * @param \Nette\Http\FileUpload $file * @return mixed * @throws \Nette\IOException */ public function storeNetteFile(\Nette\Http\FileUpload $file) { return $this->store($file->getTemporaryFile(), $file->getName(), true); }
/** * File upload handler. Saves file into upload folder and saves origin name to cache. * * @return void */ public function handlePluploadFile() { // $this->getParameter('token') is not working if (!array_key_exists($this->getParameterId('token'), $_GET)) { $this->presenter->sendJson(array('status' => 'error', 'message' => 'Token is missing.')); } $token = $_GET[$this->getParameterId('token')]; if (empty($_FILES)) { $this->presenter->sendJson(array('File is missing.')); } $file = new Nette\Http\FileUpload(end($_FILES)); if (!$file->isOk()) { $this->presenter->sendJson(array('status' => 'error', 'message' => $this->getError($file->getError()))); } $tempName = uniqid('np-') . '.' . pathinfo($file->getName(), PATHINFO_EXTENSION); $file->move($this->form->getUploadDir() . '/' . $tempName); $files = $this->form->cache->load($token, function () { return array(); }); $files[] = $file; $this->form->cache->save($token, $files); $this->presenter->sendJson(array('status' => 'success')); }