public function File::getDataUri in Helper 8
Converts a file URL into a data URI.
Parameters
string $uri: The file URI.
bool $base_64_encode: TRUE to return the data URI as base-64 encoded content.
string|null $mimetype: The optional mime type to provide for the data URI. If not provided the mime type guesser service will be used.
Return value
string The image data URI for use in a src attribute.
Throws
\Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException If the file cannot be read.
File
- src/
File.php, line 100
Class
- File
- Provides helpers for working with files.
Namespace
Drupal\helperCode
public function getDataUri($uri, $base_64_encode = TRUE, $mimetype = NULL) {
if (!isset($mimetype)) {
$mimetype = $this->mimeTypeGuesser
->guess($uri);
}
$contents = file_get_contents($uri);
if ($contents === FALSE) {
throw new AccessDeniedException($uri);
}
if ($base_64_encode) {
$contents = base64_encode($contents);
}
return 'data:' . $mimetype . ($base_64_encode ? ';base64' : '') . ',' . $contents;
}