通过转换文本到图像来回显图像

时间:2012-08-10 06:07:06

标签: php image text

我发现这个代码用于转换文本到图像,我想回显结果

<?php
// Set the content-type
header('Content-type: image/png');

// Create the image
$im = imagecreatetruecolor(400, 30);

// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);

// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';

// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);

// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);

// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?> 

例如我将该代码保存到image.php,我可以通过

回显该图像
echo"<img src='image.php'>";

但我不想这样做,因为如果我这样做,我就无法分配变量文本。

还有其他方法可以从该代码中打印图像吗?

1 个答案:

答案 0 :(得分:2)

在你的image.php

替换

 $text = 'Testing...';

 $text=$_GET["text"];

以及您要打印的代码

echo"<img src='image.php?text=AnotherText...'>";

希望这是你想要的?

编辑:

<form action="image.php" method="post">
<textarea name="text"></textarea>
<input type="Submit" value="Submit">
</form>

在image.php文件中而不是

$text=$_GET["text"];

使用

$text=$_POST["text"];
相关问题