ChatPad
The layout so far is pretty amateur but it'll improve.
I spent an 1h30/2h or so in this tonight. The concept of ChatPad is basically a dynamic image acting as a shoutbox. -
<?php
-
-
error_reporting(E_ALL);
-
-
ini_set('display_errors', true);
-
-
$lines = file("***.txt"); // load the lines of text into an array
-
-
//$image = ImageCreate(660,200); // create the image canvas
-
$image = imagecreatefromjpeg("***.jpg");
-
$blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
-
$black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness
-
-
$cur_line_y = 30; // This stores how far down the image the current line will print
-
ImageFill($image, 0, 0, $blue); // fill the canvas
-
-
// reverse the order of the lines
-
$lines = array_reverse($lines);
-
-
-
// lets get the latest 10 entries
-
$i = 0; // current line pointer
-
foreach($lines as $line) {
-
if($i == 10) break;
-
$latestlines[] = $line;
-
$i++;
-
}
-
-
// reverse the order of the latest lines
-
$latestlines = array_reverse($latestlines);
-
foreach($latestlines as $line) {
-
$cur_line_y += 15;
-
ImageString($image, 3, 15, $cur_line_y, trim($line), $black);
-
}
-
-
-
-
header("Content-Type: image/png"); // tell the browser what we're gonna give it
-
ImagePng($image); // paint the image
-
ImageDestroy($image); // clean up resources
-
?>
-
On another page (the index page) the user inputs the Name and Message via a textbox, which is then written to a text file, the data from which is put into the image..
I changed the links to ***.. This page code (shown) is responsible for creating the image.
Anyways, to the question in hand!
At the moment, if the file ***.txt is empty (has no data within it) the image is not created and a broken link symbol appears, this is because of the array.
How do I make it so that if the file is blank, the $image (just the background file) is shown?
Also, everytime you enter a character such ' or \ it appears as \' or \\ - is there any way to get rid of the backslash?
Thanks,
Sam
9 2377
Heya, Sam.
Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).
You'll want to look at empty() and stripslashes(), respectively.
You may also want to look into turning off magic quotes.
Awesome. I used the stripslashes function so that all now works. =D
Since the image uses the lines of text in lines.txt in an array and writes them to the image, how would I use the empty() function, with an if statement; so if lines.txt has no lines (it cannot create the array) and should display the background jpeg only.
How would I do this?
Sam
Heya, Sam.
If file() detects an empty file, it will return array(), and:
If file() cannot open the file, or if an error occurs, it will return false, and:
On the other hand, if there is actually data in the .txt file, then file() will return a non-empty array, and: -
empty({non-empty array}) === false
-
So, check to see if $lines is empty(), and you'll know whether to load the background image.
Heya, Sam.
If file() detects an empty file, it will return array(), and:
If file() cannot open the file, or if an error occurs, it will return false, and:
On the other hand, if there is actually data in the .txt file, then file() will return a non-empty array, and: -
empty({non-empty array}) === false
-
So, check to see if $lines is empty(), and you'll know whether to load the background image.
Thanks for that, it worked.
I did -
if (empty($lines))
-
{
-
$lines[0] = " ";
-
}
-
So it creates an array with only a space, which suffices to create the image.
Thanks for that pbmods. =D
Heya, Sam.
Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
I think this could be a javascript question but.. Would there be a way to change the colour of the text inputted?
Say if they click on a link that says 'red' it would change the text they input (and only the text they input) to red.. Would that even be possible with php, or would it be javascript (if it's possible at all for a dynamic image).?
Heya, Sam.
Since PHP is server-side, you'd have to reload the page to save the change.
Since you're dynamically generating an image, you'd have to do the color manipulation on the server side (using imagecolorallocate()).
However, have you thought about using the background image as simply a background image and using HTML and CSS to display the text over the image?
Then you can easily use JavaScript to change the color of the text.
Heya, pbmods. ChatBox
I created the whole thing again (sort of) creating a .line file. and again loaded it into a dynamic image, so you can choose the colour, and it works =D
However I have come across a bug (will explain soon).
The showimage.php page is as follows: -
<?php
-
include("linesfile.php");
-
$linesDataFile = new DataFile("data.line");
-
-
//$image = ImageCreate(550,200); // create the image canvas
-
$image = ImageCreateFromPNG("background.png");
-
$blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
-
$black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness
-
-
$cur_line_y = 45; // This stores how far down the image the current line will print
-
$pagecharwidth = 75; // this is the maximum length of the line before it wraps;
-
$lineheight = 15; // This is how much to move down to print the next line
-
$pagelinelimit = 10; // This is the maximum number lines of text that can be displayed
-
-
ImageFill($image, 0, 0, $blue); // fill the canvas
-
-
//ImageString($image, 3, 15, $cur_line_y, trim(stripslashes($wordwrapped[0])), $black);
-
-
$numberOfLines = $linesDataFile->fileSize();
-
-
for($i=0;$i<$numberOfLines;$i++) {
-
$data = $linesDataFile->getNextLine();
-
$name = "[" . $data[0] . "] ";
-
$color = $data[1];
-
$line = $data[2];
-
-
$line = $name . $line;
-
ImageString($image, 3, 15, $cur_line_y, trim($line), getColor($color));
-
-
$cur_line_y += $lineheight;
-
-
}
-
-
function getColor($color) {
-
global $image;
-
-
switch($color) {
-
case "black" :
-
return ImageColorAllocate($image, 0, 0, 0);
-
case "white" :
-
return ImageColorAllocate($image, 255, 255, 255);
-
case "blue" :
-
return ImageColorAllocate($image, 0, 0, 255);
-
case "red" :
-
return ImageColorAllocate($image, 255, 0, 0);
-
case "yellow" :
-
return ImageColorAllocate($image, 255, 255, 0);
-
case "green" :
-
return ImageColorAllocate($image, 0, 255, 0);
-
default:
-
return ImageColorAllocate($image, 255, 255, 255);
-
-
}
-
}
-
-
-
header("Content-Type: image/png"); // tell the browser what we're gonna give it
-
ImagePng($image); // paint the image
-
ImageDestroy($image); // clean up resources
-
?>
The layout of the .line file information is:
Username
colour
message -
CHATBOXTEXT
-
3
-
ben
-
red
-
hi sam
-
ben
-
white
-
hi sam
-
helraizer
-
yellow
-
This is a test of ChatBox
-
The bug is that, when the messages get to the bottom of the image, they carry on going, and just drop off the end.
With my last code I could get the last 10 lines, but since the messages that would need to to show in this one are on 3 lines, excluding lines 1 and 2. How would I get it so that it writes the message on the image and knocks the last one off the bottom, rather than carry on beyond the limits of the image?
Hope I was clear there,
Thanks,
Sam
Wasn't able to edit my post for some reason, sorry for the consecutive posting.
With most dynamic images on the web, you can view them and save them as .png or .jpg files, for mine you can only save or view it as a .php file... How would I output it to an image such as .jpg or.png? Or is the inability a side effect of Apache?
Thanks,
Sam
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Wayno |
last post by:
My php logs are coming up empty. I have done all I can think of, and
all that made sense to me. Can someone take a look at my php.ini
please and tell me what you think may be the problem.
I...
|
by: mr_burns |
last post by:
Hi,
I have been sending emails using PHP with the mail() command/method.
When I send them tho it adds slashes, for example:
'hi guy's' gets sent as 'hi guy\'s'
Why is this? How do I sort...
|
by: fabrice |
last post by:
Hello,
I've got trouble reading a text file (event viewer dump) by using the
getline() function...
After 200 - 300 lines that are read correctly, it suddenly stops
reading the rest of the...
|
by: William Starr Moake |
last post by:
Is there a script to convert image file paths from absolute to just
img src="imagename.jpg"? This is for an IE-based WYSIWYG editor. The
doImage execCommand produces an absolute file path, which...
|
by: wetherbean |
last post by:
Hi group..I am writing a playlist management protocol where I have a
file that holds all the playlists and a file that holds all the
songs....before a playlist is created I need to check to see if...
|
by: D. Shane Fowlkes |
last post by:
I'm using SQL Server 2000 and on my page, I'm simply creating a
SQLDataReader and filling in Labels with the retrieved (single) record.
However, how can I prevent from getting errors when a field...
|
by: Simon Prince |
last post by:
Hi,
I'm trying detect an empty cell in a datagrid when the content is posted
back to the server.
My code is...
Dim vDgi_GridItem As DataGridItem
For Each vDgi_GridItem In...
|
by: iaesun |
last post by:
hello,
this post assumes executing a program with file redirection as follows:
program.exe < intput.txt
where input.txt is a text file where there is not necessarily any
whitespace after...
|
by: Christian Heimes |
last post by:
otaeris@o2.pl schrieb:
As far as I remember the zip format it's not possible at all. Folders
are created implicitly. The zip format doesn't support empty directories.
Christian
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |