Stroke around text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Lucvh
Forum Newbie
Posts: 5
Joined: Thu Jun 02, 2005 5:08 pm

Stroke around text

Post by Lucvh »

This code, gets a file, and writes some text over the top.

Code: Select all

<?php
//post headers. Tells client file is type gif
header("Content-type: image/jpeg");
//load in the text using GET. 'text' is a variable in the GET method
$string = $_GET['text'];
$string2 = $_GET['text2'];
//Load the image
$im    = imagecreatefromjpeg("riots.jpg");
//make a variable to set the text color
$blue = imagecolorallocate($im, 0x78, 0x74, 0x8f);
$red = imagecolorallocate($im, 0xaf, 0x33, 0x3c);
//load the font
$font = 'gf.ttf';
$font2 = 'ang.ttf';
//draw the text onto the image
imagettftext($im, 20, 0, 110, 680, $blue, $font, $string);
imagettftext($im, 210, 0, 50, 600, $red, $font2, $string2);
//print the image
imagegif($im);
//delete the evidence
imagedestroy($im);
?>
Is there any way that i can code it so that it draws a stroke around the $string2 text?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

I dont know how you can do this using Font properties like underline, bold, stroke etc.
You know (x,y) => (50, 600)

Use imagettfbbox() to get the width and height of the font.
Use imageline to draw a line -
(50, 600 + height/2) to (50 + width, 600 + height/2)
Lucvh
Forum Newbie
Posts: 5
Joined: Thu Jun 02, 2005 5:08 pm

Post by Lucvh »

I dont't mean a stroke through the text, i mean around the outline of the text.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Same method :
imagerectangle() - (50, 600) to (width, height)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

You mean like making a coloured line follow the contours of the individual letters? I've never seen that done - it sounds quite complicated.

The only way I could think of (although I'm by no means a GD guru) would be to do it letter by letter. Create a letter +2 font size (or whatever), find the pixel of the exact center of that letter, and place the inner coloured leter at those co-ordinates.

Personally if that's the only way to do it, I'd forget it and move on.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

the general straight on method for doing this is very simple, however not so fast and not accurate, but at least gives you an outline... and it is really simple, you want 1px border? then you draw the text 8 times, each time you position it at most 1px from the origin. such as N NE E SE S SW W NE, and then on that you draw in the text again using the inner color. and voila you have an outline, (don't know, but I believe if you keep it to angles of length 1 instead you might get better result.)

2px needs however a lot of more draws and so on, but you get the point.
Post Reply