Jump to content


Photo

Help with PHP ImageCreate


  • Please log in to reply
4 replies to this topic

#1 GuitarGod

GuitarGod
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 24 September 2006 - 08:05 AM

Hey guys,

I recently tried to create an image which would change it's text, font, background-colour etc when the page was refreshed (similar to a confirmation code image that you often see on registration pages. I put the code in a file named confirmation_code.php, when i visit this file, the image works fine, but when i include the file on other pages, i get an error code and the image doesn't work either. Can anyone please help me or tell me what i'm doing wrong?

The code for confirmation_code.php:
<?php
$chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";

	srand((double)microtime()*1000000);
	$i = 0;
	
	$code_length = 5; 

	$code_return = ' ' ;

	while ($i <= $code_length)
	{
		$num = rand() % 33;
		$tmp = substr($chars, $num, 1);
		$code_return = $code_return . $tmp;
		$i++;
	}

	// ----
	// Generate the text angle

	for ($i = 0; $i < 15; $i++)
	{
		$angle[] = $i;
	}

	srand ((double) microtime() * 1000000);
  	$random_angle = rand(0,count($angle)-1);

	// ----
	// Generate the background image colour

	$bg_colour[] = array("255", "255", "204");
	$bg_colour[] = array("255", "255", "153");
	$bg_colour[] = array("255", "255", "102");
	$bg_colour[] = array("255", "255", "51");
	$bg_colour[] = array("255", "255", "0");
	$bg_colour[] = array("255", "204", "255");
	$bg_colour[] = array("255", "204", "204");
	$bg_colour[] = array("255", "204", "153");
	$bg_colour[] = array("255", "204", "102");
	$bg_colour[] = array("255", "204", "51");
	$bg_colour[] = array("255", "204", "0");
	$bg_colour[] = array("255", "153", "255");
	$bg_colour[] = array("255", "153", "204");
	$bg_colour[] = array("255", "153", "153");
	$bg_colour[] = array("255", "153", "102");
	$bg_colour[] = array("255", "153", "51");
	$bg_colour[] = array("255", "153", "0");
	$bg_colour[] = array("255", "102", "255");
	$bg_colour[] = array("255", "102", "204");
	$bg_colour[] = array("255", "102", "153");
	$bg_colour[] = array("255", "102", "102");
	$bg_colour[] = array("255", "102", "51");
	$bg_colour[] = array("255", "102", "0");
	$bg_colour[] = array("255", "51", "255");
	$bg_colour[] = array("255", "51", "204");
	$bg_colour[] = array("255", "51", "153");
	$bg_colour[] = array("255", "51", "102");
	$bg_colour[] = array("255", "51", "51");
	$bg_colour[] = array("255", "51", "0");
	$bg_colour[] = array("255", "0", "255");
	$bg_colour[] = array("255", "0", "204");
	$bg_colour[] = array("255", "0", "153");
	$bg_colour[] = array("255", "0", "102");
	$bg_colour[] = array("255", "0", "51");
	$bg_colour[] = array("255", "0", "0");

	srand ((double) microtime() * 1000000);
  	$random_bg_colour = rand(0,count($bg_colour)-1);

	// ----
	// Generate the text colour

	$txt_colour[] = array("0", "255", "204");
	$txt_colour[] = array("0", "255", "153");
	$txt_colour[] = array("0", "255", "102");
	$txt_colour[] = array("0", "255", "51");
	$txt_colour[] = array("0", "255", "0");
	$txt_colour[] = array("0", "204", "255");
	$txt_colour[] = array("0", "204", "204");
	$txt_colour[] = array("0", "204", "153");
	$txt_colour[] = array("0", "204", "102");
	$txt_colour[] = array("0", "204", "51");
	$txt_colour[] = array("0", "204", "0");
	$txt_colour[] = array("0", "153", "255");
	$txt_colour[] = array("0", "153", "204");
	$txt_colour[] = array("0", "153", "153");
	$txt_colour[] = array("0", "153", "102");
	$txt_colour[] = array("0", "153", "51");
	$txt_colour[] = array("0", "153", "0");
	$txt_colour[] = array("0", "102", "255");
	$txt_colour[] = array("0", "102", "204");
	$txt_colour[] = array("0", "102", "153");
	$txt_colour[] = array("0", "102", "102");
	$txt_colour[] = array("0", "102", "51");
	$txt_colour[] = array("0", "102", "0");
	$txt_colour[] = array("0", "51", "255");
	$txt_colour[] = array("0", "51", "204");
	$txt_colour[] = array("0", "51", "153");
	$txt_colour[] = array("0", "51", "102");
	$txt_colour[] = array("0", "51", "51");
	$txt_colour[] = array("0", "51", "0");
	$txt_colour[] = array("0", "0", "255");
	$txt_colour[] = array("0", "0", "204");
	$txt_colour[] = array("0", "0", "153");
	$txt_colour[] = array("0", "0", "102");
	$txt_colour[] = array("0", "0", "51");
	$txt_colour[] = array("0", "0", "0");

	srand ((double) microtime() * 1000000);
  	$random_txt_colour = rand(0,count($txt_colour)-1);

	// ----
	// Generate the font for the image

	$font[] = "georgia.ttf";
	$font[] = "comic.ttf";
	$font[] = "trebucbi.ttf";
	$font[] = "timesbd.ttf";
	$font[] = "impact.ttf";

	srand ((double) microtime() * 1000000);
  	$random_font = rand(0,count($font)-1);

	// ----
	// Now that the code has been generated, time to place it onto an image
	
	Header("Content-type: image/png"); 

	$w = 250; 

	$h = 70; 

	$im = ImageCreate($w, $h);

	$background_color = imagecolorallocate($im, $bg_colour[$random_bg_colour][0], $bg_colour[$random_bg_colour][1], $bg_colour[$random_bg_colour][2]); 

	$text = ImageColorAllocate($im, $txt_colour[$random_txt_colour][0], $txt_colour[$random_txt_colour][1], $txt_colour[$random_txt_colour][2]); 

	ImageRectangle($im,0,0,$w - 1,$h - 1,$BorderColor);  

	ImageFtText ($im, 16, $random_angle, 50, 50, $text, "$font[$random_font]", "$code_return", array());

	ImagePng($im);

	ImageDestroy($im);
?>

MOD EDIT : Please use code tags

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 24 September 2006 - 08:34 AM

Have you tried placing it with

<img src="confirmation_code.php">
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 24 September 2006 - 08:46 AM

BTW,

You place the image on the page and the user types the same characters into a text box and submits the form.

With your image code, how do you now verify the user entered the same characters?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 GuitarGod

GuitarGod
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 24 September 2006 - 10:53 AM

It works!! :D Dude, if you were here i'd bow down to you!!

As for your question, what i planned to do was create a session containing the generated code, then compare the code the user has entered with the session.

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 24 September 2006 - 10:58 AM

You could generate the random code outside the image, store in session var then pass it to the image code

$key = gen_rand_string();
$_SESSION['key'] = $key;

echo "<img src=\"confirmation_code.php?text=$key\">";

EDIT : Ignore this. Better to generate and save from within the image code, then it doesn't appear in page source.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users