...

View Full Version : Help with changing font of image generated by php



taz3r
05-14-2008, 10:38 PM
Hey! I'm new here but I seem to be having trouble changing the font by the image generated by this php script:


preg_match_all('/<dd><div><p>([^<]+)<\/p><p><a href="\/gwt\/[^"]+">([^<]+)<\/a><\/p>\\s*(.*?)\\s*<\/div><\/dd>/i',file_get_contents('http://www.google.com/gwt/n?u=http%3A%2F%2Fwww.gaiaonline.com%2Fprofiles%2F%3Fmode%3Dcomments%26u%3D433232&_gwt_noimg=1'),$regs,PREG_SET_ORDER);
$im=imagecreatetruecolor(275,500);
imagesavealpha($im,true);
$transparent=imagecolorallocatealpha($im,0,0,0,127);
imagefill($im,0,0,$transparent);
$messagecolor=imagecolorallocate($im,0,0,0);
$username=imagecolorallocate($im,0,0,0);
$timestamp=imagecolorallocate($im,0,0,0);
$linecolor=imagecolorallocate($im,0,0,0);
$line=2;
for($i=0;$i<count($regs);$i++)
{
if($line<500-imagefontheight(2)*2+5)
{
imagestring($im,2,2,$line,$regs[$i][2],$username);
imagestring($im,2,275-2-strlen($regs[$i][1])*imagefontwidth(2),$line,$regs[$i][1],$timestamp);
$line+=imagefontheight(2);
imageline($im,2,$line+2,275-2,$line+2,$linecolor);
$line+=4;
if(preg_match('/<br[^>]*>/i',$regs[$i][3]))
{
$message=preg_split('/<br[^>]*>/i',$regs[$i][3]);
}
else
{
$message=array($regs[$i][3]);
}
for($j=0;$j<count($message);$j++)
{
$submessage=explode('LELINEBREAK',wordwrap(preg_replace('/icon_biggrin\\.gif|icon_smile\\.gif|icon_redface\\.gif|icon_crying\\.gif|icon_stare\\.gif|icon_screa m\\.gif|icon_gonk\\.gif|icon_blaugh\\.gif|icon_3nodding\\.gif|icon_xd\\.gif|icon_stressed\\.gif|icon _sweatdrop\\.gif|icon_heart\\.gif|icon_domokun\\.gif|icon_xp\\.gif|icon_eek\\.gif|icon_surprised\\.g if|icon_sad\\.gif|icon_wink\\.gif|icon_whee\\.gif|icon_confused\\.gif|icon_cool\\.gif|icon_lol\\.gif |icon_mad\\.gif|icon_razz\\.gif|icon_exclaim\\.gif|icon_rolleyes\\.gif|icon_twisted\\.gif|icon_evil\ \.gif|icon_cry\\.gif|icon_question\\.gif|icon_idea\\.gif|icon_arrow\\.gif|icon_neutral\\.gif|icon_mr green\\.gif|icon_talk2hand\\.gif|icon_pirate\\.gif|icon_rofl\\.gif|icon_4laugh\\.gif|icon_ninja\\.gi f|burning_eyes\\.gif|cheese_whine\\.gif|dramallama\\.gif|wahmbulance\\.gif|emo\\.gif|/i','',html_entity_decode(strip_tags($message[$j]))),floor(275/imagefontwidth(2))-1,'LELINEBREAK',true));
for($k=0;$k<count($submessage);$k++)
{
if($line<500-imagefontheight(2)+1)
{
imagestring($im,2,2,$line,$submessage[$k],$messagecolor);
$line+=imagefontheight(2);
}
}
}
$line+=4;
}
}
header('content-type:image/png');
imagepng($im);

Can someone give me a hand?

Fou-Lu
05-15-2008, 01:49 AM
Can you be more specific as to what you are trying to do and what it is doing incorrectly? I'm not certain if you're looking at what it should generate from the given string or if you are referring to something like the font family. Can you expand a little please?

taz3r
05-17-2008, 04:38 PM
Well, basicly, this script generates this: Link (http://designspasm.communiful.com/tazerstuffdonottouch/test.php)

As you can see, the font used in that generated image is the default. I'm trying to change the font using something like $font = imageloadfont("example.gdf");
However, I'm not sure if that is the correct code or where to place it.

Fou-Lu
05-17-2008, 05:55 PM
Ok I see it is the font set then you are working with.
imageloadfont is the function you want to use, and the font in use needs to be in a location that the interpreter can access.
Load it before you run an imagestring, and place it as the second parameter in the method:


...
$font = imageloadfont('example.gdf');
...
$linecolor=imagecolorallocate($im,0,0,0);
$line=2;
for($i=0;$i<count($regs);$i++)
{
if($line<500-imagefontheight(2)*2+5)
{
imagestring($im,$font,2,$line,$regs[$i][2],$username);
imagestring($im,$font,275-2-strlen($regs[$i][1])*imagefontwidth(2),$line,$regs[$i][1],$timestamp);
...

Try that out, and let us know if that helps!

taz3r
05-19-2008, 10:21 PM
Thanks! It worked, but only for the username.
What if I wanted the message to change font?

Fou-Lu
05-20-2008, 03:30 AM
Everytime you run an imagestring call, all you need to do is pass the variable that has been assigned to the font load. In our case, it was $font.
I'm winging it here (been awhile since I created images), but I'm pretty sure the parameter list is:
imagestring($imageHandle, $font, $height, $width, $text, $fontColour);
The height and width may be backwards, idk for certain.

For your actual line of code, just do the exact change that you did with the first two parts:


imagestring($im, $font,2,$line,$submessage[$k],$messagecolor);
// Change here ^

Hope that helps!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum