1

I have copied the following code from somewhere in the web. It is stored in a file called captcha.php:

  $md5_hash = md5(rand(0,999)); 
    //We don't need a 32 character long string so we trim it down to 5 
    $security_code = substr($md5_hash, 15, 5); 

    //Set the session to store the security code


    //Set the image width and height
    $width = 100;
    $height = 20; 

    //Create the image resource 
    $image = ImageCreate($width, $height);  

    //We are making three colors, white, black and gray
    $white = ImageColorAllocate($image, 255, 255, 255);
    $black = ImageColorAllocate($image, 0, 0, 0);
    $grey = ImageColorAllocate($image, 204, 204, 204);

    //Make the background black 
    ImageFill($image, 0, 0, $black); 

    //Add randomly generated string in white to the image
    ImageString($image, 3, 30, 3, $security_code, $white); 

    //Throw in some lines to make it a little bit harder for any bots to break 
    ImageRectangle($image,0,0,$width-1,$height-1,$grey); 
    imageline($image, 0, $height/2, $width, $height/2, $grey); 
    imageline($image, $width/2, 0, $width/2, $height, $grey); 

    //Tell the browser what kind of file is come in 
    header("Content-Type: image/jpeg"); 

    //Output the newly created image in jpeg format 
    ImageJpeg($image);

    //Free up resources
    ImageDestroy($image);

I need to use in the laravel. I should give the path of this php file to the src attribute of the <img> tag. When the file is located in a folder in public directory, it works, but when I do the following in a route, it does not work, and no image gets created. I need to incorporate it into the Laravel Routes to use its Session:: capabilities.

Here is the router in which the above code is inserted:

Route::get('captcha', array('as'=>'captcha'), function(){
    // above code 
});

1 Answers1

2

I'm not clear about your problem but you can create a class and put this code inside that class using a public method, for example, you may create a class inside your app/libs folder (create the libs folder inside app) and in this libs folder create a class similar to this:

namespace Libs\Captcha;

class Captcha {

    public function dumpCaptcha()
    {
        // put the captcha code here but make
        // changes to the last part as given below

        //Tell the browser what kind of file is come in
        ob_start();
        header("Content-Type: image/jpeg"); 
        ImageJpeg($image);
        $img = ob_get_clean();

        ImageDestroy($image);
        return base64_encode($img);
    }

}

In the composer.json file's autoload -> classmap section add another entry at the end like this:

"autoload": {
    "classmap": [
        // ...
        "app/tests/TestCase.php",
        "app/libs/captcha/Captcha.php",
    ]
}

Then run composer dump-autoload from the terminal/command prompt and then use this class inside your route like this:

Route::get('captcha', array('as'=>'captcha'), function(){
    $captcha = App::make('Libs\\Captcha\\Captcha');
    return View::make('view_name_here')->with('captchaImage', $captcha->dumpCaptcha());
});

Then in your view you may use something like this

<img src={{ 'data:image/jpeg;base64,' . $captchaImage }} />

The image will be displayed.

The Alpha
  • 143,660
  • 29
  • 287
  • 307