Image cannot be displayed because it contains php errors

Possible duplicate:
Can html use dynamic generated images in php?

I am trying to create captcha in php. I believe that I have the code, but I cannot get the image in the browser. This is the code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php header('Content-type: image/png');?> <?php session_start(); $md5 = md5(microtime() * time() ); $string = substr($md5, -5); $captcha = imagecreatefrompng("./captcha.png"); $black = imagecolorallocate($captcha, 0, 0, 0); $line = imagecolorallocate($captcha,233,239,239); imageline($captcha,0,0,39,29,$line); imageline($captcha,40,0,64,29,$line); $_SESSION['key'] = md5($string); imagestring($captcha, 5, 20, 10, $string, $black); imagepng($captcha);?> </body> </html> 

The png image is in the same folder as this code. GD option is included in php .. I don't know ... any help is appreciated ... thanks

-1
source share
2 answers

You cannot output anything before setting <?php header('Content-type: image/png');?> Or session_start();

You need to create a script image that processes the image and then reference that script in your html

Example: captcha.php

 <?php session_start(); header('Content-type: image/png'); $md5 = md5(microtime() * time() ); $string = substr($md5, -5); $captcha = imagecreatefrompng("./captcha.png"); $black = imagecolorallocate($captcha, 0, 0, 0); $line = imagecolorallocate($captcha,233,239,239); imageline($captcha,0,0,39,29,$line); imageline($captcha,40,0,64,29,$line); $_SESSION['key'] = md5($string); imagestring($captcha, 5, 20, 10, $string, $black); imagepng($captcha); ?> 

Your html

 <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <img src="captcha.php"/> </body> </html> 
0
source

I assume this is the source of your CAPTCHA image (for example, the file "image.php"), and it is downloaded from another location (for example, from "captcha.php" using <img src="image.php" /> ). You may need to include session_start() in the captcha.php file.

From the code you posted, just remove all the HTML.

Also, as a rule, never send the type of image content until you are ready (and, if necessary, check for errors first.)

 <?php session_start(); $md5 = md5(microtime() * time() ); $string = substr($md5, -5); $captcha = imagecreatefrompng("./captcha.png"); $black = imagecolorallocate($captcha, 0, 0, 0); $line = imagecolorallocate($captcha,233,239,239); imageline($captcha,0,0,39,29,$line); imageline($captcha,40,0,64,29,$line); $_SESSION['key'] = md5($string); imagestring($captcha, 5, 20, 10, $string, $black); Header('Content-type: image/png'); imagepng($captcha); ?> 

NOTE : you are using MD5 string MD5. It's right? Why not use uniqid() instead of md5(microtime() * time() ) and save $md5 in _SESSION ?

0
source

All Articles