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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8502610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:52:23  来源:igfitidea点击:

how to create a base64encoded string from image resource

phpgdlib

提问by netzaffin

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

我已经通过 AJAX 向 PHP 发送了一个 base64 编码的字符串并创建了一个图像资源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.

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

回答by Michael Robinson

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_clean();

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

imagedestroy($image);