473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Cannot generate random image for form security - help

[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
6 3048
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
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
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
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Yannick Turgeon | last post by:
Hello all, Back-end: SS2000 SP2 + NT4 Front-end: A97 + XP We are currently trying to add a Domain Server to our network. We added our SQL Server to the domain and added a client too....
5
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets...
2
by: Jim in Arizona | last post by:
I was trying to create a random image generator. I'm using visual web dev express with 2.0 framework. On the web form page (mypage.aspx), I have an image control: <asp:Image ID="Image1"...
4
by: Kim | last post by:
Random image downloader for specified newsgroup. Hi I'm writing a small script that will download random images from a specified newsgroup. I've imported yenc into the script but I can't open the...
12
by: skip | last post by:
Is there a module out there that will generate an image with a random text string such as the confirmation images you see on various websites? I'm thinking I'm going to have to add that to the...
0
sashi
by: sashi | last post by:
Generate Random Password In the course of programming you may have cause to generate a password. The following function will generate a password of randomly selected characters up to a maximum of...
3
by: jjmontreal | last post by:
I would like to create a form where by clicking some where in the form it will generate a alphanumeric sequence. I want to generate random PIN numbers preceeded with specific letters. An example...
5
by: ashurack | last post by:
I found a stored procedure online a while back and want to inplement it. The only problem is that it doesn't check to see if the number generated is currently in use in the DB. I know it's really...
24
by: pereges | last post by:
I need to generate two uniform random numbers between 0 and 1 in C ? How to do it ? I looked into rand function where you need to #define RAND_MAX as 1 but will this rand function give me ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.