Page 1 of 1

Building an Image

Posted: Fri Sep 09, 2005 7:09 pm
by jwalsh
Hi,

I'm experimenting with trying to build a basic captcha type script. Before I get too far into building the actual image, I want to make sure I can get it to display. Right now, I'm not getting anything... maybe you guys can point me in the right direction.

Code: Select all

<img src="verify.php?<?php echo SID ?>" alt="" border="0" />
Here is verify.php...

Code: Select all

<?
require_once("include/layout/header.php");
Header("Content-Type: image/png");
?>
<html><body>
<? Captcha("Test"); ?>
</body></html>
And Finally the Captcha Function...

Code: Select all

function Captcha($message) {
	// Create The Image WxH
	$myimage = ImageCreate(200, 40); 

	// Fill Image Black
	ImageFill($myimage, 0, 0, $black); 
	
	// Write Message
	ImageString($myimage, 4, 96, 19, $message, $white); 
	
	// Output Image
	ImagePNG($myimage); 
}

Posted: Fri Sep 09, 2005 7:11 pm
by feyd
you're trying to write html into a png stream with your verify script... so yeah..

Posted: Fri Sep 09, 2005 7:16 pm
by jwalsh
I originally didn't have that, but I read somewhere that I was supposed to... I removed it with no change.

Code: Select all

<?
require_once("include/layout/header.php");
Header("Content-Type: image/png");

Captcha("Test"); ?>

Posted: Fri Sep 09, 2005 7:20 pm
by deltawing
Leave out the

Code: Select all

require_once("include/layout/header.php");
(I'm assuming that has normal HTML or PHP output in it), and it should work fine.

Posted: Fri Sep 09, 2005 7:31 pm
by jwalsh
Well, Closer I think. I still get no output... So I've simplified. Rather than using a function to build, I just consolidated it into a single file.

Code: Select all

<?
Header("Content-Type: image/png");

// Create The Image WxH
$myimage = ImageCreate(200, 40); 

// Fill Image Black
ImageFill($myimage, 0, 0, $black); 

// Write Message
ImageString($myimage, 4, 96, 19, "Test", $white); 

// Output Image
ImagePNG($myimage); 

?>

Posted: Fri Sep 09, 2005 7:41 pm
by feyd
try accessing that script directly..

make sure there are no additional characters outside of php's processing..

Posted: Fri Sep 09, 2005 7:46 pm
by jwalsh
One step ahead of you... I am accessing the file, and I'm getting nothing. Also, accessing from an img tag on the other page doesn't even give a broken image.

Sorry to be such a pain :( I thought this would be the easy part of the experiment lol.

Posted: Fri Sep 09, 2005 7:50 pm
by feyd
have you checked your error logs?

Posted: Fri Sep 09, 2005 7:52 pm
by deltawing
Hmmm... are you sure there are no newlines, spaces, anything like that outside the <? ... ?> tags?

Posted: Fri Sep 09, 2005 8:01 pm
by jwalsh
HAHAHA Good Call Feyd....

Code: Select all

[Fri Sep  9 19:49:23 2005] [error] PHP Fatal error:  imagepng() [<a href='function.imagepng'>function.imagepng</a>]: gd-png:  fatal libpng error: Invalid number of colors in palette\n in /home/zoneprof/public_html/verify.php on line 13
[Fri Sep  9 19:45:20 2005] [error] PHP Fatal error:  imagepng() [<a href='function.imagepng'>function.imagepng</a>]: gd-png:  fatal libpng error: Invalid number of colors in palette\n in /home/zoneprof/public_html/verify.php on line 13
[Fri Sep  9 19:44:17 2005] [error] PHP Fatal error:  imagepng() [<a href='function.imagepng'>function.imagepng</a>]: gd-png:  fatal libpng error: Invalid number of colors in palette\n in /home/zoneprof/public_html/verify.php on line 15
[Fri Sep  9 19:43:54 2005] [error] PHP Fatal error:  imagepng() [<a href='function.imagepng'>function.imagepng</a>]: gd-png:  fatal libpng error: Invalid number of colors in palette\n in /home/zoneprof/public_html/verify.php on line 15
[Fri Sep  9 19:39:51 2005] [error] PHP Fatal error:  imagepng() [<a href='function.imagepng'>function.imagepng</a>]: gd-png:  fatal libpng error: Invalid number of colors in palette\n in /home/zoneprof/public_html/verify.php on line 14
code]

About 400 times.

I tried adding...

Code: Select all

ImageTrueColorToPalette( $myimage, false, 256 );
To Move to palette color, but still giving the error.

Posted: Fri Sep 09, 2005 8:03 pm
by feyd
try using imagejpeg(), or imagecreatetruecolor() or both.

Posted: Fri Sep 09, 2005 8:03 pm
by deltawing
Well, I've tried it myself, and after a bit of fiddling, all I can come up with is to try changing the header to gif and the output to ImageGIF. That works for me.

Posted: Fri Sep 09, 2005 8:15 pm
by deltawing
Done it!

For some reason with PNGs, you need to use ImageColorAllocate() to make things change colour. So...

Code: Select all

<?php

Header ("Content-type: image/png");
$myimage = ImageCreate (200, 40);
$bgcolor = ImageColorAllocate ($myimage, 0, 0, 0);
$textcolor = ImageColorAllocate ($myimage, 255, 255, 255);
ImageString ($myimage, 4, 96, 19,  "test", $textcolor);
ImagePng ($myimage);

?>
You should probably look into this on php.net to find out more about it why you had a problem.

Posted: Fri Sep 09, 2005 8:36 pm
by jwalsh
Delta,

Ok, that makes sense. Part of the experiment was using PNG's, and you've solved the problem. Thanks everyone for your patience, and your help.

Josh

Posted: Sat Sep 10, 2005 11:29 am
by onion2k