Jump to content

Trouble creating image


doni49

Recommended Posts

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().

[quote]
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
[/quote]

The following is from my script file:
[code]
<?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
?>
[/code]

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

This is my attempt to show the image:
[code]
<img src="captcha.php">
[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote author=Barand link=topic=112736.msg458212#msg458212 date=1161890169]
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.
[/quote]
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.

[quote author=Barand link=topic=112736.msg458212#msg458212 date=1161890169]
Also, after output, you need to call "imagedestroy($NewImage)" to free the memory.
[/quote]
I'll add that.

[quote author=Barand link=topic=112736.msg458212#msg458212 date=1161890169]
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.
[/quote]
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.

[code]
<?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..................

[/code]

I'll work with your suggestions.

Thanks.
Link to comment
Share on other sites

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

The "main page" (the one that contains img tag):
[code]
<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>
[/code]

captcha.php:
[code]
<?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
?>
[/code]

The results of a "view source" in firefox:
[quote]
    <td>
      <img src="captcha.php">
      <input type="hidden" name="captcha_key" value="Double Grrrr">
    </td>
[/quote]
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

[code]
<?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
?> 
[/code]

This is captchascript.php that's called by the img tag:
[code]
<?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
?>
[/code]

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!
Link to comment
Share on other sites

::captcha.php::
[code]
<?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
?>
[/code]

::testCaptcha.php::
[code]
<?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>
[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.