Welcome Guest, Not a member yet? Register   Sign In
Sending an image generated by imagepng to a view
#1

[eluser]Dandy_andy[/eluser]
I am trying to modify a simple script that generates a captcha so that when it is called for, it generates the image within a portion of the view. I am confused about how to do this using the MVC approach. I have a library file called 'captcha.php' which is a very slightly modified script that I have used before and which I copied from a source on the net:-

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Captcha {

function captcha()
{


$string = '';  
for ($i = 0; $i < 8; $i++) {  
$string .= chr(rand(97, 122));  
}  
  $code['rand_code'] = $string;
    
  $dir = '';
    
  $image = imagecreatetruecolor(170, 60);  
  $black = imagecolorallocate($image, 0, 0, 0);  
  $color = imagecolorallocate($image, 200, 100, 90); // red  
  $white = imagecolorallocate($image, 255, 255, 255);
  $lgrey = imagecolorallocate($image, 238, 238, 238);    

imagefilledrectangle($image,0,0,200,100,$lgrey);  
    imagettftext($image, 30, 5, 10, 40, $color, $dir."font.ttf", $code['rand_code']);
header("Content-type: image/png");  
imagepng($image);
imagedestroy($image);

}

}

?&gt;

Before using this in Codeigniter, the script was run directly (outside of a function) by loading it as a image using the HTML
Code:
<img src="captcha.php" alt="" />
but I am trying to work this within the MVC method. How can I pass the generated PNG image without using a database or generating an image that I have to store and then retrieve from the viewer? At the moment, the captcha is generated as soon as the library is loaded and takes control.
#2

[eluser]Cristian Gilè[/eluser]
Put the captcha function in a controller:

Code:
function captcha()
{
$string = '';
  
for ($i = 0; $i < 8; $i++)
{  
     $string .= chr(rand(97, 122));  
}  

$code['rand_code'] = $string;
    
$dir = '';
    
$image = imagecreatetruecolor(170, 60);  
$black = imagecolorallocate($image, 0, 0, 0);  
$color = imagecolorallocate($image, 200, 100, 90); // red  
$white = imagecolorallocate($image, 255, 255, 255);
$lgrey = imagecolorallocate($image, 238, 238, 238);    

imagefilledrectangle($image,0,0,200,100,$lgrey);  
imagettftext($image, 30, 5, 10, 40, $color, $dir."font.ttf", $code['rand_code']);
header("Content-type: image/png");  
imagepng($image);
imagedestroy($image);
}

and load the captcha image as follow:

Code:
<img src="&lt;?php echo site_url('controller_name/captcha'); ?&gt;" alt="" />

Make sure to load the url helper and to set the dir variable in the captcha function.
#3

[eluser]Dandy_andy[/eluser]
Thanks. That's a very simple change which works. I think I was over-complicating things!
#4

[eluser]Dandy_andy[/eluser]
How can I then pass a variable from the captcha controller to the view it is being called by if the view is actually an image generated by the controller?
#5

[eluser]Cristian Gilè[/eluser]
You are probably loading your view in the index method of the controller. You can pass a variable there.




Theme © iAndrew 2016 - Forum software by © MyBB