Building an Image

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Building an Image

Post 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); 
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're trying to write html into a png stream with your verify script... so yeah..
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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"); ?>
deltawing
Forum Commoner
Posts: 46
Joined: Tue Jun 14, 2005 2:55 pm

Post 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.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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); 

?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try accessing that script directly..

make sure there are no additional characters outside of php's processing..
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

have you checked your error logs?
deltawing
Forum Commoner
Posts: 46
Joined: Tue Jun 14, 2005 2:55 pm

Post by deltawing »

Hmmm... are you sure there are no newlines, spaces, anything like that outside the <? ... ?> tags?
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try using imagejpeg(), or imagecreatetruecolor() or both.
deltawing
Forum Commoner
Posts: 46
Joined: Tue Jun 14, 2005 2:55 pm

Post 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.
deltawing
Forum Commoner
Posts: 46
Joined: Tue Jun 14, 2005 2:55 pm

Post 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.
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Post Reply