| | |
Image Generation Problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
See, I am using the following PHP code to generate a PNG image file
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
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
PHP Syntax (Toggle Plain Text)
<?php // create a 100*30 image $im = imagecreate(100, 30); // white background and blue text $bg = imagecolorallocate($im, 255, 255, 255); $textcolor = imagecolorallocate($im, 0, 0, 255); // write the string at the top left imagestring($im, 5, 0, 0, "Hello world!", $textcolor); // output the image header("Content-type: image/png"); imagepng($im); ?>
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)
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
•
•
Join Date: Jan 2005
Posts: 18
Reputation:
Solved Threads: 2
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
// 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!
// 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)
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.
•
•
Join Date: Jul 2004
Posts: 494
Reputation:
Solved Threads: 21
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.
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.
![]() |
Similar Threads
- Dreamweaver Image Moving Problem (Site Layout and Usability)
- image retrieval problem (PHP)
- Image swap behaviour from Dreamweaver no longer works once uploaded (Site Layout and Usability)
- Flash - loaded swf image quality problems (Graphics and Multimedia)
Other Threads in the PHP Forum
- Previous Thread: PHP menu link causes error
- Next Thread: If Or Switch
| Thread Tools | Search this Thread |
ajax apache api array beginner beneath binary broadband broken button cakephp checkbox class cms code countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla limit link login loop mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion regex remote script search server session sessions sms smtp soap source space sql strip_tags survey syntax system table tutorial undefined update upload url validator variable video virus votedown web window.onbeforeunload=closeme; xml youtube





