Jump to content

Help with PHP ImageCreate


GuitarGod

Recommended Posts

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 [i]confirmation_code.php[/i], 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:
[code]<?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);
?>[/code]

MOD EDIT : Please use code tags
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.