/** * @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; }
/** * @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 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()); } }
private function getInvalidMetadata(FileUpload $song) : array { $invalidMetadata = []; $songReader = new \SongReader($song->getTemporaryFile()); if (!$songReader->getDuration()) { $invalidMetadata[] = 'playtime_string'; } if (!$songReader->getAuthor()) { $invalidMetadata[] = 'artist'; } if (!$songReader->getTitle()) { $invalidMetadata[] = 'title'; } if (!$songReader->getAlbum()) { $invalidMetadata[] = 'album'; } return $invalidMetadata; }
/** * 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; }
/** * @param FileEntityInterface $entity * @param FileUpload $file * * @return FileEntityInterface */ public function upload(FileEntityInterface $entity, FileUpload $file) { $checksum = sha1_file($file->getTemporaryFile()); $pairs = $this->storageDao->findPairs([], 'checksum', [], 'id'); if (in_array($checksum, $pairs)) { $id = array_search($checksum, $pairs); $e = $this->storageDao->find($id); ++$e->joints; return $e; } else { $e = $entity; $y = date('Y'); $m = date('m'); $data = $this->getFileData($file); $file->move($this->uploadDir . '/' . $y . '/' . $m . '/' . $data->name . '.' . $data->extension); $e->name = $data->name; $e->extension = $data->extension; $e->year = $y; $e->month = $m; $e->checksum = $checksum; $e->joints = 1; return $e; } }
/** * Stores the given uploaded file. * * @param FileUpload $upload * @param meta $meta * @return ImageStorage Fluent interface * @throws UploaderException */ public function upload(FileUpload $upload, Meta $meta) { if ($upload->getError()) { throw new UploaderException($upload->getError()); } $source = $upload->getTemporaryFile(); $this->readMeta($source, $meta); $target = $this->createFilename($meta); if (!$this->contains($meta)) { $image = Image::fromFile($source); $image->resize(1920, 1080); $image->save($target); } if (file_exists($source)) { unlink($source); } return $this; }
/** * 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()); }
/** * @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; }
/** * 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); }