Jump to content

Dynamic image creation with GD and a flat text file


crueheads

Recommended Posts

Newbie here, and my first post after banging my head on Google for several hours...  And all I can find are scripts that rip a single random line out of a text file, not do the handling I need done...

 

Anyway here goes, I need a php script that will read a flat text file, entries on the text file are separated by lines and then create a dynamic image using GD...

 

The flat text file looks something like this, and new entries will be added so the list will grow but not overly large, say 30 entries max...

 

Title
Name 1
Hash 1
Name 2
Hash 2
Name 3
Hash 3
Name 4
Hash 4

 

What I need is for PHP to read the list, grab even numbered lines only and create an image of just the 'Name X' entries, in a single column...

 

The width of the created image can be constant and fixed, but I'm having a hard time figuring out how to parse the entire text file, dropping odd numbered entries and generate an image 'tall' enough with all the text to it...  If that makes any sense...  If I have to fix the height of the generated image to a safe height I can accept that...

 

Any help would be appreciated, and thank you in advance...

 

BTW here is the SLOPPY, code I used to verify that my text file creation script was doing its job, this will parse the generated text file and spit out what I want, but getting the output to a generated image is stumping me...

 

 

<?
$file = "data.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000) ) {
$line2 = fgets($f, 1000);
$gtext = nl2br($line2);
print $gtext;
}
?>

$fcontents = join ('', file ($_SERVER['DOCUMENT_ROOT'].'/mytextdata.txt'));
$s_con = split("~",$fcontents);

$data= rand(0,(count($s_con)-1));
echo $s_con[$data]; 

 

Here we are splitting data with symbl ~

 

mytextdata.txt should be like this

 

Name 1
~
Hash 1
~
Name 2
~
Hash 2
~
Name 3

$fcontents = join ('', file ($_SERVER['DOCUMENT_ROOT'].'/mytextdata.txt'));
$s_con = split("~",$fcontents);

$data= rand(0,(count($s_con)-1));
echo $s_con[$data]; 

 

I believe there is a misunderstanding I'm not interested in pulling a random line out, I want all the 'names' to be pulled out and displayed...

 

I want the script to read a text file like this

 

Title
Name 1
Hash 1
Name 2
Hash 2
Name 3
Hash 3

 

and generate a dynamic image like the one attached...

 

[attachment deleted by admin]

you can you even modify this code to get entries one by one :)

 

Well modifying the code that makes the flat text file to include the ~ splitting entries is not a problem...  That's easy enough to change...

 

It's reading that text file and then creating the dynamic image that I'm asking for help with I'm drawing a blank...

OK so modifying your code, I can parse the text file, so now can some one toss me a bone on creating the dynamic image and putting it all together?

 

<?php
$fcontents = join ('', file ($_SERVER['DOCUMENT_ROOT'].'/text.txt'));
$s_con = split("~",$fcontents);
$total = (count($s_con)-1);
$step = 0;
while ($step <= $total)
{
echo (($s_con[$step])"\n");
$step = $step + 2;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.