Skip to content
April 11, 2008 / ranacse05

Image Validation Script


When ever we try to get register ourselves in any web app we have to pass the image validation.Its for the security.This can be done in 2 ways

  • Using Database
  • Generate the Image Dynamically

If we follow the first one the we have to have a database where we’ll store the image.And we have to generate lots of image manually and stored there.Sounds bad to me 😦 .

I was just working on a new project.There i have to use an image validation.So i wrote a library which will generate the image dynamically,so i dont have to worry about generate a lots of image and store them on database.Here is the code

<?php
function security_image($rand) {
$width = 100;
$height = 25;
$image = imagecreate($width, $height);
$bgColor = imagecolorallocate ($image, 255, 255, 255);
$textColor = imagecolorallocate ($image, 255, 0, 0);

// Add Random noise
for ($i = 0; $i < 10; $i++) {
$rx1 = rand(0,$width);
$rx2 = rand(0,$width);
$ry1 = rand(0,$height);
$ry2 = rand(0,$height);
$rcVal = rand(0,255);
$rc1 = imagecolorallocate($image,
rand(0,255),
rand(0,255),
rand(100,255));

imageline ($image, $rx1, $ry1, $rx2, $ry2, $rc1);
}

imagestring($image, 5, 30, 10, $rand, $textColor);

@header(‘Content-type: image/png’);
imagepng($image,”s.png”);
imagedestroy($image);
}
?>

Now call the function

$new = //your string u wanna show as image
security_image($new);

Then use img tag to view the s.png file where u want. 🙂

Advertisements

4 Comments

Leave a Comment
  1. nuhil / Apr 11 2008 11:39 pm

    Nice work. I’m also trying this with the help of Net. thnx for faster help.

  2. salahuddin66 / Apr 12 2008 4:21 pm

    good work… 🙂

  3. manchumahara / Apr 12 2008 4:23 pm

    ok I will try this. thanks

  4. Saikat / Apr 12 2008 8:08 pm

    cool ….. sounds really gr8 ….good job!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: