diff --git a/src/image/Helper/ImageHelper.php b/src/image/Helper/ImageHelper.php index 172e9be92..cc1e1e581 100644 --- a/src/image/Helper/ImageHelper.php +++ b/src/image/Helper/ImageHelper.php @@ -12,7 +12,6 @@ namespace WBW\Library\Image\Helper; use RuntimeException; -use SplFileObject; use WBW\Library\Image\Factory\ImageFactory; use WBW\Library\Image\Model\Image; use WBW\Library\Image\Model\ImageInterface; @@ -37,14 +36,16 @@ public static function base64Encode(?string $uri): ?string { return null; } - $data = ""; + $data = file_get_contents($uri); - $splFileObject = new SplFileObject($uri); - while (false === $splFileObject->eof()) { - $data .= $splFileObject->fgets(); - } + $stream = fopen("php://memory", "w+b"); + fwrite($stream, $data); + + $mime = mime_content_type($stream); + + fclose($stream); - return sprintf("data:%s;base64,%s", mime_content_type($uri), base64_encode($data)); + return sprintf("data:%s;base64,%s", $mime, base64_encode($data)); } /** diff --git a/tests/image/Helper/ImageHelperTest.php b/tests/image/Helper/ImageHelperTest.php index 7e25c1a17..34dc9dd6c 100644 --- a/tests/image/Helper/ImageHelperTest.php +++ b/tests/image/Helper/ImageHelperTest.php @@ -51,10 +51,12 @@ protected function setUp(): void { public function testBase64Encode(): void { // Set the mocks. - $src = realpath($this->images[0]); + $uri = realpath($this->images[0]); + $url = "https://raw.githubusercontent.com/webeweb/core-library/master/tests/image/Fixtures/TestImage_1920x1037.jpg"; $res = file_get_contents(__DIR__ . "/ImageHelperTest.testBase64Encode.txt"); - $this->assertEquals($res, ImageHelper::base64Encode($src)); + $this->assertEquals($res, ImageHelper::base64Encode($uri)); + $this->assertEquals($res, ImageHelper::base64Encode($url)); $this->assertNull(ImageHelper::base64Encode(null)); }