我已经创建了一个将文本覆盖到图像上的函数,但我正在绞尽脑汁地寻找一种输出图像的方法,以便在另一个脚本中使用或输出到屏幕上。
我可以从php脚本调用这个函数,向它传递要使用的图像和文本,但是当该函数返回数据时-由于头文件,它接管了页面-作为输出,我得到的只是图像。
我怀疑这是一个简单的问题,只是显示了我的PHP知识中的一个漏洞-有人能纠正我的错误吗?
谢谢!
function makeimage($file,$text) { ob_start();$x = getimagesize($file);$width = $x;$height = $x1;$type = $x2;
//header('Content-type: $type');
if ($type == 1) {
$im = imagecreatefromgif($file);
} elseif ($type==2) {
$im = imagecreatefromjpeg($file);
} elseif ($type==3) {
$im = imagecreatefrompng($file);
}
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
// 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);
ob_clean(); //to be sure there are no other strings in the output buffer
imagepng($im);
$string = ob_get_contents();
ob_end_clean();
return $string;}
我想创建这个图像,然后以一种方式输出,以便我可以在屏幕上显示它在所有其他输出中。
发布于 2009-11-27 21:18:00
您必须将输出图像的脚本放在页面中,并在html标记中调用该页面以显示它。
示例:
image.php
<php
$image = $_GET['image'];
$text = $_GET['text'];
makeimage($image, $text);
?>page.html:
<html>
<body>
<img src="image.php?image=foo.jpg&text=hello" />
</body>
</html>发布于 2009-11-27 21:11:37
如果你不想直接输出图像,就不要在你的函数中发送头文件。每个浏览器都会将响应视为图像文件。
此外,返回后的imagedestroy($im)将永远不会被执行!
如果只想创建图像并将其保存到文件,请检查imagepng() documentation。第二个参数接受文件名:
保存文件的路径。如果未设置或为空,则直接输出原始图像流。
根据您的编辑进行:
您应该使用带有filename参数的imagepng来创建图像,然后从脚本链接到该图像。
小示例:
<?php
// ...
imagepng($im, 'foo.png');
?>
<img src="foo.png" />另一种方式是Davide Gualanos解决方案,通过使用包装器脚本传递粗略的图像/png头文件并直接输出。
发布于 2009-11-27 21:17:05
您可以使用临时输出缓冲区来捕获函数的输出,然后用它填充字符串
示例:
ob_start();
..。你的图像函数代码...
ob_clean();//确保输出缓冲区中没有其他字符串
imagepng($im);
$string = ob_get_contents();
ob_end_clean();
返回$string;
$string从imagepng()获得了所有的输出;
https://stackoverflow.com/questions/1808670
复制相似问题