473,382 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Detecting an empty file and removing slashes from text

118 100+
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4.  
  5. ini_set('display_errors', true);
  6.  
  7. $lines = file("***.txt"); // load the lines of text into an array
  8.  
  9. //$image = ImageCreate(660,200); // create the image canvas
  10. $image = imagecreatefromjpeg("***.jpg");
  11. $blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
  12. $black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness
  13.  
  14. $cur_line_y = 30;  // This stores how far down the image the current line will print
  15. ImageFill($image, 0, 0, $blue); // fill the canvas
  16.  
  17. // reverse the order of the lines
  18. $lines = array_reverse($lines);
  19.  
  20.  
  21. // lets get the latest 10 entries
  22. $i = 0; // current line pointer    
  23. foreach($lines as $line) {
  24.     if($i == 10) break;
  25.     $latestlines[] = $line;
  26.     $i++;
  27. }
  28.  
  29. // reverse the order of the latest lines
  30. $latestlines = array_reverse($latestlines);
  31. foreach($latestlines as $line) {
  32.     $cur_line_y += 15;
  33.     ImageString($image, 3, 15, $cur_line_y, trim($line), $black);
  34. }
  35.  
  36.  
  37.  
  38. header("Content-Type: image/png"); // tell the browser what we're gonna give it
  39. ImagePng($image); // paint the image
  40. ImageDestroy($image); // clean up resources
  41. ?>
  42.  
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
Oct 6 '07 #1
9 2377
pbmods
5,821 Expert 4TB
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.
Oct 6 '07 #2
helraizer1
118 100+
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
Oct 6 '07 #3
pbmods
5,821 Expert 4TB
Heya, Sam.

If file() detects an empty file, it will return array(), and:
Expand|Select|Wrap|Line Numbers
  1. empty(array()) === true
If file() cannot open the file, or if an error occurs, it will return false, and:
Expand|Select|Wrap|Line Numbers
  1. empty(false) === true
On the other hand, if there is actually data in the .txt file, then file() will return a non-empty array, and:
Expand|Select|Wrap|Line Numbers
  1. empty({non-empty array}) === false
  2.  
So, check to see if $lines is empty(), and you'll know whether to load the background image.
Oct 6 '07 #4
helraizer1
118 100+
Heya, Sam.

If file() detects an empty file, it will return array(), and:
Expand|Select|Wrap|Line Numbers
  1. empty(array()) === true
If file() cannot open the file, or if an error occurs, it will return false, and:
Expand|Select|Wrap|Line Numbers
  1. empty(false) === true
On the other hand, if there is actually data in the .txt file, then file() will return a non-empty array, and:
Expand|Select|Wrap|Line Numbers
  1. empty({non-empty array}) === false
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. if (empty($lines)) 
  2. {
  3.        $lines[0] = " ";
  4. }
  5.  
So it creates an array with only a space, which suffices to create the image.

Thanks for that pbmods. =D
Oct 7 '07 #5
pbmods
5,821 Expert 4TB
Heya, Sam.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Oct 7 '07 #6
helraizer1
118 100+
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).?
Oct 7 '07 #7
pbmods
5,821 Expert 4TB
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.
Oct 7 '07 #8
helraizer1
118 100+
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("linesfile.php");
  3. $linesDataFile = new DataFile("data.line");
  4.  
  5. //$image = ImageCreate(550,200); // create the image canvas
  6. $image = ImageCreateFromPNG("background.png");
  7. $blue = ImageColorAllocate($image, 200, 200, 255); // prepare some blueness
  8. $black = ImageColorAllocate($image, 0, 0, 0); // ... and whiteness
  9.  
  10. $cur_line_y = 45;  // This stores how far down the image the current line will print
  11. $pagecharwidth = 75; // this is the maximum length of the line before it wraps;
  12. $lineheight = 15; // This is how much to move down to print the next line
  13. $pagelinelimit = 10; // This is the maximum number lines of text that can be displayed
  14.  
  15. ImageFill($image, 0, 0, $blue); // fill the canvas
  16.  
  17. //ImageString($image, 3, 15, $cur_line_y, trim(stripslashes($wordwrapped[0])), $black);
  18.  
  19. $numberOfLines = $linesDataFile->fileSize();
  20.  
  21. for($i=0;$i<$numberOfLines;$i++) {
  22.     $data = $linesDataFile->getNextLine();
  23.     $name = "[" . $data[0] . "] ";
  24.     $color = $data[1];
  25.     $line = $data[2];
  26.  
  27.     $line = $name . $line;
  28.     ImageString($image, 3, 15, $cur_line_y, trim($line), getColor($color));
  29.  
  30.     $cur_line_y += $lineheight;
  31.  
  32. }
  33.  
  34. function getColor($color) {
  35.     global $image;
  36.  
  37.     switch($color) {
  38.         case "black" :
  39.             return ImageColorAllocate($image, 0, 0, 0); 
  40.         case "white" :
  41.             return ImageColorAllocate($image, 255, 255, 255); 
  42.         case "blue" :
  43.             return ImageColorAllocate($image, 0, 0, 255); 
  44.         case "red" :
  45.             return ImageColorAllocate($image, 255, 0, 0); 
  46.         case "yellow" :
  47.             return ImageColorAllocate($image, 255, 255, 0); 
  48.         case "green" :
  49.             return ImageColorAllocate($image, 0, 255, 0); 
  50.         default: 
  51.             return ImageColorAllocate($image, 255, 255, 255); 
  52.  
  53.     }
  54. }
  55.  
  56.  
  57. header("Content-Type: image/png"); // tell the browser what we're gonna give it
  58. ImagePng($image); // paint the image
  59. ImageDestroy($image); // clean up resources
  60. ?>
The layout of the .line file information is:
Username
colour
message

Expand|Select|Wrap|Line Numbers
  1. CHATBOXTEXT
  2. 3
  3. ben
  4. red
  5. hi sam
  6. ben
  7. white
  8. hi sam
  9. helraizer
  10. yellow
  11. This is a test of ChatBox
  12.  
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
Oct 8 '07 #9
helraizer1
118 100+
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
Oct 8 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
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...
1
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...
1
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...
3
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...
19
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...
9
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...
3
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...
7
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...
1
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
1
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...
0
isladogs
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.