PCTechTalkhttp://www.pctechtalk.com/forums/

Go Back   PCTechTalk > PC Tech > Web Related

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2004, 04:26 PM   #1 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
PHP Script Problem

Hi all,

ive been trying to get a script working
one that writes text onto an image
but it doesnt, it just shows the image.

http://www.colin-uk.com/scripts/capt...php?text=hello

if you go to that url its ment to say "hello" on the sign

heres the code im using:

PHP Code:
header('Content-type: image/png');

$image imagecreatefrompng("familyguy.png");

$white imagecolorallocate($image255255255);
imagettftext($image70270380$white"annifont.ttf""TEXT HERE");                                 

imagepng($image);

?> 
any ideas whats wrong?

thanks
-c0lin
Colin-uk is offline   Reply With Quote
Old 02-04-2004, 03:20 AM   #2 (permalink)
Da House Nerd
 
greffov's Avatar
 
Join Date: Dec 1969
Location: One CPU Lane
Posts: 3,512
greffov will become famous soon enough
1. it can be that PHP can't find the font you list (use absolute location or copy the ttf into the location the script also is (ie find out relative path)

2. the given x and y coordinates can be out of the image scope (don't know that since I'm too lazy to look up the image size)

3. This function requires both the GD library and the FreeType library. Make sure you have them both

tip: destroy the image after sending it using imagedestroy($image);
__________________
Linux virusscanner detected a virus:
Windows 95 ... delete [Y/n] y
~
~

:wq
greffov is offline   Reply With Quote
Old 02-04-2004, 04:44 AM   #3 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
thanks for the reply greffov,

the font is in the same directory as the script so that shouldnt be a problem
the GD image library is installed as i asked my host about it a few days ago, not sure about the freetype library though. will have to ask
i,ll try changing the co-ordinates and see if that makes any diffrence.
and thanks for the tip
Colin-uk is offline   Reply With Quote
Old 02-04-2004, 12:51 PM   #4 (permalink)
Da House Nerd
 
greffov's Avatar
 
Join Date: Dec 1969
Location: One CPU Lane
Posts: 3,512
greffov will become famous soon enough
maybe better start using the PHP example, and/or with coordinates 0,0 (top left corner)

Let us know if you get it to work!
__________________
Linux virusscanner detected a virus:
Windows 95 ... delete [Y/n] y
~
~

:wq
greffov is offline   Reply With Quote
Old 02-04-2004, 01:17 PM   #5 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
Well i got it to work

See here

but i have another question/problem now

it works great for short words but, if i use long words then it goes off the edge of the image.
is there anyway i can wrap it onto a new line?

Click here for a long text example

and can i stop the slanting of the text? it makes it look unrealistic lol

thanks
Colin-uk is offline   Reply With Quote
Old 02-04-2004, 03:33 PM   #6 (permalink)
Da House Nerd
 
greffov's Avatar
 
Join Date: Dec 1969
Location: One CPU Lane
Posts: 3,512
greffov will become famous soon enough
all nice and well, but couldn't you think of anything else like this?


About long lines... I really think you are to be creative there yourself.
A possible approach would be defining the max characters that fit in the board, then split the text after each x characters, respecting word boundaries. That means if on char x is not a space you start looking backwards for the first space. If you can't find any, then just split on position x.

suppose your max is 6, and your sentence is hello my name is collin, then the split would be:
hello
my
name
is
collin
But suppose your sentence was, hello I am rediculous, then the split would be:
hello
I am
redicu
lous

The slanting is caused by the first argument (a 7), which defines the degrees of rotation. Do you ever RTFM? LOL
__________________
Linux virusscanner detected a virus:
Windows 95 ... delete [Y/n] y
~
~

:wq
greffov is offline   Reply With Quote
Old 02-04-2004, 05:59 PM   #7 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
lol yeah i do RTFM
(i didnt know if it was just the font style lol)

i found a function called wordwrap in the php manual
but i dunno if i could use that some how...

heres what i got so far

PHP Code:
<?php

header
('Content-type: image/png');

$image imagecreatefrompng("familyguy.png");

$black imagecolorallocate($image000);
imagettftext($image01040150$black"annifont.ttf"$text);                                 

$newtext wordwrap($text16"<br />");

imagepng($image);

