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

时间:2011-12-14 10:01:59

标签: php gdlib

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

现在我想在调整te图像后获取base64编码的字符串,但是我无法找到一个函数来获取base64encoded字符串。

1 个答案:

答案 0 :(得分: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);