使用 PHP 输出文本图像时出现问题
Problems outputting text image with PHP
我要在屏幕上将一些文本作为图像打印在页面上 PHP,但它只显示一个白色方块。
主页代码如下:
<?php
SESSION_START();
if ($_SESSION["log"]!=true){
//die("<h1><center>Non sei loggato come amministratore</center></h1>");
header('location:index.php'); }
include('ase.php');
echo ('
<!DOCTYPE html>
<html>
<body>
<a href="./logout.php">Logout</a>
<img src="data:image/png;base64,' . base64_encode($stringdata) . '">
</body></html>');
?>
这里是应该输出图像的页面代码:
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(800, 2000);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 800, 2000, $white);
// The text to draw
$text = "Read the text and the questions below. For each question mark
the letter next to the correct answer - A,B,C or D.";
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
ob_start();
imagepng($im);
$stringdata = ob_get_contents();
ob_end_clean();
?>
(对不起我的英语)
我已经解决了删除 header ( header('Content-Type: image/png');
) 的问题。
我要在屏幕上将一些文本作为图像打印在页面上 PHP,但它只显示一个白色方块。
主页代码如下:
<?php
SESSION_START();
if ($_SESSION["log"]!=true){
//die("<h1><center>Non sei loggato come amministratore</center></h1>");
header('location:index.php'); }
include('ase.php');
echo ('
<!DOCTYPE html>
<html>
<body>
<a href="./logout.php">Logout</a>
<img src="data:image/png;base64,' . base64_encode($stringdata) . '">
</body></html>');
?>
这里是应该输出图像的页面代码:
<?php
// Set the content-type
header('Content-Type: image/png');
// Create the image
$im = imagecreatetruecolor(800, 2000);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 800, 2000, $white);
// The text to draw
$text = "Read the text and the questions below. For each question mark
the letter next to the correct answer - A,B,C or D.";
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
ob_start();
imagepng($im);
$stringdata = ob_get_contents();
ob_end_clean();
?>
(对不起我的英语)
我已经解决了删除 header ( header('Content-Type: image/png');
) 的问题。