Jump to content


Photo

Trouble creating image


  • Please log in to reply
11 replies to this topic

#1 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 26 October 2006 - 01:27 AM

I'm trying to set up a captcha for my email contact page.

GD IS enabled on my server.  The following was copy/pasted from PHPInfo().

gd
GD Support enabled
GD Version bundled (2.0.28 compatible)
FreeType Support enabled
FreeType Linkage with freetype
FreeType Version 2.1.4
GIF Read Support enabled
GIF Create Support enabled
JPG Support enabled
PNG Support enabled
WBMP Support enabled
XPM Support enabled
XBM Support enabled


The following is from my script file:
<?php
//captcha.php
$RandomStr = md5(microtime());// md5 to generate the random string

$ResultStr = substr($RandomStr,0,5);//trim 5 digit 

$NewImage =imagecreatefromjpeg("/home2/donirela/includes/captcha/captcha.gif");//image create by existing image and as back ground 

$LineColor = imagecolorallocate($NewImage,233,239,239);//line color 
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white

imageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image 
imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image 

imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally 

$captcha_key = $ResultStr;// carry the data through session

header("Content-type: image/gif");// out out the image 

imagejpeg($NewImage);//Output image to browser 
?>

I even went so far as to place an 'echo' statement in the captcha php file (the first line after "<?php".  But the echo statement never happens and I don't get ANY error messages.

This is my attempt to show the image:
<img src="captcha.php">

Don

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 26 October 2006 - 07:16 PM

The only problem I can find with your image code is the last 2 lines where you send an image/gif header and then output a jpeg image.

Also, after output, you need to call "imagedestroy($NewImage)" to free the memory.

BUT, you need to store the random code somewhere so you can test against the stored value when the user replies. Maybe in a db table with username or session id as the key, or as a session variable.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 27 October 2006 - 12:43 AM

The only problem I can find with your image code is the last 2 lines where you send an image/gif header and then output a jpeg image.

Yes--I DID miss that part.  Thanks for catching it!  I'm starting with a script that I found online--the author had written it for use with a jpg.  But I want to use a GIF.  I changed two of the lines but I missed the third.

Also, after output, you need to call "imagedestroy($NewImage)" to free the memory.

I'll add that.

BUT, you need to store the random code somewhere so you can test against the stored value when the user replies. Maybe in a db table with username or session id as the key, or as a session variable.

The form page that uses the script stores the key.but what was befuddling me was that put an echo statement as the FIRST line in the captch file.

<?php
echo "WHY won't this work!!!?";  //But this never appeared in my browser so I thought the script wasn't even running.  I even checked the pages's source code and it wasn't there.
//I removed the echo statement before posting because I was afraid it would just confuse matters.
//captcha.php
$RandomStr = md5(microtime());// md5 to generate the random string
continue the code as shown previously..................


I'll work with your suggestions.

Thanks.
Don

#4 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 27 October 2006 - 01:06 AM

Well, I just confirmed that for SOME reason the image script ISN'T EVEN RUNNING.

The "main page" (the one that contains img tag):
<td>
<?php $ireland = "Double Grrrr";?>
      <img src="captcha.php">
      <input type="hidden" name="captcha_key" value="<? if(isset($ireland)){echo $ireland;}else{echo "gottaGetItWorking";} ?>">
    </td>

captcha.php:
<?php
$ireland = "Grrrr";
$RandomStr = md5(microtime());// md5 to generate the random string

$ResultStr = substr($RandomStr,0,5);//trim 5 digit 

$NewImage =imagecreatefromjpeg("/home2/donirela/includes/captcha/captcha.gif");//image create by existing image and as back ground 

$LineColor = imagecolorallocate($NewImage,233,239,239);//line color 
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white

imageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image 
imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image 

imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally 

$captcha_key = $ResultStr;// carry the data through session

header("Content-type: image/gif");// out out the image 

imagegif($NewImage);//Output image to browser 

imagedestroy($NewImage); //destroy the resource
?>

The results of a "view source" in firefox:

    <td>
      <img src="captcha.php">
      <input type="hidden" name="captcha_key" value="Double Grrrr">
    </td>


Don

#5 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 27 October 2006 - 03:08 AM

UPDATE:  I've tried this in both FIREFOX AND IE6.  Neither one will allow the script to even run.  If I change it include the script file, then it DOES attempt to run it (the test variable DOES get set by the script) but of course it doesn't know what to do when told to output the image.


Don

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 27 October 2006 - 06:39 AM

Comment out the header() line and run it on its own. See if you get any errors
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 28 October 2006 - 03:04 AM

Well I eventually got it to show the image--still not too sure why it wasn't.  I don't think I changed anything of signifigance.

So now the only issue left is storing the random string.

The following are snippets from the main page (one with all the HTML code that displays the form).

<?php
	session_start();
	$_SESSION['testing'] = "This string was added in contact3.html";      //  <--this test variable will be important later.
?>



    <td>
	<img src="captchascript.php">
	<input type="hidden" name="captcha_key" value="<? if(isset($_SESSION['captcha_key'])){echo $_SESSION['captcha_key'];}?>"> 
    </td>


<?php 
if(isset($_SESSION['captcha_key'])){echo $_SESSION['testing'];}    //  <-- this is the varible that was set at the top of the page
?>   

This is captchascript.php that's called by the img tag:
<?php
$_SESSION['testing'] = "This string was added in captcha.php";    //  <--here I attempt to reset the testing variable
$RandomStr = md5(microtime());// md5 to generate the random string

$ResultStr = substr($RandomStr,0,5);//trim 5 digit 

$NewImage =imagecreatefromjpeg("/home2/donirela/includes/captcha/captcha.jpg");//image create by existing image and as back ground 

$LineColor = imagecolorallocate($NewImage,233,239,239);//line color 
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white

//imageline($NewImage,1,1,1,1,$LineColor);//create line 1 on image 
//imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image 

imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally 

$_SESSION['captcha_key'] = $ResultStr;// carry the data through session

header("Content-type: image/jpeg");// out out the image 

imagejpeg($NewImage);//Output image to browser 

imagedestroy($NewImage); //destroy the resource
?>

When the testing variable is echo'd to the browser, what shows up is "This string was added in contact3.html".  I added this test variable because when I tried to access the random string from outside the script file, it didn't seem to work.

Thanks for all the help thus far!  I greatly appreciate it!
Don

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 28 October 2006 - 07:36 AM

::captcha.php::
<?php
session_start();

$RandomStr = md5(microtime());// md5 to generate the random string

$ResultStr = substr($RandomStr,0,5);//trim 5 digit 

$_SESSION['captcha_key'] = $ResultStr;// carry the data through session

$NewImage =imagecreatefromgif("/home2/donirela/includes/captcha/captcha.gif");//image create by existing image and as back ground 

$LineColor = imagecolorallocate($NewImage,233,239,239);//line color 
$TextColor = imagecolorallocate($NewImage, 255, 255, 255);//text color-white

imageline($NewImage,1,1,40,40,$LineColor);//create line 1 on image 
imageline($NewImage,1,100,60,0,$LineColor);//create line 2 on image 

imagestring($NewImage, 5, 20, 10, $ResultStr, $TextColor);// Draw a random string horizontally 

header("Content-type: image/gif");// out out the image 

imagegif($NewImage);//Output image to browser 

imagedestroy($NewImage); //destroy the resource
?>

::testCaptcha.php::
<?php
session_start();
echo '<pre>', print_r($_SESSION, true), '</pre>';    //debug code
echo '<pre>', print_r($_POST, true), '</pre>';       //debug code 

if ($_SESSION['captcha_key']==$_POST['key']) {
    echo "<h3>Valid</h3>";
}
else {
    echo "<h3>Invalid</h3>";
}  
?>
<form method='post'>
      Name <input type="text" name="name" size="15"><br/>
      <img src='captcha.php'><br/>
      Enter Key from Image <input type="text" name="key" size="5" maxlength="5"><br/>
      <input type="submit" name="action" value="Submit">
</form>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 28 October 2006 - 02:04 PM

I tried your code this is the results and it worked.  Now I need to integrate it into my code.  Strange.

Thanks for the assistance.
Don

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 28 October 2006 - 02:11 PM

Iget

[pre]
Session Array

Array
(
    [captcha_key] => 4e9c1
)

Post Array

Array
(
    [name] => barand
    [key] => 4e9c1
    [action] => Submit
)

Valid
[/pre]

???
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 doni49

doni49
  • Members
  • PipPipPip
  • Advanced Member
  • 515 posts
  • LocationCentral MO

Posted 28 October 2006 - 02:15 PM

Barrand:  Yes I posted that last message (telling you of my errors with your code--most people won't know what I'm talking about here) then I noticed one issue and fixed it.  When I tested it, it worked.

So I modfied my message as shown above.  You must've tested it after i made the correction.
Don

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,020 posts

Posted 28 October 2006 - 02:23 PM

Glad all's well
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users