i made a script to display random number in image mode for security code. I have 2 files:

1.login.php
<?php ob_start(); ?>
<html>
<body>
<?php
include("showImg.php");
?>
</body>
</html>
<?php ob_end_flush(); ?>

2.showImg.php
<?php
function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
srand(make_seed());
$Number = rand(11111, 99999);
$imgWidth = 45;
$imgHeight = 20;
$img = ImageCreate ($imgWidth, $imgHeight);
$bgColor = ImageColorAllocate ($img, 255, 255, 255);
$txtColor= ImageColorAllocate ($img, 0, 0, 0);
ImageFilledRectangle ($img, 0, 0, $imgHeight, $imgHeight, $bgColor);
ImageString ($img, 4, 3, 3, $Number, $txtColor);
Header("Content-Type: image/jpeg");
ImageJpeg($img);
ImageDestroy($img);
?>

Before i used "ob_flush" the error was "headers alredy sent bla..bla..bla"

then i use ob_flush but the image still can't be displayed (only display strange characters).

Any idea how to fix this? Thanks friend..

    well, this is what I use, you should be able to modify it to suit your needs:

    $text = $_GET['text'];

    $height = 30; //h
    $width = 300; //w
    $im = ImageCreate($width, $height); //make it
    $txt = ImageColorAllocate ($im, 0, 0, 0); //black
    $bg = ImageColorAllocate ($im, 203, 203, 203); //greyish to match website background color
    ImageFill($im, 0, 0, $bg); //fill bg with the greyish
    ImageString($im, 5, 5, 6, "$text", $txt); //write the text
    header ('Content-type: image/png'); //mime type
    ImagePng ($im); //send it to the browser
    ImageDestroy($im); //DIEEEEEEE

    and, you shouldent include it, use the <img> tag or else it will display the text, like you mentioned.

      Thanks for your help. but i have to separate between web Template and script engine.

      So... in my template (login.php), i have to include the engine (showImg.php). Any other idea?

        What you're trying won't work because, if you're wanting to send a JPEG image to the browser, then that's what you should send - and JPEG images don't start with "<html>"

        So lose all the HTML and just send the JPEG.

        Then, as it so happens, you won't need any output buffering anyway, because you'll already be sending all your headers before you send any content.

          dude, use the <img> tag instead of an include!
          <html>
          <body>
          <img src="showimg.php" />
          </body>
          </html>

            yes, it is work now using <img> ... thanks to you all 🙂

              Write a Reply...