Skip to content

Commit

Permalink
base64 and mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jan 24, 2024
1 parent aaa8234 commit 4518dab
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/FileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use SplFileInfo;
use Symfony\Component\Mime\MimeTypesInterface;
use Throwable;
use UnexpectedValueException;
use Windwalker\Data\Collection;
Expand Down Expand Up @@ -452,8 +453,6 @@ public function read(
}

/**
* readStream
*
* @param string $mode
*
* @return StreamInterface
Expand All @@ -463,6 +462,23 @@ public function readStream(string $mode = READ_ONLY_FROM_BEGIN): StreamInterface
return $this->getStream($mode);
}

/**
* @param string|MimeTypesInterface $mime Must provide mime type string or a Symfony MimeTypesInterface
* to detect it automatically.
*
* @return string
*/
public function readBase64DataUri(string|MimeTypesInterface $mime): string
{
if ($mime instanceof MimeTypesInterface) {
$types = $mime->getMimeTypes($this->getExtension());

$mime = $types[0] ?? throw new FilesystemException('Unable to detect file mime type');
}

return 'data:' . $mime . ';base64,' . base64_encode((string) $this->read());
}

public function readAndParse(?string $format = null, array $options = []): Collection
{
if (!class_exists(Collection::class)) {
Expand Down

0 comments on commit 4518dab

Please sign in to comment.