图像编辑功能
我创建了一个将文本叠加到图像上的函数,但我正在绞尽脑汁寻找一种输出图像的方法,以便可以在另一个脚本中使用它或输出到屏幕。
我可以从 php 脚本调用此函数,向其传递要使用的图像和文本,但是当函数返回数据时(由于标头,它会接管页面),我得到的输出就是图像。
我怀疑这是一个简单的问题,只是显示了我的 PHP 知识中的一个漏洞 - 有人可以在这里纠正我吗?
谢谢!
函数 makeimage($file, $text) { ob_start(); $x = getimagesize($文件); $宽度=$x[0]; $高度= $x[1]; $类型=$x[2];
//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;
我
想创建这个图像,然后以这样的方式输出它,以便我可以将它显示在屏幕上的所有其他输出中。
I've created a function to overlay text onto images, but I'm scratching my head to find a way of outputting the image in a way in which it can be utilised within another script or outputted to the screen.
I can call this function from a php script, passing it the image and the text to use, but when the function returns the data - it takes over the page due to the header - all I get as output is the image.
I suspect this is an easy one and just shows a hole in my PHP knowledge - can someone set me straight here?
Thanks!
function makeimage($file, $text) {
ob_start();
$x = getimagesize($file);
$width = $x[0];
$height = $x[1];
$type = $x[2];
//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;
}
I want to create this image, and then have it outputted in such a way as I can display it on screen in amongst all the other output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想直接输出图像,只需不要在函数中发送标头即可。每个浏览器都会将响应视为图像文件。
另外,返回后的 imagedestroy($im) 永远不会被执行!
如果您只想创建图像并将其保存到文件中,请检查 imagepng () 文档。第二个参数接受文件名:
根据您的编辑:
您应该使用带有 filename 参数的 imagepng 来创建图像,然后从脚本链接到该图像。
小例子:
另一种方法是 Davide Gualanos 解决方案,使用包装器脚本传递 image/png 标头并直接输出。
If you don't want to output the image directly, just don't send the header in your function. Every browser will treat the response as image file.
Also, imagedestroy($im) after return will never get executed!
If you want to just create the image and save it to a file, check the imagepng() documentation. The second parameter accepts a filename:
According to your edit:
You should use imagepng with the filename parameter to create the image, and then link to it from your script.
Small example:
The other way around would be Davide Gualanos solution by using a wrapper script to passtrough image/png headers and direct output.
您必须将输出图像的脚本放在页面中,并在 html 标记中调用该页面来显示它。
示例:
image.php
page.html:
You have to put the script that output the image in a page, and call that page in a html tag to display it.
Example:
image.php
page.html:
您可以使用临时输出缓冲区来捕获函数的输出,然后用它填充字符串
示例:
ob_start();
...你的图像函数代码...
ob_clean(); //确保输出缓冲区中没有其他字符串
imagepng($im);
$string = ob_get_contents();
ob_end_clean();
返回$字符串;
$string 获取 imagepng() 的所有输出;
you can use a temporary output buffer to catch the output of the function and then having a string filled with it
Example:
ob_start();
... your image function code ...
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;
$string got all the output from imagepng();