PHP imagejpeg-将输出转换为base64

PHP imagejpeg-将输出转换为base64,php,base64,imagejpeg,Php,Base64,Imagejpeg,我正在像这样的图像中生成一个简单的文本 // 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 -

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

// 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),但它对我来说无法正常工作


有人有我能看到的例子吗?

尝试使用
ob\u get\u 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);

$im是一个资源,不能将其用作字符串。获取imagejpeg结果并对其进行Base64编码