...

View Full Version : Image Generation from FlatFile.



EffakT
04-12-2012, 01:33 AM
Well, I had this working perfectly until they stopped using a mysql database and started using a flatfile.
What it should do:
Grab data from the flatfile, trim the unwanted parts and set it in an array.
With the received data display as an image.
I am very confused about how I would do this.

My current code:
signature.php:

<?php

//GET AND TRIM
$fileName="accounts.txt";
$fileHandle = fopen($fileName,"r");
$filecontents = fread($fileHandle,filesize($fileName));

$exploded = explode(" ",$filecontents);
$name="";
$balance="";
$accounts=array();
$num=0;
for($i=0;$i<count($exploded);$i++)
{

$line = explode("\t",$exploded[$i]);
for($a=0;$a<count($line);$a++)
{

if(trim($line[$a])=="player")
{
}elseif($line[$a]=="money")
{
}elseif($line[$a]=="type")
{
}elseif(trim($line[$a])=="{")
{
}else
{
$subline = explode("\r",$line[$a]);
for($e=0;$e<count($subline);$e++)
{
if(trim($subline[$e])=="}")
{
}
elseif(is_numeric(trim($subline[$e])))
{
$balance=trim($subline[$e]);
}
else
{
$name=trim($subline[$e]);
}
if($name!=""&&$balance!="")
{
$temp=array($name=>$balance);
$accounts[$num]=$temp;
$num++;
$name="";
$balance="";
}
}
}

}


}

//Image Generation
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$font = "minecraft.ttf";
$user="";
if(isset($_GET['u']))
{
if(!empty($_GET['u']))
{
$user = $_GET['u'];
}
}

if(isset($_GET['i']))
{
if(!empty($_GET['i']))
{
$img = $_GET['i'];
}
else{$img='1';}
}
else{$img='1';}

if(isset($_POST['u'],$_POST['i']))
{
if(!empty($_POST['u']))
{
$user=$_POST['u'];
}
if(!empty($_POST['i']))
{
$img = $_POST['i'];
}
}

if(in_array($user, $accounts))
{
//Info Grabs
$money = get_balance($user);
//End Info Grabs
}
else
{
die("That is an invalid player.");
}

//Display Image

$user = ucfirst($user);

$image = imagecreatefrompng("images/".$img.".png");
$color = imagecolorallocate($image, 255,255,255);

if ($_GET['i'] >= '8')
{

ImageTTFText ($image, "8", 0, 100, 13, $color, $font,"Name:");
ImageTTFText ($image, "8", 0, 138, 13, $color, $font,$user);

ImageTTFText ($image, "8", 0, 240, 13, $color,$font,"Money:");
ImageTTFText ($image, "8", 0, 285, 13, $color,$font,"$".$money);
}

else
{
ImageTTFText ($image, "7", 0, 246, 10, $color, $font,"Spartan Universe");
ImageTTFText ($image, "12", 0, 130, 50, $color, $font,"Name:");
ImageTTFText ($image, "12", 0, 130, 96, $color,$font,"Money:");
ImageTTFText ($image, "12", 0, 185, 50, $color, $font,$user);
ImageTTFText ($image, "12", 0, 198, 96, $color,$font,"$".$money);
}

//End Image Display

header("Content-type: image/png");
imagepng($image);
imagedestroy($image);

?>

accounts.txt:

arkangel011 {
type player
money 23.47
}
chemicalbacon {
type player
money 203.89
}


What I would like to know is how would I go about getting the information form the array created called accounts?

mlseim
04-12-2012, 01:51 AM
Does each player name only appear one time in the .txt file?

Example, "chemicalbacon" only appears this one time ... ?
chemicalbacon {
type player
money 203.89
}

Or do you have to find several "chemicalbacon" entries and add them up?

EffakT
04-12-2012, 01:54 AM
Only once, the file gets updated regularly adding to the number in it.