?>
but it only shows the image with no text
Colin-uk is offline   Reply With Quote
Old 02-05-2004, 05:10 AM   #8 (permalink)
Da House Nerd
 
greffov's Avatar
 
Join Date: Dec 1969
Location: One CPU Lane
Posts: 3,512
greffov will become famous soon enough
hmmm, wait, I should rtfm too...
the first argument was the font size...

array imagettftext ( resource image, int size, int angle, int x, int y, int color, string fontfile, string text)

wordwrap sounds like a function that does what I described... cool, try and see what it does.
__________________
Linux virusscanner detected a virus:
Windows 95 ... delete [Y/n] y
~
~

:wq
greffov is offline   Reply With Quote
Old 02-06-2004, 08:09 AM   #9 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
it just shows the image with no text,
would the code above work?
Colin-uk is offline   Reply With Quote
Old 02-06-2004, 02:04 PM   #10 (permalink)
Da House Nerd
 
greffov's Avatar
 
Join Date: Dec 1969
Location: One CPU Lane
Posts: 3,512
greffov will become famous soon enough
fontsize = 0 means the text it unreadable
__________________
Linux virusscanner detected a virus:
Windows 95 ... delete [Y/n] y
~
~

:wq
greffov is offline   Reply With Quote
Old 02-07-2004, 12:32 PM   #11 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
oops...

well i tried it with this code:

PHP Code:
<?php

header
('Content-type: image/png');

$image imagecreatefrompng("familyguy.png");

$black imagecolorallocate($image000);
imagettftext($image15040150$black"annifont.ttf"$text);

$newtext wordwrap($text16"<br />");


imagepng($image);

?>
and this is what i got:

www.colin-uk.com/scripts/captions/familyguy.php?text=this is a very long sentence
(copy and paste url)

so word wrap doesnt seem to work then...

Last edited by c0lin; 02-07-2004 at 12:38 PM.
Colin-uk is offline   Reply With Quote
Old 02-07-2004, 02:00 PM   #12 (permalink)
Da House Nerd
 
greffov's Avatar
 
Join Date: Dec 1969
Location: One CPU Lane
Posts: 3,512
greffov will become famous soon enough
LOL, colin, forgive me lauging, but... you are wordwrapping AFTER you made the image!!!

FIRST do the wordwrap (and use "\n" instead of "<br />", for we're not in HTML now), THEN generate the image, like:

PHP Code:
$text wordwrap($text16"\n");
imagettftext($image15040150$black"annifont.ttf"$text); 
__________________
Linux virusscanner detected a virus:
Windows 95 ... delete [Y/n] y
~
~

:wq
greffov is offline   Reply With Quote
Old 02-07-2004, 03:18 PM   #13 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
i thought it was the imagepng command that made the image?

the php manual says the "\n" and "<br />" are the same so i just used that one

i tried what u suggessted but now i just get a blank image with no text...
Colin-uk is offline   Reply With Quote
Old 02-07-2004, 03:50 PM   #14 (permalink)
Da House Nerd
 
greffov's Avatar
 
Join Date: Dec 1969
Location: One CPU Lane
Posts: 3,512
greffov will become famous soon enough
uhm, yes, you're right about the imagepng, but the imagettftext command draws the text in the image, which I referred to as 'make'.

I assume you only used my excerpt code. You needed all lines, but those two in that order. This is the complete code:
PHP Code:
<?php
header
('Content-type: image/png');

$image imagecreatefrompng("familyguy.png");

$black imagecolorallocate($image000);

$text wordwrap($text16"\n");
imagettftext($image15040150$black"annifont.ttf"$text);

imagepng($image);
?>
<br /> and \n might in some cases have the same impact, I didn't RTFM of wordwrap, but they sure AREN'T the same, lol.
__________________
Linux virusscanner detected a virus:
Windows 95 ... delete [Y/n] y
~
~

:wq
greffov is offline   Reply With Quote
Old 02-08-2004, 04:53 AM   #15 (permalink)
Registered User
 
Colin-uk's Avatar
 
Join Date: Jan 2003
Location: UK
Posts: 928
Colin-uk is on a distinguished road
Woo hoo it works

See here

Thanks a lot for all the help lol


Now to go and find some more people holding signs lol....

Last edited by c0lin; 02-08-2004 at 05:02 AM.
Colin-uk is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 07:24 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
2001 PCTechTalk