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

时间:2022-11-13 12:26:54

I have sent a base64 encoded string via AJAX to PHP and created an image resource with imagecreatefromstring - all is fine.

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

Now I want to get the base64 encoded string after resizing te image, but i CANT find a function to get the base64encoded string.

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

1 个解决方案

#1


42  

Taken from http://www.php.net/manual/en/book.image.php#93393

摘自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);

#1


42  

Taken from http://www.php.net/manual/en/book.image.php#93393

摘自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);