Jump to content

Recommended Posts

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); 
}

Link to comment
https://forums.phpfreaks.com/topic/78047-using-the-image-library-within-functions/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.