|  overlaying text on image in php | 
| Author: | 
Message: | 
Supersonicdarky 
Veteran Member 
     
  
  
 
Posts: 2316 Reputation: 48 
– / – /   
Joined: Feb 2005
 
Status: Away
 
 | 
O.P.  overlaying text on image in php
why is it that when i try to overlay a text file with line breaks, it shows some random symbol instead? 
 
should be
 
Some 
Random 
Text
 code: <?php 
header("Content-type: image/png"); 
$im    = imagecreatefrompng("sigs/chichan.png"); 
$color = imagecolorallocate($im, 0, 0, 0); 
$file = fopen("text.txt","r"); 
$text = fread($file, filesize("text.txt")); 
$text2 = str_replace("<br />", "\n", $text); 
imagestring($im, 2, 100, 20, $text2, $color); 
imagepng($im); 
imagedestroy($im); 
fclose ($file); 
?>
  
(it shows the symbol when the txt just has line breaks or when i replace something with \n)
 
-this is ansi, utf doesnt work either  
 This post was edited on 09-17-2007 at 10:37 PM by Supersonicdarky.
 |   
 | 
| 09-17-2007 10:36 PM | 
 | 
  | 
Ezra 
Veteran Member 
     
  
  
Forgiveness is between them and God
  
Posts: 1960 Reputation: 31 
38 /   /   
Joined: Mar 2003 
 | 
 RE: overlaying text on image in php
Are you using a windows server? It might have a problem that you are using linux line-endings (\n) and not windows line endings (\r\n) 
If your server is *nix then I don't know    
 |   
 | 
| 09-17-2007 11:15 PM | 
 | 
  | 
Supersonicdarky 
Veteran Member 
     
  
  
 
Posts: 2316 Reputation: 48 
– / – /   
Joined: Feb 2005
 
Status: Away
 
 | 
| 
O.P.  RE: overlaying text on image in php
 i'm pretty sure its debian 
 |   
 | 
| 09-17-2007 11:18 PM | 
 | 
  | 
WDZ 
Former Admin 
     
  
  
 
Posts: 7105 Reputation: 107 
– /   /   
Joined: Mar 2002 
 | 
| 
 RE: overlaying text on image in php
 I don't think you can do line breaks with imagestring() at all. You need to call the function once for each line of text, increasing the y coordinate each time. 
 |   
 | 
| 09-17-2007 11:21 PM | 
 | 
  | 
Supersonicdarky 
Veteran Member 
     
  
  
 
Posts: 2316 Reputation: 48 
– / – /   
Joined: Feb 2005
 
Status: Away
 
 | 
| 
O.P.  RE: overlaying text on image in php
 aww. lame. 
 
I'll try to do it my self. if i cant, i'll come for help ;> 
 |   
 | 
| 09-17-2007 11:23 PM | 
 | 
  | 
Ezra 
Veteran Member 
     
  
  
Forgiveness is between them and God
  
Posts: 1960 Reputation: 31 
38 /   /   
Joined: Mar 2003 
 | 
 RE: overlaying text on image in php
ImageTTFText works with line-endings.
 code: <?php 
  header("Content-type: image/png"); 
  $im    = imagecreatefrompng("write.png"); 
  $color = imagecolorallocate($im, 0, 0, 0); 
  $file = fopen("text.txt","r"); 
  $text = fread($file, filesize("text.txt")); 
  $text2 = str_replace("<br/>", "\n", $text); 
  $font = "arial.ttf"; 
  //imagestring($im, 2, 150, 60, $text2, $color); 
  imagettftext($im, 12, 0, 150, 60, $color, $font, $text2); 
  imagepng($im); 
  imagedestroy($im); 
  fclose ($file); 
?>
  
EDIT: Added used code and example
 
EDIT2: MyBB tags are hard, when you are tired    
 This post was edited on 09-18-2007 at 12:31 AM by Ezra.
 |   
 | 
