如何从图像资源创建base64encoded字符串

net*_*fin 20 php gdlib

我已经通过AJAX将一个base64编码的字符串发送到PHP并创建了一个图像资源imagecreatefromstring- 一切都很好.

现在我想在调整te图像后获取base64编码的字符串,但我找不到获取base64encoded字符串的函数.

Mic*_*son 46

摘自http://www.php.net/manual/en/book.image.php#93393

$image = imagecreatefromstring($file);

// start buffering
ob_start();
imagepng($image);
$contents =  ob_get_contents();
ob_end_clean();

echo "<img src='data:image/png;base64,".base64_encode($contents)."' />";

imagedestroy($image);
Run Code Online (Sandbox Code Playgroud)

  • [根据手册](https://secure.php.net/manual/en/function.ob-get-clean.php)`$contents = ob_get_clean();`是`ob_get_contents`的一个单线而`ob_end_clean` 确实如此。 (3认同)