WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : A damaged image generator (class) for validating text. CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart
Categories : PHP, PHP Classes, Security, GD image library, Security Click here to Update Your Picture
Ioannis Cherouvim
Date : Jun 25th 2005
Grade : 3 of 5 (graded 16 times)
Viewed : 9405
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Ioannis Cherouvim
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php

//***************************************************************
//** title  : An damaged image generator for validating text
//**
//** story  : Sometimes you need to verify that the client posting
//**          or registering in your site is actually a human. By
//**          asking him/her to type in the word s/he sees, you ensure
//**          that the client is human, and not a bot/spider which
//**          is probably trying to harm your system.
//**          Play around with the values when constructing this
//**          object, but also feel free to experiment with the
//**          maths inside the image manipulating loops.
//**          Note that this class is writing a png file on disk,
//**          so you might need to have a png with the same name
//**          already present in that location with write
//**          permissions set.
//**
//** author : Ioannis Cherouvim
//** e-mail : morales@hack.gr
//** date   : 2005-06-25
//*******************************************************

   
class PWImage {

       
//instantiate class
        //$filename : the path+filename of the written png file
        //$string   : the text that you want to have in the image
        //$fact     : a magnification factor. play with values>2
        //$bcol     : the background colour, R, G, B (0-255)
        //$fcol     : the foreground colour, R, G, B (0-255)
       
function PWImage($filename, $string, $fact, $bcol, $fcol) {
           
$font = 5;
           
$cosrate = rand(10,19);
           
$sinrate = rand(10,18);
           
           
$charwidth = imagefontwidth($font);
           
$charheight = imagefontheight($font);
           
$width=(strlen($string)+2)*$charwidth;
           
$height=2*$charheight;
   
           
$im = @imagecreatetruecolor($width, $height) or die("Cannot Initialize new GD image stream");
           
$im2 = imagecreatetruecolor($width*$fact, $height*$fact);
           
$bcol = imagecolorallocate($im, $bcol[0], $bcol[1], $bcol[2]);
           
$fcol = imagecolorallocate($im, $fcol[0], $fcol[1], $fcol[2]);

           
imagefill($im, 0, 0, $bcol);
           
imagefill($im2, 0, 0, $bcol);

           
$dotcol = imagecolorallocate($im, (abs($this->getR($fcol)-$this->getR($bcol)))/2.5,
                                              (
abs($this->getG($fcol)-$this->getG($bcol)))/2.5,
                                              (
abs($this->getB($fcol)-$this->getB($bcol)))/2.5);
   
           
$dotcol2 = imagecolorallocate($im, (abs($this->getR($fcol)-$this->getR($bcol)))/1.5,
                                               (
abs($this->getG($fcol)-$this->getG($bcol)))/1.5,
                                               (
abs($this->getB($fcol)-$this->getB($bcol)))/3.5);
   
           
$linecol = imagecolorallocate($im, (abs($this->getR($fcol)-$this->getR($bcol)))/2.4,
                                               (
abs($this->getG($fcol)-$this->getG($bcol)))/2.1,
                                               (
abs($this->getB($fcol)-$this->getB($bcol)))/2.5);
   
            for(
$i=0; $i<$width; $i=$i+rand(1,5)) {
                for(
$j=0; $j<$height; $j=$j+rand(1,5)) {
                   
imagesetpixel($im, $i, $j, $dotcol);
                }
            }

           
imagestring($im, $font, $charwidth, $charheight/2, $string, $fcol);

            for(
$j=0; $j<$height*$fact; $j=$j+rand(3,6)) {
               
imageline($im2, 0, $j, $width*$fact, $j, $linecol);
            }

            for(
$i=0; $i<$width*$fact; $i=$i+rand(4,9)) {
               
imageline($im2, $i, 0, $i, $height*$fact, $linecol);
            }

            for(
$i=0; $i<$width*$fact; $i++) {
                for(
$j=0; $j<$height*$fact; $j++) {
               
$x = abs(((cos($i/$cosrate)*5+sin($j/$sinrate*2)*2+$i)/$fact))%$width;
                   
$y = abs(((sin($j/$sinrate)*5+cos($i/$cosrate*2)*2+$j)/$fact))%$height;
                   
$col = imagecolorat($im, $x, $y);
                    if (
$col!=$bcol) imagesetpixel($im2, $i, $j, $col);
                }
            }
       
            for(
$j=0; $j<$height*$fact; $j=$j+rand(1,5)) {
                for(
$i=0; $i<$width*$fact; $i=$i+rand(1,5)) {
                   
imagesetpixel($im2, $i, $j, $dotcol2);
                }
            }

           
ob_start();
           
imagepng($im2);
           
$buffer = ob_get_contents();
           
ob_end_clean();
           
$handle = fopen($filename, "w");
           
fwrite($handle, $buffer);
           
fclose($handle);

           
imagedestroy($im);
           
imagedestroy($im2);
        }
       
       
//functions to extract RGB values from combined 24bit color value
       
function getR($col) {return (($col >> 8) >> 8) % 256;}
        function
getG($col) {return ($col >> 8) % 256;}
        function
getB($col) {return $col % 256;}

    }   

   
//**********************************************
    //*************** example use ******************
    //**********************************************
   
$pw = new PWImage("temp1.png", "some pass", 2, array(0, 0, 0), array(255, 128, 200));
   
$pw = new PWImage("temp2.png", "WEBERDEV", 4, array(255, 255, 255), array(0, 0, 0));
   
$pw = new PWImage("temp3.png", "9df2ceA", 3, array(0, 0, 0), array(255, 228, 250));
   
$pw = new PWImage("temp4.png", "abcdef_g", 1.8, array(0, 0, 0), array(0, 255, 255));

    echo
"<img src='temp1.png'/><br/>";
    echo
"<img src='temp2.png'/><br/>";
    echo
"<img src='temp3.png'/><br/>";
    echo
"<img src='temp4.png'/><br/>";

?>



Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags.
Categories : PHP, PHP Classes, Algorithms, Security
Antispoof - a class to help prevent people hi-jacking and misusing parts of a website
Categories : PHP, PHP Classes, Security
Scramble Eggs - php class to scramble/encode
Categories : PHP, PHP Classes, Security, Encryption
Dynamic Image Authentication System in Signup Pages (CAPTCHA)
Categories : PHP, Security, GD image library
Creates a CAPTCHA image in PHP, which displays 5 numbers stored in a session.
Categories : PHP, GD image library, Form Processing, Security
Generating and Matching Secure and Strong Password Hash
Categories : PHP, PHP Classes, Cryptography, Security
CAPTCHA[Image verification]
Categories : PHP, Security, GD image library, Graphics, Sessions
Generate image with random number (CAPTCHA)
Categories : PHP, GD image library, Graphics, Security
A captcha image allows you to prevent spam posting when users reload the page and stop bots from submitting forms automatically. This version allows you to use your own fonts (.ttf) to show the text.
Categories : PHP, Security, Graphics, GD image library
Use of bitmasks to represent permissions
Categories : PHP, Authentication, Bitwise Operators, Security, PHP Classes
Scan Apache access log files and report possible worms attack
Categories : PHP, PHP Classes, Security, Apache, Log Files
Securing Web Forms with Simple PHP-CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart)
Categories : PHP, Security, GD image library, Sessions
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
MD5 secured login
Categories : PHP, Java Script, Authentication, Security
Simple Password example
Categories : PHP, Authentication, Security, HTTP