473,581 Members | 2,497 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot generate random image for form security - help

[PHP]
/**
* Generate the random security image
*
* @access public
* @param $willUseFilePat h (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($willU seFilePath = false, $filePath = '') {

$type = $this->getImageType() ;
$length = $this->getLength();
if ($willUseFilePa th && $filePath) $this->setFilePath($f ilePath);

$text = $this->generateText() ;
$fileName = $this->getFileName( );
if (!$fileName) {
$fileName = $this->generateFileNa me();
$this->setFileName($f ileName);
}

// create an temporary image from an image file
if (is_file(actual _path("$filePat h/$fileName"))) $msg = eval('$image
= imagecreatefrom ' . $type . '("' . $filePath . '/' . $fileName .
'");');
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetrue color(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 = imagecoloralloc ate($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($im age, 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($i mage);

if (is_file(actual _path("$filePat h/$fileName")) || $msg) {
$this->setSession($fi leName, $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 =
imagecreatetrue color(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 3065
I'd just use javascript....
in the head section:
<script>
var s = new Array("imgsrc1" ,"imgsrc2","etc .")
document.getEle mentById("Rando mImage").src =
Math.round(Math .random()*s.len gth)
</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 =
imagecreatetrue color(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.getEle mentById("Rando mImage").src =
Math.round(Math .random()*s.len gth)
</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 =
imagecreatetrue color(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.getEle mentById("Rando mImage").src =
Math.round(Math .random()*s.len gth)
</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 =
imagecreatetrue color(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 $willUseFilePat h (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($will UseFilePath = false, $filePath = '') { //
STATIC VOID METHOD

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

$type = $this->getImageType() ;
$length = $this->getLength();
if ($willUseFilePa th && $filePath) $this->setFilePath($f ilePath);

$text = $this->generateText() ;
$fileName = $this->getFileName( );
if (!$fileName) {
$fileName = $this->generateFileNa me();
$this->setFileName($f ileName);
}

// create an temporary image from an image file
if (is_file(actual _path("$filePat h/$fileName"))) $msg = eval('$image
= imagecreatefrom ' . $type . '(actual_path(" $filePath/$fileName"));') ;
if (!$image && strcmp($type, 'gif') != 0) $image =
imagecreatetrue color(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 = imagecoloralloc ate($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($im age, 3, $width, $height, $text, $orange);

// now create the final image [ADDITIONAL: ADD TO FILE IF FILE
NAME INDICATED]
if ($willUseFilePa th && $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($i mage);

if (is_file(actual _path("$filePat h/$fileName")) || $msg) {
$this->setSession($fi leName, $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
9539
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. Everything is working fine... for maybe 20 or 30 minutes. Then the client generates this error: -----------------
5
2184
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 details of a random image from an access database. I know this works because when I run the query in access, it picks a different image each time...
2
2885
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" runat="server" /> On the codebehind, (mypage.aspx.vb), I have this code when a button is clicked:
4
3071
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 image or save it. This is my first script so be gentle! Heres the script ####random group downloader#### import nntplib import string, random...
12
2786
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 forms on the Mojam websites. Over the past couple weeks we've begun to get lots of spam submission crap. Thx, Skip
0
8651
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 10 characters (this is easy to increase). The number of characters required is passed as a parameter. Using this type of procedure as opposed to...
3
5488
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 would be BW6666 or BW1234. Can anyone help?
5
5967
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 unlikely for the same number to be generated twice (with a 10 digit number) but it's for a shopping cart so I need it to be 100% reliable. I...
24
7196
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 uniformly distributed and unique numbers ?
0
7868
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8149
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8175
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6553
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3827
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1403
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1138
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.