Image Generation Problem

Reply

Join Date: Dec 2004
Posts: 234
Reputation: cancer10 is an unknown quantity at this point 
Solved Threads: 0
cancer10's Avatar
cancer10 cancer10 is offline Offline
Posting Whiz in Training

Image Generation Problem

 
0
  #1
Jul 10th, 2006
See, I am using the following PHP code to generate a PNG image file

  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

  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 50
Reputation: barnamos is an unknown quantity at this point 
Solved Threads: 0
barnamos's Avatar
barnamos barnamos is offline Offline
Junior Poster in Training

Re: Image Generation Problem

 
0
  #2
Jul 11th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 18
Reputation: demo is an unknown quantity at this point 
Solved Threads: 2
demo demo is offline Offline
Newbie Poster

Re: Image Generation Problem

 
0
  #3
Jul 11th, 2006
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 234
Reputation: cancer10 is an unknown quantity at this point 
Solved Threads: 0
cancer10's Avatar
cancer10 cancer10 is offline Offline
Posting Whiz in Training

Re: Image Generation Problem

 
0
  #4
Jul 11th, 2006
Thanx


Problem solved, Plz close this topic
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: Image Generation Problem

 
0
  #5
Jul 13th, 2006
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.
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC