By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,971 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,971 IT Pros & Developers. It's quick & easy.

Cannot generate random image for form security - help

P: n/a
[PHP]
/**
* Generate the random security image
*
* @access public
* @param $willUseFilePath (default false) boolean to determine if you
will be using a file path
* @param mixed $filePath (optional) file path to store image resource
object contents
* @see actual_path
*/
function generate($willUseFilePath = false, $filePath = '') {

$type = $this->getImageType();
$length = $this->getLength();
if ($willUseFilePath && $filePath) $this->setFilePath($filePath);

$text = $this->generateText();
$fileName = $this->getFileName();
if (!$fileName) {
$fileName = $this->generateFileName();
$this->setFileName($fileName);
}

// create an temporary image from an image file
if (is_file(actual_path("$filePath/$fileName"))) $msg = eval('$image
= imagecreatefrom' . $type . '("' . $filePath . '/' . $fileName .
'");');
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetruecolor(150, 30); else $im = imagecreate(100, 50) or
die("Cannot Initialize new GD image stream");

// get a color from the image (in this case, yellow)
$orange = imagecolorallocate($image, 220, 210, 60);

// Now we need to get widh and height of the image, so that we can
center the key on the image, so that it does not go outside of the
borders or look strange
$width = (imagesx($image) - 7.5 * strlen($text)) / 2;
$height = (imagesy($image) - 7.5) / 2;

// here we write the key (the string) on the image
imagestring($image, 3, $width, $height, $text, $orange);

// now create the final image
$msg = eval('image' . $type . '($image);');
// to free up results, we need to destroy the temporary image.
imagedestroy($image);

if (is_file(actual_path("$filePath/$fileName")) || $msg) {
$this->setSession($fileName, $text);
} else {
$this->dbAP->isSuccessful = false;
$this->dbAP->setErrorArray(array('action' => 'Unable to generate
random image for form security: ' . nl2br($msg)));
}
}

[/PHP]

This class method is supposed to generate one of those random images
you see on forms, you know, with the letters and numbers all mixed up
for people to type it in for security and repost protection purposes.

This method, however, fails. No errors, no warnings, no notices, not
even a broken image, it just DIES on the spot, completely, stops dead
on the line:

[PHP]
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetruecolor(150, 30); else $im = imagecreate(100, 50) or
die("Cannot Initialize new GD image stream");
[/PHP]

I've no clue what to do.

Thanx
Phil

Apr 3 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
I'd just use javascript....
in the head section:
<script>
var s = new Array("imgsrc1","imgsrc2","etc.")
document.getElementById("RandomImage").src =
Math.round(Math.random()*s.length)
</script>

in body section:
<img id="RandomImage" src="" />

Apr 4 '06 #2

P: n/a
comp.lang.php said the following on 03/04/2006 23:00:
<...SNIP CODE...>

This class method is supposed to generate one of those random images
you see on forms, you know, with the letters and numbers all mixed up
for people to type it in for security and repost protection purposes.

This method, however, fails. No errors, no warnings, no notices, not
even a broken image, it just DIES on the spot, completely, stops dead
on the line:

[PHP]
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetruecolor(150, 30); else $im = imagecreate(100, 50) or
die("Cannot Initialize new GD image stream");
[/PHP]


I'm not going to attempt to debug your code, but firstly, do you
definitely have error reporting level set to E_ALL? Secondly, you're
using eval() on the line before this, so there's a good chance that this
could cock things up. Incidentally, why are you using eval() at all?

--
Oli
Apr 4 '06 #3

P: n/a
mouseit101 said the following on 04/04/2006 01:43:
I'd just use javascript....
in the head section:
<script>
var s = new Array("imgsrc1","imgsrc2","etc.")
document.getElementById("RandomImage").src =
Math.round(Math.random()*s.length)
</script>

in body section:
<img id="RandomImage" src="" />


What? So rather than use a server-side randomly-generated image, you'd
use a client-side selection from a pre-defined list of images? In
JavaScript?
--
Oli
Apr 4 '06 #4

P: n/a
See below, thanx

Oli Filth wrote:
comp.lang.php said the following on 03/04/2006 23:00:
<...SNIP CODE...>

This class method is supposed to generate one of those random images
you see on forms, you know, with the letters and numbers all mixed up
for people to type it in for security and repost protection purposes.

This method, however, fails. No errors, no warnings, no notices, not
even a broken image, it just DIES on the spot, completely, stops dead
on the line:

[PHP]
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetruecolor(150, 30); else $im = imagecreate(100, 50) or
die("Cannot Initialize new GD image stream");
[/PHP]

I'm not going to attempt to debug your code, but firstly, do you
definitely have error reporting level set to E_ALL? Secondly, you're
using eval() on the line before this, so there's a good chance that this
could cock things up. Incidentally, why are you using eval() at all?


I debug by setting error_reporting() to E_ALL, however, that does not
show anything, not even notices that could be spawned.

Secondly, the reason I'm using eval() is very simple: I will be
generating a random image that could be a PNG, a GIF, a JPEG, a TIFF, a
BMP, etc., depending on the immediate need, thus, one command like
imagepng() or imagejpeg() is insufficient.

Phil

--
Oli


Apr 4 '06 #5

P: n/a

mouseit101 wrote:
I'd just use javascript....
in the head section:
<script>
var s = new Array("imgsrc1","imgsrc2","etc.")
document.getElementById("RandomImage").src =
Math.round(Math.random()*s.length)
</script>

in body section:
<img id="RandomImage" src="" />


And when javascript is turned off? Not to mention what <noscript>
option could possibly match that considering it's a federal mandate for
government sites to follow Section 508 compilance..

Phil

Apr 4 '06 #6

P: n/a

comp.lang.php wrote:
See below, thanx

Oli Filth wrote:
comp.lang.php said the following on 03/04/2006 23:00:
<...SNIP CODE...>

This class method is supposed to generate one of those random images
you see on forms, you know, with the letters and numbers all mixed up
for people to type it in for security and repost protection purposes.

This method, however, fails. No errors, no warnings, no notices, not
even a broken image, it just DIES on the spot, completely, stops dead
on the line:

[PHP]
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetruecolor(150, 30); else $im = imagecreate(100, 50) or
die("Cannot Initialize new GD image stream");
[/PHP]

I'm not going to attempt to debug your code, but firstly, do you
definitely have error reporting level set to E_ALL? Secondly, you're
using eval() on the line before this, so there's a good chance that this
could cock things up. Incidentally, why are you using eval() at all?


I debug by setting error_reporting() to E_ALL, however, that does not
show anything, not even notices that could be spawned.

Secondly, the reason I'm using eval() is very simple: I will be
generating a random image that could be a PNG, a GIF, a JPEG, a TIFF, a
BMP, etc., depending on the immediate need, thus, one command like
imagepng() or imagejpeg() is insufficient.


** UPDATE **
Turns out that the Linux package "php-gd" was never installed. An
update with that and httpd restart fixed the lack-of-GD problem.

However, there is another problem: I am trying to create a brand new
image file. How do you do that using GD? Each time I try the image
file does not exist.

/**
* Generate the random security image
*
* @access public
* @param $willUseFilePath (default false) boolean to determine if you
will be using a file path
* @param mixed $filePath (optional) file path to store image resource
object contents
* @see actual_path
*/
function &generate($willUseFilePath = false, $filePath = '') { //
STATIC VOID METHOD

if ($this->hasGD() && $this->dbAP->isSuccessful) {

$type = $this->getImageType();
$length = $this->getLength();
if ($willUseFilePath && $filePath) $this->setFilePath($filePath);

$text = $this->generateText();
$fileName = $this->getFileName();
if (!$fileName) {
$fileName = $this->generateFileName();
$this->setFileName($fileName);
}

// create an temporary image from an image file
if (is_file(actual_path("$filePath/$fileName"))) $msg = eval('$image
= imagecreatefrom' . $type . '(actual_path("$filePath/$fileName"));');
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetruecolor(150, 30); else $image = imagecreate(100, 50);
if (!$image) {
$this->dbAP->isSuccessful = false;
$this->dbAP->setErrorArray(array('action' => 'Unable to generate
random image for form security'));
}

// get a color from the image (in this case, yellow)
$orange = imagecolorallocate($image, 220, 210, 60);

// Now we need to get widh and height of the image, so that we can
center the key on the image, so that it does not go outside of the
borders or look strange
$width = (imagesx($image) - 7.5 * strlen($text)) / 2;
$height = (imagesy($image) - 7.5) / 2;

// here we write the key (the string) on the image
imagestring($image, 3, $width, $height, $text, $orange);

// now create the final image [ADDITIONAL: ADD TO FILE IF FILE
NAME INDICATED]
if ($willUseFilePath && $filePath) {
$msg = eval('image' . $type . '($image, "$filePath/$fileName");');
} else {
$msg = eval('image' . $type . '($image);');
}

// to free up results, we need to destroy the temporary image.
imagedestroy($image);

if (is_file(actual_path("$filePath/$fileName")) || $msg) {
$this->setSession($fileName, $text);
} else {
$this->dbAP->isSuccessful = false;
$this->dbAP->setErrorArray(array('action' => "Unable to generate
random image \"$filePath/$fileName\" for form security: " .
nl2br($msg)));
}

}
}

Phil

Phil

--
Oli


Apr 4 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.