tags:

views:

62

answers:

4

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

}
+3  A: 

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.

Ciarán Walsh
+1  A: 

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 ?>

Tor Valamo
+1  A: 

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.

meagar
+2  A: 

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()
Dereleased
Thanks, inside the beginning of the function I placed ob_start() and right before the end I placed ob_end_clean();
usertest
While this is a good protection against general errors, it might also be useful to see what is causing the error; try invoking the script directly (and comment out the call to `header()`) to see what error is being generated, and why
Dereleased
I works when I call it by itself. So something to do with the information being used repeatedly maybe?
usertest