943,985 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1629
  • PHP RSS
Jul 10th, 2006
0

Image Generation Problem

Expand Post »
See, I am using the following PHP code to generate a PNG image file

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. // create a 100*30 image
  3. $im = imagecreate(100, 30);
  4.  
  5. // white background and blue text
  6. $bg = imagecolorallocate($im, 255, 255, 255);
  7. $textcolor = imagecolorallocate($im, 0, 0, 255);
  8.  
  9. // write the string at the top left
  10. imagestring($im, 5, 0, 0, "Hello world!", $textcolor);
  11.  
  12. // output the image
  13. header("Content-type: image/png");
  14. imagepng($im);
  15. ?>

The above code works absolutely fine if I am running on the top of my webpage,

But if I try to run anywhere in between my page, then it generates the following error

PHP Syntax (Toggle Plain Text)
  1. The image "http://localhost" cannot be displayed, because it contains erros

I know this is due to because I am passing the header after the HTML codes, but if I try to pass the header before any HTML tag then I get the same error as above.

Any suggestions?

PS: I want to make a graphical counter at the bottom of my page, and this is the reason I need this code to work. Or is there any alternative????


Kindly Help


God Bless
Similar Threads
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004
Jul 11th, 2006
0

Re: Image Generation Problem

header stuff has to come out before anything else is printed to the screen. Try splitting the code in two. Put the header at the top and then move your image code to where you need it.
Reputation Points: 15
Solved Threads: 0
Junior Poster in Training
barnamos is offline Offline
50 posts
since Mar 2005
Jul 11th, 2006
0

Re: Image Generation Problem

Just make a html image tag and call your image counter script from that tag!

// add to your html output page

[PHP]<img src='/my_counter.php?page=index' height='24' width='100' alt='' />[/PHP]

example counter file content

file name = index

PHP Syntax (Toggle Plain Text)
  1. 0000000000


// my_counter.php

[PHP]<script language='php'>

/* path to counter files */

$path = './counters/';

/* default count, invalid page or no page or counter file doesn't exist */

$count = 0;

if ( ! empty ( $_GET['page'] ) )
{
$p = trim ( $_GET['page'] );

if ( preg_match ( "/^[a-z]{4,10}$/", $p ) && file_exists ( $path . $p ) )
{
$io = fopen ( $path . $p, 'r+' );
$count = fgets ( $io, 11 );
fseek ( $io, 0 );
fputs ( $io, sprintf ( '%010d', ++$count ) );
fclose ( $io );
}
}

if ( $count == 0 )
{
++$count;
}

$im = imagecreate ( 100, 24 );

$bg = imagecolorallocate ( $im, 255, 255, 255 );

$tc = imagecolorallocate ( $im, 0, 0, 255 );

imagestring ( $im, 5, 6, 4, sprintf ( '%010d', $count), $tc );

header ( 'Content-Type: image/png' );

imagepng ( $im );

imagedestroy ( $im );

</script>[/PHP]


demo!
Last edited by demo; Jul 11th, 2006 at 11:56 am.
Reputation Points: 10
Solved Threads: 2
Newbie Poster
demo is offline Offline
18 posts
since Jan 2005
Jul 11th, 2006
0

Re: Image Generation Problem

Thanx


Problem solved, Plz close this topic
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004
Jul 13th, 2006
0

Re: Image Generation Problem

cancer10, you can mark the topic as solved yourself. Up on the menu navigation there's an option for the topic starter to mark the topic as solved.
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP menu link causes error
Next Thread in PHP Forum Timeline: If Or Switch





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC