PDA

View Full Version : Spaces in my GD please.


student101
07-22-2008, 05:55 PM
How do I take this and add spaces in between?
This outputs a 5 digit number like this: 28641
if(!isset($_SESSION)) {
session_start(); }
$text = rand(10000,99999) ;
$_SESSION["thecode"] = $text;

$height = 25;
$width = 55;

$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 255, 255, 255);
$white = imagecolorallocate($image_p, 255, 0, 0);
$font_size = 12;
imagestring($image_p, $font_size, 5, 10, $text, $white);
imagejpeg($image_p, null, 100);
I would like to make this: 2 8 6 4 1 - just add spaces.
Cheers

davidj
07-22-2008, 07:46 PM
wrap your chops round this

example...


/// rand function
$text = rand(10000,99999) ;

/// count length of $text
$countLength = strlen($text);

/// initiate var
$randString = "";

/// set var val
$max = 1;

/// loop through each char
for($i=0;$i<$countLength;$i++){

/// pick each char seperatly and add to var
$randString .= substr($text,$i,$max);

/// now add a space before the next
$randString .=" ";

}

/// trim any white space from the ends
echo trim($randString);


im sure you can improve this a man of your calibre

student101
07-23-2008, 06:17 AM
Pretty cool, I see you aren't using GD.

//Check if the session is started, if not start it!
if(!isset($_SESSION)) {
session_start(); }

//Set a variable to a random number
$text = rand(10000,99999) ;

//Set a session with the now random number
$_SESSION["vercode"] = $text;

//Split the random number
$text = str_split( $text );

//Add spaces around the random number
$text = implode( ' ', $text );

//set the width and height
$height = 25;
$width = 85;

//Set a variable to use later and create an image
$image_p = imagecreate($width, $height);

//Set the color and font size
$black = imagecolorallocate($image_p, 255, 255, 255);
$white = imagecolorallocate($image_p, 255, 0, 0);
$font_size = 12;

//Draw the string
imagestring($image_p, $font_size, 5, 10, $text, $white);

//Output the image to browser
imagejpeg($image_p, null, 100);

davidj
07-23-2008, 07:44 AM
what i have done is provided a solution to your requirements

its your job to take what i provided and graft it in place

davidj
07-23-2008, 07:46 AM
addition...

you could creat a function and pass in the rand num as an argument then output the processed number

student101
07-23-2008, 07:48 AM
Does GD work on all browsers? if not then a random num like yours will have to do.
Or this could do it...

//numbers or letters could work here
$random_chars = substr(str_shuffle('abcdefghijklmnopqrstuvwxyz'), 0, 6);
$_SESSION['secret']=$random_chars;

davidj
07-23-2008, 10:21 AM
thought your objective was to insert a space between random numerics turning this 345 into 3 4 5

this is what i provided

is this what you want?


Does GD work on all browsers?


GD does not care or know what a browser is.

it produces images so the image type is relevant though

student101
07-23-2008, 05:16 PM
I was just adding a different random idea.
Yes, thank you.