litebearer
04-12-2012, 02:09 AM
Just a thought (pseudo code

rethink your txt file ie
arkangel011,player,23.47
chemicalbacon,player,203.89

use file() to put the lines into an array
$lines = file($textfilename);

loop thru the array
$count = count($lines)
for($i = 0; $i<$count; $i++) {
expolde each line into a temp array
$temp[$i] = explode(",", $lines[$i]);
use the values in the temp array as you desire
}

EffakT
04-12-2012, 02:13 AM
I am unable to edit how the accounts.txt file stores its information.

mlseim
04-12-2012, 02:16 AM
litebearer has the right idea ...

You'll have to loop through the array ...

Look for { to appear on a line,
explode by " " and grab the name.

Look for "type" on a line,
explode by " " and grab the type.

Look for "money" on a line,
explode by " " and grab the amount.

Look for the closing "}" on a line.
That signals the end of that player ...

You begin the parsing again ... looking for the next "{"

etc.


.

EffakT
04-12-2012, 03:07 AM
Right, I am not actually sure how to do that. Could I see pseudo code or an example please?

litebearer
04-12-2012, 04:23 AM
a start (see if you can expand upon it)...


$contents = file_get_contents($filename);
$my_array01 = explode("}", $contents);
print_r($my_array01);


ok ok...


<?PHP
/* for this example we are creating a string that represents the content of your file */
$content = "arkangel011 {
type player
money 23.47
}
chemicalbacon {
type player
money 203.89
}";

/*
this how you would actually get the file contents
$content = file_get_contents($filename);
*/

$content = str_replace("}", "~", $content);
$content = str_replace("{", "", $content);
$content = str_replace("type", "|", $content);
$content = str_replace("money", "|", $content);
$content = preg_replace("/[^a-zA-Z0-9\.\~\|]/", "", $content);
$my_array_00 = explode("~", $content);
/* note you will need to remove the last array element */
$junk = array_pop($my_array_00);
$count = count($my_array_00);
for($i=0; $i<$count; $i++) {
$temp = explode("|", $my_array_00[$i]);
echo "Name: " . $temp[0] . "<br>";
echo "Type: " . $temp[1] . "<br>";
echo "Money: " . $temp[2] . "<hr>";

}
echo "<PRE>";
print_r($my_array_00);
echo "</pre>";
?>

EffakT
04-12-2012, 05:17 AM
Okay, I will test it as soon as we can get this FTP to work properly.

EffakT
04-12-2012, 11:26 AM
Awesome it worked.

Just a slight problem I have.

<?PHP
//Stop errors from displaying
//ini_set('display_errors', 0);

// Get information and trim/replace
$name = $_GET['u'];
$content = file_get_contents('./accounts.txt');
$content = str_replace("}", "~", $content);
$content = str_replace("{", "", $content);
$content = str_replace("type", "|", $content);
$content = str_replace("money", "|", $content);
$content = preg_replace("/[^a-zA-Z0-9\.\~\|]/", "", $content);
$accounts = explode("~", $content);

//check if $name is in $accounts array
if (in_array($name, $accounts))
{
$junk = array_pop($accounts);
$count = count($accounts);
for($i=0; $i<$count; $i++)
{
//Display results
$temp = explode("|", $accounts[$i]);
echo "Name: " . $temp[0] . "<br>";
echo "Type: " . $temp[1] . "<br>";
echo "Money: " . $temp[2] . "<hr>";

}
}
else
{
echo "Account Not Found";
}
?>

No matter what name I put in it returns Account Not Found.

litebearer
04-12-2012, 02:05 PM
Try...



<?PHP
//Stop errors from displaying
//ini_set('display_errors', 0);
// Get information and trim/replace
$name = $_GET['u'];
$content = file_get_contents('./accounts.txt');
if(stristr($content, $name) !== FALSE){
$content = str_replace("}", "~", $content);
$content = str_replace("{", "", $content);
$content = str_replace("type", "|", $content);
$content = str_replace("money", "|", $content);
$content = preg_replace("/[^a-zA-Z0-9\.\~\|]/", "", $content);
$accounts = explode("~", $content);
$junk = array_pop($accounts);
$count = count($accounts);
for($i=0; $i<$count; $i++){
//Display results
$temp = explode("|", $accounts[$i]);
if($name == $temp[0]) {
echo "Name: " . $temp[0] . "<br>";
echo "Type: " . $temp[1] . "<br>";
echo "Money: " . $temp[2] . "<hr>";
}
}
}else{
echo "Account Not Found";
}
?>

EffakT
04-13-2012, 12:16 AM
Try...



<?PHP
//Stop errors from displaying
//ini_set('display_errors', 0);
// Get information and trim/replace
$name = $_GET['u'];
$content = file_get_contents('./accounts.txt');
if(stristr($content, $name) !== FALSE){
$content = str_replace("}", "~", $content);
$content = str_replace("{", "", $content);
$content = str_replace("type", "|", $content);
$content = str_replace("money", "|", $content);
$content = preg_replace("/[^a-zA-Z0-9\.\~\|]/", "", $content);
$accounts = explode("~", $content);
$junk = array_pop($accounts);
$count = count($accounts);
for($i=0; $i<$count; $i++){
//Display results
$temp = explode("|", $accounts[$i]);
if($name == $temp[0]) {
echo "Name: " . $temp[0] . "<br>";
echo "Type: " . $temp[1] . "<br>";
echo "Money: " . $temp[2] . "<hr>";
}
}
}else{
echo "Account Not Found";
}
?>


Excellent. Thanks you so much!

EDIT:

Now, I put it together with the image generation portion of my original code and it displays Account Not Found again?

<?PHP
//Stop errors from displaying
ini_set('display_errors', 0);
//Stop Cache
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

// Get information and trim/replace
$content = file_get_contents('./accounts.txt');
if(stristr($content, $name) !== FALSE){
$content = str_replace("}", "~", $content);
$content = str_replace("{", "", $content);
$content = str_replace("type", "|", $content);
$content = str_replace("money", "|", $content);
$content = preg_replace("/[^a-zA-Z0-9\.\~\|]/", "", $content);
$accounts = explode("~", $content);
$junk = array_pop($accounts);
$count = count($accounts);
for($i=0; $i<$count; $i++){
//Display results
$temp = explode("|", $accounts[$i]);

//Image Generation
$font = "minecraft.ttf";
$user="";
if(isset($_GET['u']))
{
if(!empty($_GET['u']))
{
$user = $_GET['u'];
}
}

if(isset($_GET['i']))
{
if(!empty($_GET['i']))
{
$img = $_GET['i'];
}
else{$img='1';}
}
else{$img='1';}

if(isset($_POST['u'],$_POST['i']))
{
if(!empty($_POST['u']))
{
$user=$_POST['u'];
}
if(!empty($_POST['i']))
{
$img = $_POST['i'];
}
}

if($name == $temp[0]) {

//Display Image
$user = ucfirst($user);
$image = imagecreatefrompng("./images/".$img.".png");
$color = imagecolorallocate($image, 255,255,255);

if ($_GET['i'] >= '8')
{

ImageTTFText ($image, "8", 0, 100, 13, $color, $font,"Name:");
ImageTTFText ($image, "8", 0, 138, 13, $color, $font,$temp[0]);

ImageTTFText ($image, "8", 0, 240, 13, $color,$font,"Money:");
ImageTTFText ($image, "8", 0, 285, 13, $color,$font,"$".$temp[2]);
}

else
{
ImageTTFText ($image, "7", 0, 246, 10, $color, $font,"Spartan Universe");
ImageTTFText ($image, "12", 0, 130, 50, $color, $font,"Name:");
ImageTTFText ($image, "12", 0, 130, 96, $color,$font,"Money:");
ImageTTFText ($image, "12", 0, 185, 50, $color, $font,$temp[0]);
ImageTTFText ($image, "12", 0, 198, 96, $color,$font,"$".$temp[2]);
}


}
}
header("Content-type: image/png");
imagepng($image);
imagedestroy($image);
}else{
echo "Account Not Found";
}

?>

litebearer
04-13-2012, 01:40 AM
well let's see... Hmmmm where are you defining $name?

EffakT
04-13-2012, 01:54 AM
Oh... Fail of my behalf. thanks for pointing that out :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum