Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/huggingface/text-to-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
'task' => Task::TEXT_TO_IMAGE,
]);

echo $result->asBase64().\PHP_EOL;
echo $result->asDataUri().\PHP_EOL;
6 changes: 3 additions & 3 deletions src/platform/src/Result/BinaryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public function toBase64(): string
return base64_encode($this->data);
}

public function toDataUri(): string
public function toDataUri(?string $mimeType = null): string
{
if (null === $this->mimeType) {
if (null === ($mimeType ?? $this->mimeType)) {
throw new RuntimeException('Mime type is not set.');
}

return 'data:'.$this->mimeType.';base64,'.$this->toBase64();
return 'data:'.($mimeType ?? $this->mimeType).';base64,'.$this->toBase64();
}
}
4 changes: 2 additions & 2 deletions src/platform/src/Result/DeferredResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public function asBinary(): string
/**
* @throws ExceptionInterface
*/
public function asBase64(): string
public function asDataUri(?string $mimeType = null): string
{
$result = $this->as(BinaryResult::class);

\assert($result instanceof BinaryResult);

return $result->toDataUri();
return $result->toDataUri($mimeType);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/platform/tests/Result/BinaryResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,13 @@ public function testToDataUriThrowsExceptionWhenMimeTypeNotSet()

$result->toDataUri();
}

public function testToDataUriWithMimeTypeExplicitlySet()
{
$result = new BinaryResult('binary data');
$actual = $result->toDataUri('image/jpeg');
$expected = 'data:image/jpeg;base64,'.base64_encode('binary data');

$this->assertSame($expected, $actual);
}
}