| 09-17-2007 11:34 PM | 
 | 
  | 
Supersonicdarky 
Veteran Member 
     
  
  
 
Posts: 2316 Reputation: 48 
– / – /   
Joined: Feb 2005
 
Status: Away
 
 | 
O.P.  RE: overlaying text on image in php
awesome! 
but what is the 3rd var in imagettftext()? 
 
code: imagettftext($im, 12, 0, 150, 60, $color, $font, $text2);
  
nvm, clicked link 
 why is it that when I open the file as r+ it acts as though its w/w+ ? (replaces what is already in the file with the string)
 http://ssd.sc47.info/Random/random%20sig/post.php
code: <?php 
 
$item = $_POST["item"]; 
$name = $_POST["name"]; 
 
if ($item && $name) 
    $file = fopen("text.txt", "r+"); 
    fwrite($file, date("H:m ")." - ".$name." - ".$item."<br />"); 
    fclose($file); 
     
 
 
?> 
 
<img src="write.php" alt="" /> 
 
<form action="post.php" method="post"> 
    <table cellspacing="0" cellpadding="1" border="0"> 
        <tr> 
            <td>Name /td><td><input type="text" name="name" maxlength="12" /></td> 
        </tr> 
        <tr> 
            <td>Message /td><td><input type="text" name="item" maxlength="30" /></td> 
        </tr> 
    </table> 
    <input type="Submit" value="Shout!" /> 
</form> 
  
a+ works fine, but i want it to add new line at the beginning of file  
 This post was edited on 09-17-2007 at 11:50 PM by Supersonicdarky.
 |   
 | 
| 09-17-2007 11:49 PM | 
 | 
  | 
roflmao456 
Skinning Contest Winner 
    
  
  
 
Posts: 955 Reputation: 24 
31 /   /   
Joined: Nov 2006
 
Status: Away
 
 | 
 RE: overlaying text on image in php
code: <?php 
 
$item = $_POST["item"]; 
$name = $_POST["name"]; 
 
if ($item && $name) 
    $file = fopen("text.txt", "r+"); 
    fwrite($file, date("H:m:s")." - ".$name." - ".$item."<br />".file_get_contents("text.txt")); 
    fclose($file); 
     
 
 
?> 
 
<img src="write.php" alt="" /> 
 
<form action="post.php" method="post"> 
    <table cellspacing="0" cellpadding="1" border="0"> 
        <tr> 
            <td>Name:</td><td><input type="text" name="name" maxlength="12" /></td> 
        </tr> 
        <tr> 
            <td>Message:</td><td><input type="text" name="item" maxlength="30" /></td> 
        </tr> 
    </table> 
    <input type="Submit" value="Shout!" /> 
</form> 
   
 This post was edited on 09-18-2007 at 12:39 AM by roflmao456.
[quote]  
Ultimatess6: What a noob mod  
 |   
 | 
| 09-18-2007 12:38 AM | 
 | 
  | 
Supersonicdarky 
Veteran Member 
     
  
  
 
Posts: 2316 Reputation: 48 
– / – /   
Joined: Feb 2005
 
Status: Away
 
 | 
O.P.  RE: overlaying text on image in php
 
but why doesnt it work the way its supposed to =/  
 |   
 | 
| 09-18-2007 12:41 AM | 
 | 
  | 
WDZ 
Former Admin 
     
  
  
 
Posts: 7105 Reputation: 107 
– /   /   
Joined: Mar 2002 
 | 
 RE: overlaying text on image in php
There is no "prepend" mode... r+ lets you read and write any position within the file, and the file won't be automatically truncated. 
If example.txt contains "aaaaaaaaaa" and you do this...
 $fp = fopen('example.txt', 'r+'); 
fwrite($fp, 'bbbbb'); 
fclose($fp);
It will contain "bbbbbaaaaa".    
 |   
 | 
| 09-18-2007 01:40 AM | 
 | 
  | 
| 
 |