tiki Posted November 20, 2007 Share Posted November 20, 2007 I have a form checking class I wrote and im currently writing a method that creates a captcha image, but im relatively new to the image library. Every way ive seen a captcha made it uses a seperate file, is it possible to create the image using a method. Heres my current method, its entirely a work in progress. public function captcha($height, $width, $bgColor = "", $textColor = "", $options = "") { header ("Expires: Sat, 10 Dec 1983 07:00:00 GMT"); header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header ("Pragma: no-cache"); // HTTP/1.0 header ("Content-type: image/gif"); // If no bg color / text color / options set if (!is_array($bgColor)) { $bgColor = array( "red" => 0, "blue" => 0, "green" => 0 ); } if (!is_array($textColor)) { $textColor = array( "red" => 255, "blue" => 255, "green" => 255 ); } if (!is_array($options)) { $options = array( "font" => 1, "x" => 5, "y" => 5 ); } // Create the image $image = imagecreate($width, $height); $image_bg = imagecolorallocate($image, $bgColor["red"], $bgColor["green"], $bgColor["blue"]); $image_text = imagecolorallocate($image, $textColor["red"], $textColor["green"], $textColor["blue"]); imagestring($image, $options["font"], $options["x"], $options["y"], $this->pass, $image_text); imagegif($image); } Quote Link to comment https://forums.phpfreaks.com/topic/78047-using-the-image-library-within-functions/ Share on other sites More sharing options...
MadTechie Posted November 20, 2007 Share Posted November 20, 2007 if you use a function to create the image your need to output to an image FILE (then return the file path/name) or call it without outputting ANYTHING else.. (read up on headers for more info) Quote Link to comment https://forums.phpfreaks.com/topic/78047-using-the-image-library-within-functions/#findComment-395153 Share on other sites More sharing options...
tiki Posted November 20, 2007 Author Share Posted November 20, 2007 So I must have lets say a blank gif file on the server somewere and output the image functions to that image? Quote Link to comment https://forums.phpfreaks.com/topic/78047-using-the-image-library-within-functions/#findComment-395327 Share on other sites More sharing options...
tiki Posted November 20, 2007 Author Share Posted November 20, 2007 Can someone point me in the right direction for this? Quote Link to comment https://forums.phpfreaks.com/topic/78047-using-the-image-library-within-functions/#findComment-395478 Share on other sites More sharing options...
MadTechie Posted November 20, 2007 Share Posted November 20, 2007 Okay lets say i have a script that acts as an image.. ie <?php header("Content-Type: image/jpeg"); $im = imagecreatetruecolor(150, 30); $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); imagestring($im, 1, 5, 5, "This is a image!", $tc); imagejpeg($im); ?> Loading it up by itself works fine.. BUT if add echo "Hello world"; to the start.. (before or after the header) it fails the reason is because the client browser reads the header and expects to get VALID image data and will display it as an image.. now, just say i wanted to use a function to create the image ie <?php function DisplayImage() { header("Content-Type: image/jpeg"); $im = imagecreatetruecolor(150, 30); $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); imagestring($im, 1, 5, 5, "This is a image!", $tc); imagejpeg($im); } ?> this is fine..but how are we going to call it ?? remember you can NOT send anything to the browser before hand! one solution may be this <?php $test="test.jpg"; DisplayImage($test); echo "<img src=\"$test\">"; function DisplayImage($file) { //header("Content-Type: image/jpeg"); $im = imagecreatetruecolor(150, 30); $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); imagestring($im, 1, 5, 5, "This is a image!", $tc); imagejpeg($im,$file); } ?> NOTE: i commented out the header! as we are creating a file Quote Link to comment https://forums.phpfreaks.com/topic/78047-using-the-image-library-within-functions/#findComment-395482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.