PHP imagejpeg - 将输出转换为 base64
PHP imagejpeg - convert output to 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);
// 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_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);
我正在像这样在图像中生成一个简单的文本...
// 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_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);