/** * Create media from file * @param string $filePath * @param string|NULL $mimeType * @return Media */ public static function fromFile($filePath, $mimeType = NULL) { if (!$mimeType) { $mimeType = MimeTypeDetector::fromFile($filePath); } return new Media(file_get_contents($filePath), $mimeType); }
/** * Returns the MIME content type of an uploaded file. * @return string */ public function getContentType() { if ($this->isOk() && $this->type === NULL) { $this->type = Nette\Utils\MimeTypeDetector::fromFile($this->tmpName); } return $this->type; }
/** * Replace image with data uri * @param type $url * @param type $quote * @param type $sourcePath */ public static function convert($url, $sourcePath) { $file = $sourcePath . DIRECTORY_SEPARATOR . $url; if (file_exists($file)) { $type = \Nette\Utils\MimeTypeDetector::fromFile($file); return 'data:' . ($type ? "{$type};" : '') . 'base64,' . base64_encode(file_get_contents($file)); } else { return $url; } }
/** * Get format from the image stream in the string. * @param string * @return mixed detected image format */ public static function getFormatFromString($s) { $types = array('image/jpeg' => self::JPEG, 'image/gif' => self::GIF, 'image/png' => self::PNG); $type = Utils\MimeTypeDetector::fromString($s); return isset($types[$type]) ? $types[$type] : NULL; }
/** * The data: URI generator. * @param string * @param string * @return string */ public static function dataStream($data, $type = NULL) { if ($type === NULL) { $type = Nette\Utils\MimeTypeDetector::fromString($data); } return 'data:' . ($type ? "{$type};" : '') . 'base64,' . base64_encode($data); }
/** * @param $file * @param $content * @param $contentType * @param $disposition * @return array * @throws FileNotFoundException */ private function createAttachment($file, $content, $contentType, $disposition) { if ($content === NULL) { $content = @file_get_contents($file); if ($content === FALSE) { throw new FileNotFoundException("Unable to read file '{$file}'."); } } else { $content = (string) $content; } $attachment = array('type' => $contentType ? $contentType : MimeTypeDetector::fromString($content), 'name' => Strings::fixEncoding(basename($file)), 'content' => rtrim(chunk_split(base64_encode($content), self::LINE_LENGTH, self::EOL))); return $attachment; }
/** * Creates file MIME part. * @return MimePart */ private function createAttachment($file, $content, $contentType, $disposition) { $part = new MimePart(); if ($content === NULL) { $content = file_get_contents($file); if ($content === FALSE) { throw new Nette\FileNotFoundException("Unable to read file '{$file}'."); } } else { $content = (string) $content; } $part->setBody($content); $part->setContentType($contentType ? $contentType : Nette\Utils\MimeTypeDetector::fromString($content)); $part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64); $part->setHeader('Content-Disposition', $disposition . '; filename="' . Strings::fixEncoding(basename($file)) . '"'); return $part; }
/** * Prepare file for send * * @param string $filename * @param string $mimetype * @param string $postname * @return \CURLFile|string */ public function fileCreate($filename, $mimetype = NULL, $postname = NULL) { if (PHP_VERSION_ID < 50500) { return '@' . $filename; } if ($mimetype === NULL) { $mimetype = MimeTypeDetector::fromFile($filename); } if ($postname === NULL) { $postname = basename($filename); } return curl_file_create($filename, $mimetype, $postname); }
/** * @return string */ public function getMimeType() { return \Nette\Utils\MimeTypeDetector::fromFile($this->getStorageDir() . "/" . $this->path); }
str_replace($search,$replacement,$subject);}static function dataStream($data,$type=NULL){if($type===NULL){$type=Nette\Utils\MimeTypeDetector::fromString($data,NULL);}return'data:'.($type?"$type;":'').'base64,'.base64_encode($data);}static
UnknownImageFileException("Unknown image type or file '$file' not found.");}}static function getFormatFromString($s){$types=array('image/jpeg'=>self::JPEG,'image/gif'=>self::GIF,'image/png'=>self::PNG);$type=Utils\MimeTypeDetector::fromString($s);return isset($types[$type])?$types[$type]:NULL;}static
/** * Returns the MIME content type of file. * * @param string * * @return string */ public static function fromString($data) { return Nette\Utils\MimeTypeDetector::fromString($data); }
/** * Return's file content type * * @param string $filePath File path * * @return string */ public function fileContentType($filePath) { return \Nette\Utils\MimeTypeDetector::fromFile($filePath); }