【问题标题】:PHP imagejpeg - convert output to base64PHP imagejpeg - 将输出转换为base64
【发布时间】:2018-07-19 15:28:30
【问题描述】:

我正在像这样的图像中生成一个简单的文本...

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');

// Output the image
imagejpeg($im);

// Free up memory
imagedestroy($im);

我需要输出为 base64 字符串,我尝试过 base64_encode($im) 但它对我来说不能正常工作。

谁有我可以看到的例子?

【问题讨论】:

  • $im 是一种资源,不能将其用作字符串。获取imagejpeg结果并base64encode

标签: php base64 imagejpeg


【解决方案1】:

尝试使用ob_get_clean 函数从输出缓冲区获取图像,然后将其编码为base64:

// Create a blank image and add some text
$im = imagecreatetruecolor(120, 20);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  'A Simple Text String', $text_color);

ob_start();

// Output the image
imagejpeg($im);

$img = ob_get_clean();
ob_end_clean();

// Free up memory
imagedestroy($im);

echo base64_encode($img);

【讨论】:

    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多