문제

How can I get a base64 string from an image resource in PHP? Note however, I made the image on the fly, so no URL exists.

Here is what I've tried, but it doesn't work:

echo 'data:image/png;base64,'.base64_encode($img);

This gives an error:

Warning: base64_encode() expects parameter 1 to be string, resource given
도움이 되었습니까?

해결책

There's no way to tell GD to return your image as a binary string, unfortunately. GD only supports writing to a file or to the screen. What we can do though is use output buffering to capture its output and then put it in a string.

ob_start();
imagepng($img);
$image = ob_get_clean();

echo 'data:image/png;base64,'.base64_encode($image);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top