Wednesday, 14 May 2014

Php: Generate Captcha Secure Image

global $image;
    $_SESSION['count'] = time();
   
    $image              = imagecreatetruecolor(200, 50) or die("Cannot Initialize new GD image stream");
    $background_color   = imagecolorallocate($image, 255, 255, 255);
    $text_color         = imagecolorallocate($image, 0, 255, 255);
    $line_color         = imagecolorallocate($image, 64, 64, 64);
    $pixel_color        = imagecolorallocate($image, 0, 0, 255);

    imagefilledrectangle($image, 0, 0, 200, 50, $background_color);

    for ($i = 0; $i < 3; $i++) {
        imageline($image, 0, rand() % 50, 200, rand() % 50, $line_color);
    }

    for ($i = 0; $i < 1000; $i++) {
        imagesetpixel($image, rand() % 200, rand() % 50, $pixel_color);
    }


    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $len = strlen($letters);
    $letter = $letters[rand(0, $len - 1)];

    $text_color = imagecolorallocate($image, 0, 0, 0);
    $word = "";
    for ($i = 0; $i < 6; $i++) {
        $letter = $letters[rand(0, $len - 1)];
        imagestring($image, 7, 5 + ($i * 30), 20, $letter, $text_color);
        $word .= $letter;
    }
    $_SESSION['captcha_string'] = $word;

    $images = glob("*.png");
    foreach ($images as $image_to_delete) {
        @unlink($image_to_delete);
    }
   
    imagepng($image, "image" . $_SESSION['count'] . ".png");


To display The captcha

<img src="../image<?php echo $_SESSION['count'] ?>.png">



Tuesday, 6 May 2014

Cakephp: Insert Query In Loop

If You are Using $this->Model->save() in  LOOP then this will not work.
TO overcome this issue simply add

Add this before save this will move the cursor to a new Line and then insert again.

$this->Model->create()
$this->Model->save()

Monday, 5 May 2014