CodeIgniter Forums
Sending an image generated by imagepng to a view - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Sending an image generated by imagepng to a view (/showthread.php?tid=52250)



Sending an image generated by imagepng to a view - El Forum - 06-03-2012

[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.


Sending an image generated by imagepng to a view - El Forum - 06-03-2012

[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.


Sending an image generated by imagepng to a view - El Forum - 06-03-2012

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


Sending an image generated by imagepng to a view - El Forum - 06-03-2012

[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?


Sending an image generated by imagepng to a view - El Forum - 06-03-2012

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