2 Replies - 3703 Views - Last Post: 04 December 2008 - 10:26 AM Rate Topic: -----

#1 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

captcha image not displaying

Post icon  Posted 02 December 2008 - 11:42 PM

Hi
i found the following code in a website. i am trying to use it. but the captcha image is not displaying in any browser.
the following is the code i am trying.
 <?php
	// Start the session
	 session_start();
	 
	 // Generate randomness from MD5
	 $rand = mt_rand();
	 //echo $rand."<br>";
		  
	 // String for randomness
	 $string = substr($rand, 0, 10);
	 //echo $string;
	 
	 // Set the session
	 $_SESSION['captcha'] = $string;
	 
	 /*
		 @ NOW WE CAN MAKE THE IMAGE!!
	 */
	 
	 $img = imagecreatefrompng("captcha.png");
	 
	 // Colours
	 $linecolour = imagecolorallocate($img, 250, 250, 250);
	 $textcolour = imagecolorallocate($img, 0, 0, 0);
	 
	 // Other variables
	 $lines = 10;
	 $i = 1; # Don't change
	 $xcentre = (150 / 2) - ((strlen($string) / 2) * imagefontwidth(5)); # Centre the text X
	 $ycentre = (25 - imagefontheight(5)) / 2; # Centre the text Y
	 
	 // Repeat the lines
	 while($i <= $lines){
		 $randxs = rand(0, 150); # Random X start point
		 $randys = rand(0, 0); # Random Y start point
		 $randxe = rand(0, 150); # Random X end point
		 $randye = rand(0, 50); # Random Y end point
		 
		 imageline($img, $randxs, $randys, $randxe, $randye, $linecolour);
		 $i += 1;
	 }
	 
	 // Echo text
	 imagestring($img, 5, $xcentre, $ycentre, $string, $textcolour);
	 
	 // Send headers (save time laters)
	 header("Content-Type: image/png");
	 imagepng($img); # Echo image out
	 imagedestroy($img); # Destroy image
 ?>



the image captcha.png was in the current working directory.
but i am getting the following error in mozilla
The image “http://localhost/temp/test.php” cannot be displayed, because it contains errors.


i don't know what is causing this error. in other browsers no error msg is being displayed. but they are showing broken image link symbol

can anyone help me pls.. i got this code from x10hosting.com forums site.

This post has been edited by kummu4help: 02 December 2008 - 11:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: captcha image not displaying

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: captcha image not displaying

Posted 03 December 2008 - 01:10 AM

The problem may lie in the fact that you're trying to create an image from the following:
$img = imagecreatefrompng("captcha.png");
Which may not exist and thus produces errors.
Was This Post Helpful? 0
  • +
  • -

#3 dimjaxor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 10-September 08

Re: captcha image not displaying

Posted 04 December 2008 - 10:26 AM

I agree with grimpirate I think your issue is that there is no captcha.png file in your directory. However you don't need to use that function, you can use imagecreatetruecolor(width, height)

I changed the code around for you and I think it is how you might want it to look. It ends up looking like this with the numbers and lines being random every time it loads.

Posted Image

You can change the width/height/colors, all that good stuff. Also, I'm not sure about this but, when I deal with sessions, if I have a blank line or space before <?php or after ?> I get errors. So in my mind it's good practice to make sure there's no blank lines :)


<?php
	// Check if a session is already started, if not start one
	
	if(!session_start()){
	
		session_start();
	}
	 
	 // Generate randomness from MD5
	 $rand = mt_rand();
	 
		 
	 // String for randomness
	 $string = substr($rand, 0, 10);
	 
	 
	 // Set the session
	 $_SESSION['captcha'] = $string;
	 
	 /*
		 @ NOW WE CAN MAKE THE IMAGE!!
	 */
	 
	  //  Create the image - Width = 120px, Height = 25px  //
	 $img = imagecreatetruecolor(120,25);
	 
	 // Set my colors variables //
	 $white = imagecolorallocate($img, 255, 255, 255);
	 $black = imagecolorallocate($img, 0, 0, 0);
	 $green = imagecolorallocate($img, 0, 225, 40);
	 $red   = imagecolorallocate($img, 255, 0, 0);
	 
	 // fill the image from the coordinates 2, 2 - 117, 22 (to create a 2px border around box) with the color white
	 // if you don't, the image background is black by default
	 imagefilledrectangle($img, 2, 2, 117, 22, $white);

	 // Other variables
	 $lines = 10;
	 $i = 1; # Don't change
	 $xcentre = (120 / 2) - ((strlen($string) / 2) * imagefontwidth(5)); # Centre the text X
	 $ycentre = (25 - imagefontheight(5)) / 2; # Centre the text Y
	 
	 // Repeat the lines
	 while($i <= $lines){
		 $randxs = rand(5, 117); # Random X start point
		 $randys = rand(5, 2); # Random Y start point
		 $randxe = rand(5, 117); # Random X end point
		 $randye = rand(0, 22); # Random Y end point
		 
		 imageline($img, $randxs, $randys, $randxe, $randye, $green);
		 $i += 1;
	 }
	 
	 // Echo text
	 imagestring($img, 11, $xcentre, $ycentre, $string, $red);
	 
	 // Send headers (save time laters)
	 header("Content-Type: image/png");
	 imagepng($img); # Echo image out
	 imagedestroy($img); # Destroy image
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1