调用 PHP 函数

发布于 2024-08-15 23:52:39 字数 1553 浏览 3 评论 0原文

我有以下函数,可以根据文本参数生成并保存图像。我如何在我的文件中调用它?我尝试

INCLUDE 'outPrice.php'; 

链接到外部 PHP 并使用此命令调用它,

outPrice($text);

我得到了以下响应。

Warning: Cannot modify header information - headers already sent 

任何帮助将不胜感激。

function outPrice($textval){
    $textcolor = '666666';


    $font="bgtbt.ttf";
    $size = 20;
    $padding= 1;
    $bgcolor= "ffffff";

    $transparent = 0;
    $antialias = 0;

    $fontfile = $fontpath.$font;

    $box= imageftbbox( $size, 0, $fontfile, $textval, array());
    $boxwidth= $box[4];
    $boxheight= abs($box[3]) + abs($box[5]);
    $width= $boxwidth + ($padding*2) + 1;
    $height= $boxheight + ($padding) + 0;
    $textx= $padding;
    $texty= ($boxheight - abs($box[3])) + $padding;

    // create the image
    $png= imagecreate($width, $height);


    $color = str_replace("#","",$bgcolor);
    $red = hexdec(substr($bgcolor,0,2));
    $green = hexdec(substr($bgcolor,2,2));
    $blue = hexdec(substr($bgcolor,4,2));
    $bg = imagecolorallocate($png, $red, $green, $blue);

    $color = str_replace("#","",$textcolor);
    $red = hexdec(substr($textcolor,0,2));
    $green = hexdec(substr($textcolor,2,2));
    $blue = hexdec(substr($textcolor,4,2));
    $tx = imagecolorallocate($png, $red, $green, $blue);


    imagettftext( $png, $size, 0, $textx, $texty, $tx, $fontfile, $textval );

    header("content-type: image/jpeg");
    imagejpeg($png, "price".$textval.".jpg");
    imagedestroy($png);

}

I've got the following function that generates and saves an image based a text parameter. How can I call this in my file? I tried

INCLUDE 'outPrice.php'; 

to link to the external PHP and called it with this command,

outPrice($text);

To which I got the following response.

Warning: Cannot modify header information - headers already sent 

Any help would be appreciated.

function outPrice($textval){
    $textcolor = '666666';


    $font="bgtbt.ttf";
    $size = 20;
    $padding= 1;
    $bgcolor= "ffffff";

    $transparent = 0;
    $antialias = 0;

    $fontfile = $fontpath.$font;

    $box= imageftbbox( $size, 0, $fontfile, $textval, array());
    $boxwidth= $box[4];
    $boxheight= abs($box[3]) + abs($box[5]);
    $width= $boxwidth + ($padding*2) + 1;
    $height= $boxheight + ($padding) + 0;
    $textx= $padding;
    $texty= ($boxheight - abs($box[3])) + $padding;

    // create the image
    $png= imagecreate($width, $height);


    $color = str_replace("#","",$bgcolor);
    $red = hexdec(substr($bgcolor,0,2));
    $green = hexdec(substr($bgcolor,2,2));
    $blue = hexdec(substr($bgcolor,4,2));
    $bg = imagecolorallocate($png, $red, $green, $blue);

    $color = str_replace("#","",$textcolor);
    $red = hexdec(substr($textcolor,0,2));
    $green = hexdec(substr($textcolor,2,2));
    $blue = hexdec(substr($textcolor,4,2));
    $tx = imagecolorallocate($png, $red, $green, $blue);


    imagettftext( $png, $size, 0, $textx, $texty, $tx, $fontfile, $textval );

    header("content-type: image/jpeg");
    imagejpeg($png, "price".$textval.".jpg");
    imagedestroy($png);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

丘比特射中我 2024-08-22 23:52:39

你如何调用这个文件?如果您在 标记内部或从 CSS 指令调用它,请按照 C. Walsh 所说的操作并检查文件中的空格(提示:对于二进制数据,不要不要在脚本中包含结束 ?> 标记)以及 Byte在文件开头添加 Mark(使用十六进制编辑器,例如 PSPad)。

您可能还需要考虑,由于图像脚本执行期间可能会发生错误,因此将脚本内容包装在 ob_start()ob_end_clean(),其中 ob_end_clean() 在发送标头和生成图像之前调用。

如果您直接从要嵌入图像的文件中调用它,请使用以下 HTML 方案:

<img src="data:image/png;base64,DATADATADATA" alt ="" />

其中 DATADATADATA 是图像的 base64 编码版本。

请参阅:

  1. 数据 URI 方案
  2. base64_encode() 的 PHP 手动输入

How are you invoking this file? If you are calling it inside of an <img> tag, or from a CSS directive, then do as C. Walsh says and check for whitespace in the file (tip: for binary data, don't include a closing ?> tag in your script) as well as a Byte Order Mark at the beginning of your file (use a HEX editor like PSPad for this).

You may also want to consider, since errors may occur during your image script's execution, wrapping the script contents in ob_start() and ob_end_clean(), where ob_end_clean() is called just before the headers are sent and the image is generated.

If you are invoking it directly from the file in which you wish to embed the image, then use the following HTML scheme:

<img src="data:image/png;base64,DATADATADATA" alt ="" />

Where DATADATADATA is the base64 encoded version of your image.

See:

  1. Data URI Scheme
  2. PHP Manual Entry for base64_encode()
怕倦 2024-08-22 23:52:39

该消息意味着页面已经向浏览器发送了一些数据 - 如果您认为这不应该发生,那么它可能是 PHP 文件中某处的一些空白。您需要找到它的来源并将其删除。

如果它确实是空格,则此线程 可能会有所帮助。

The message means that the page has already sent some data to the browser – if you don’t think this should have happened then it could be some whitespace somewhere in your PHP files. You’ll need to find where it’s coming from and remove it.

If it does turn out to be whitespace then this thread might be helpful.

戴着白色围巾的女孩 2024-08-22 23:52:39

在发送任何内容作为响应后,不可能使用 header() 函数(倒数第三行),因为在所有 http 通信中都需要首先发送标头。确保您的 php 文件在 之前或 ?> 之后没有空格

Using the header() function (third last line) after any content has been sent as response, is not possible, because headers need to be sent first in all http communications. Make sure that your php files don't have whitespace before <?php or after ?>

浅暮の光 2024-08-22 23:52:39

您收到的错误是由于在函数末尾附近调用“header”之前将输出发送到浏览器而引起的。

您的第一个和最后一个 > 标记之外可能有一些空白。发生这种情况时,PHP 会自动向浏览器发送标头,表明您正在输出 HTML 文档。一旦发生这种情况,您将无法再更改标头,并且 PHP 会发出错误。

追踪输出的来源;尝试查看文档的源代码,并确保所包含的文件中没有前导/尾随空格。

The error you're getting is caused by output being sent to the browser before you call "header" near the end of the function.

Chances are you've got some white space sitting outside of your first and last <?php ?> tags. When this happens, PHP automagically sends headers to the browser indicating that you're outputting an HTML document. Once this has happened, you can no longer alter the headers, and PHP issues an error.

Track down where the output is coming from; try viewing the source of the document, and making sure you have no leading/trailing whitespace in the files that are included.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文