File tree Expand file tree Collapse file tree 4 files changed +15
-6
lines changed Expand file tree Collapse file tree 4 files changed +15
-6
lines changed Original file line number Diff line number Diff line change 2020 'task ' => Task::TEXT_TO_IMAGE ,
2121]);
2222
23- echo $ result ->asBase64 ().\PHP_EOL ;
23+ echo $ result ->asDataUri ().\PHP_EOL ;
Original file line number Diff line number Diff line change @@ -39,12 +39,12 @@ public function toBase64(): string
3939 return base64_encode ($ this ->data );
4040 }
4141
42- public function toDataUri (): string
42+ public function toDataUri (? string $ mimeType = null ): string
4343 {
44- if (null === $ this ->mimeType ) {
44+ if (null === ( $ mimeType ?? $ this ->mimeType ) ) {
4545 throw new RuntimeException ('Mime type is not set. ' );
4646 }
4747
48- return 'data: ' .$ this ->mimeType .';base64, ' .$ this ->toBase64 ();
48+ return 'data: ' .( $ mimeType ?? $ this ->mimeType ) .';base64, ' .$ this ->toBase64 ();
4949 }
5050}
Original file line number Diff line number Diff line change @@ -90,13 +90,13 @@ public function asBinary(): string
9090 /**
9191 * @throws ExceptionInterface
9292 */
93- public function asBase64 ( ): string
93+ public function asDataUri (? string $ mimeType = null ): string
9494 {
9595 $ result = $ this ->as (BinaryResult::class);
9696
9797 \assert ($ result instanceof BinaryResult);
9898
99- return $ result ->toDataUri ();
99+ return $ result ->toDataUri ($ mimeType );
100100 }
101101
102102 /**
Original file line number Diff line number Diff line change @@ -58,4 +58,13 @@ public function testToDataUriThrowsExceptionWhenMimeTypeNotSet()
5858
5959 $ result ->toDataUri ();
6060 }
61+
62+ public function testToDataUriWithMimeTypeExplicitlySet ()
63+ {
64+ $ result = new BinaryResult ('binary data ' );
65+ $ actual = $ result ->toDataUri ('image/jpeg ' );
66+ $ expected = 'data:image/jpeg;base64, ' .base64_encode ('binary data ' );
67+
68+ $ this ->assertSame ($ expected , $ actual );
69+ }
6170}
You can’t perform that action at this time.
0 commit comments