Jump to content

Help putting text onto image


Kairu

Recommended Posts

Alright.... I managed to scrape this together.... It works in my head.... Just not on the server (imagine!). I was hoping someone could spot the errors? Some of this is new to me, having never tried to put text onto an image before.

Oh! I almost forgot. I also need to know how to ouput the data from more then one row, since I have three rows of data returning.

I hope you can help!

[code]<?php
header ('Content-Type: image/gif');

mysql_connect('localhost:3306', 'P', 'U');

mysql_select_db('gaia_image');

$result = mysql_query('SELECT *
FROM `box`
WHERE ID = 1
ORDER BY `box`.`Timestamp` DESC');

$row = mysql_fetch_array($result);

for ($i=0; $i<=1; $i++)
{
$row[$i + 1] = str_replace("\r", '', str_replace("\n", '', $row[$i + 1]));
}

$image = imagecreatefromstring('background.jpg');

$text_color = imagecolorallocate($image, 55, 55, 55);


$img = (floor(imagesy($image)/imagefontheight(2)) * imagefontheight(2)) - imagefontheight(2);

for ($i = 0; $i <= 1 ; $i++)
{
$text = explode("\n", wordwrap($row[$i + 1], floor((imagesx($image)-11)/imagefontwidth(2)), "\n", 1));

for ($j = (count($text) - 1); $j >= 0; $j--)
{
imagestring($image, 2, 7, $img, $text[$j], $text_color);
$img -= imagefontheight(2);
}
}

imagegif($image);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/32293-help-putting-text-onto-image/
Share on other sites

Ok... forget the original code, did a rewrite after noticing a few glaring errors in my code.

[code]<?php
header ('Content-Type: image/gif');

mysql_connect('localhost:3306', 'U', 'P');

mysql_select_db('gaia_image');

$result = mysql_query('SELECT *
FROM `box`
WHERE ID = 1
ORDER BY `box`.`Timestamp` DESC');

$row = mysql_fetch_array($result);

for ($i=0; $i<=1; $i++)
{
$row[$i + 1] = str_replace("\r", '', str_replace("\n", '', $row[$i + 1]));
}

$image = imagecreatefromstring('http://thedarkrealm.no-ip.org/shoutboxes/background.jpg');

$text_color = imagecolorallocate($image, 55, 55, 55);


$img = (floor(imagesy($image)/imagefontheight(2)) * imagefontheight(2)) - imagefontheight(2);

for ($i=0; $i <= 0; i++)
{
$text = $row[1] . ": " . $row[2];
$text = wordwrap($text, floor((imagesx($image)-11)/imagefontwidth(2)), "\n    ", 1)
$text = explode("\n", $text);

for ($j = (count($text) - 1); $j >= 0; $j--)
{
imagestring($image, 2, 7, $img, $text[$j], $text_color);
$img -= imagefontheight(2);
}
}

imagegif($image);
?>[/code]
Well, you can move [code=php:0]header()[/code] down to just above [code=php:0]imagegif()[/code]. That way if you have any errors, you'll be able to see them.

When you do, one error you'll notice is [code=php:0]imagecreatefromstring()[/code]. The correct way to do it is [code=php:0]imagecreatefromstring(file_get_contents('background.jpg'))[/code]

good luck.
x_X Uh... this?


[table][tr][td]ID[/td][td]Username[/td][td]Message[/td][td]Timestamp[/td][/tr][tr][td]1[/td][td]Kairu [/td][td]Hello![/td][td]2006-12-30 10:56:06[/td][/tr][tr][td]1[/td][td]George[/td][td]Hi![/td][td]2006-12-30 10:57:06[/td][/tr][tr][td]1[/td][td]Lennie[/td][td]How's it going?[/td][td]2006-12-30 11:45:29[/td][/tr][/table]
Are you sure you moved the header to the second to last line? You forgot a $ in front of "i" on your first for() loop. You also neglected a semi-colon after your $text=wordwrap... line. So, you should be getting parse errors -- if you put that "header()" first, you won't see any errors at all.

anyway, after fixing those errors, and emulating your sql query, it seems to work fine.:
[code]<?php
$row=array('1','Kairu','Hello!','2006-12-30 10:56:06');
for ($i=0; $i<=1; $i++)
{
$row[$i + 1] = str_replace("\r", '', str_replace("\n", '', $row[$i + 1]));
}
$image = imagecreatefromstring(file_get_contents('background.jpg'));
$text_color = imagecolorallocate($image, 55, 55, 55);
$img = (floor(imagesy($image)/imagefontheight(2)) * imagefontheight(2)) - imagefontheight(2);
for ($i=0; $i <= 0; $i++) {
$text = $row[1] . ": " . $row[2];
$text = wordwrap($text, floor((imagesx($image)-11)/imagefontwidth(2)), "\n    ", 1);
$text = explode("\n", $text);
for ($j = (count($text) - 1); $j >= 0; $j--)
{
imagestring($image, 2, 7, $img, $text[$j], $text_color);
$img -= imagefontheight(2);
}
}
header ('Content-Type: image/gif');
imagegif($image);
?>[/code]
:o It does! Um.... Last question. How would I place multiple rows into one array? It's supposed to be able to place all three (or more) onto the image. I am aware that I have limited the for loop, and the rows going into $text But what would I do have it output more? Like, the second row in a different array? Or the same array, except make it dynamic ($array[1][1])?

PS: Thank god for On-Screen-Keyboard. Blast my need to spell and use grammar properly!
^^ It works!

Last question of the day.....

Can you think of a way to limit the area of the text? The way it is now, I can limit the bottom of the text, and either side, but not the top.... I've tried it but cant seem to get it to work.

I guess I'll post my code.

For refrence, my database is:
ID (Digit), Username (Up to 45 chars), Message(Up to 100 Chars), Timestamp (Time)

The code:
[code]<?php
header ('Content-Type: image/gif');

mysql_connect('localhost:3306', 'U', 'P');

mysql_select_db('gaia_image');

$result = mysql_query('SELECT *
FROM `box`
WHERE ID = 1
ORDER BY `box`.`Timestamp` DESC');
$txt = array();
while($row = mysql_fetch_array($result))
{
for ($i=0; $i<=1; $i++)
{
array_push($txt, str_replace("\r", '', str_replace("\n", '', $row[$i + 1])));
}
}

$image = imagecreatefromstring(file_get_contents('http://thedarkrealm.no-ip.org/shoutboxes/background.jpg'));

$text_color = imagecolorallocate($image, 55, 55, 55);

$img = (floor(imagesy($image)/imagefontheight(2)) * imagefontheight(2)) - imagefontheight(2);

for ($i=0; $i <= count($txt); $i++)
{
$text = $txt[($i*2)] . ": " . $txt[(($i*2)+1)];
$text = wordwrap($text, floor((imagesx($image)-21)/imagefontwidth(2)), "\n    ", 1);
$text = explode("\n", $text);

for ($j = count($text) - 1; $j >= 0; $j--)
{
imagestring($image, 2, 7, $img, $text[$j], $text_color);
$img = $img - imagefontheight(2);
}
}

imagegif($image);
?>[/code]
I tried this, but I cant make it work for the life of me!
[code]for ($i=0; $i <= count($txt); $i++)
{
if (count($text) >= 6)
{
$i = count($txt) +1;
}
else
{
$text = $txt[($i*2)] . ": " . $txt[(($i*2)+1)];
$text = wordwrap($text, floor((imagesx($image)-21)/imagefontwidth(2)), "\n    ", 1);
$text = explode("\n", $text);

for ($j = count($text) - 1; $j >= 0; $j--)
{
imagestring($image, 2, 7, $img, $text[$j], $text_color);
$img = $img - imagefontheight(2);
}
}
}[/code]
Hrm....

I think it would have to have something to do with that wouldn't it?...... Perhaps an if statement around where it posts.... and if it is full then it wont...... lets see....

Oh.... just realized I put up the wrong code..... x.X

[code]<?php
header ('Content-Type: image/gif');

mysql_connect('localhost:3306', 'Gaia', 'Kairu');

mysql_select_db('gaia_image');

$result = mysql_query('SELECT *
FROM `box`
WHERE ID = 1
ORDER BY `box`.`Timestamp` DESC');
$txt = array();
while($row = mysql_fetch_array($result))
{
for ($i=0; $i<=1; $i++)
{
array_push($txt, str_replace("\r", '', str_replace("\n", '', $row[$i + 1])));
}
}

$image = imagecreatefromstring(file_get_contents('http://thedarkrealm.no-ip.org/shoutboxes/background.jpg'));

$text_color = imagecolorallocate($image, 55, 55, 55);


$img = (floor(imagesy($image)/imagefontheight(2)) * imagefontheight(2)) - imagefontheight(2);

for ($i=0; $i <= count($txt); $i++)
{
$text = $txt[($i*2)] . ": " . $txt[(($i*2)+1)];
$text = wordwrap($text, floor((imagesx($image)-21)/imagefontwidth(2)), "\n    ", 1);
$text = explode("\n", $text);

for ($j = count($text) - 1; $j >= 0; $j--)
{
imagestring($image, 2, 7, $img, $text[$j], $text_color);
$img = $img - imagefontheight(2);
}
}

imagegif($image);
?>[/code]

Alright.... thinking out loud here.....

I need it to output under a certain number of lines.....
It cant output the butt end of a message if it is long, so if it's going to output the butt end then it cant.....
Uh.... Crap this is going to be hard.....

Yes, this is now partially pointless, but it gives anyone looking an idea of what my head looks like on the inside.... o.0
Theres a question.... I cant figure out how to modify my code so it can do these two extra things:
I need it to output under a certain number of lines.....
like.... if(number of lines output <= 8 ){continue} else{stop outputting lines}

It cant output the butt end of a message if it is long, so if it's going to output the butt end then it cant.....
This is the hard one.... I think it could be placed around the point it wordwraps.... but not sure at all what the code would look like.

The question?.... How would I do those two things? What would it look like? Is it possible to see a sample of what I should do?

Sorry if any of this is basic... I've been up for almost a solid day and have a headache.... I was hoping to have this done by morning so I can sleep and be ready for the new year.....
first part, just limit your SQL query to 8 "SELECT * FROM `box` WHERE ID = 1 ORDER BY `box`.`Timestamp` DESC LIMIT 0,8"

second part. [code=php:0]substr($row['comment'],-30);[/code] would just display the last 30 characters of the comment. You could also search for the closest linebreak or whitespace to 30 with a small loop.

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.