调用PHP函数 [英] Calling a PHP function

查看:71
本文介绍了调用PHP函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能可以生成和保存基于文本参数的图像。我怎样才能在我的文件中调用它?我试过了

  INCLUDE'outPrice.php'; 

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

  outPrice($ text); 

我得到以下回应。

 警告:无法修改标题信息 - 已发送标题

任何帮助将不胜感激。

  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;

//创建图片
$ 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);


$ / code>


解决方案

你调用这个文件?如果您在< img> 标记或CSS指令中调用它,请按照C. Walsh的说法进行操作,并检查文件中的空白(提示:对于二进制数据,请勿在脚本中包含结尾?>> 标记)以及字节顺序标记在文件开头(使用HEX编辑器,如标签中。

a href =http://php.net/manual/en/function.ob-end-clean.php =nofollow noreferrer> ob_start() ob_end_clean() ,其中 ob_end_clean() 被称为just然后使用下面的HTML方案:

 < img src =data:image / png; base64,DATADATADATAalt =/> 

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



请参阅:


  1. 数据URI方案

  2. PHP手动输入 base64_encode()


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);

}

解决方案

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()

这篇关于调用PHP函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