[help] Writing text to an image with a border

Everything PHP-related! Discuss scripts, host challenges, request a script, or just ask for plain old help.

Moderators: IRC Operators, Support Team

mountaindew
User
User
Posts: 172
Joined: Sat Jun 30, 2007 9:07 pm
Contact:

[help] Writing text to an image with a border

Postby mountaindew » Fri Jan 09, 2009 9:49 pm

I'm trying to make a dynamic userbar but I can't figure out how to get a black border around the text like userbars.com get it. I'm ussing imagettftext() to write it, but I can't figure out how to get the border around it :?

Any ideas?
Zanith
IRC Operator
IRC Operator
Posts: 1082
Joined: Sun Nov 05, 2006 7:23 pm
Location: Internet
Contact:

Re: [help] Writing text to an image with a border

Postby Zanith » Sat Jan 10, 2009 1:00 pm

Code: Select all

$black = imagecolorallocate($im, 0, 0, 0);
imagerectangle($im, 1, 1, $width, $height, $black);


Where $im is your image reference and width/height is the dimensions.
http://www.scaryinter.net
Official SwiftIRC Slogon: <Ab`Couttsy> what topic
mountaindew
User
User
Posts: 172
Joined: Sat Jun 30, 2007 9:07 pm
Contact:

Re: [help] Writing text to an image with a border

Postby mountaindew » Sun Jan 11, 2009 4:26 am

I meant like a one pixel border around each letter :?
steven10172
Newbie
Newbie
Posts: 17
Joined: Tue Aug 15, 2006 1:14 am

Re: [help] Writing text to an image with a border

Postby steven10172 » Sun Jan 11, 2009 6:48 am

I think he means each letter is a certain color and it has a border that is black around it, each letter.
Zanith
IRC Operator
IRC Operator
Posts: 1082
Joined: Sun Nov 05, 2006 7:23 pm
Location: Internet
Contact:

Re: [help] Writing text to an image with a border

Postby Zanith » Mon Jan 12, 2009 3:52 pm

I had this same problem with an image I was going to make. With dynamic content, it required a really messy workaround for each character.
You can make it appear you have a border round the text by choosing your font wisely, a custom font. Try also creating a rectangle behind each character to appear similar to a border.
http://www.scaryinter.net
Official SwiftIRC Slogon: <Ab`Couttsy> what topic
User avatar
Louis
User
User
Posts: 99
Joined: Mon Oct 29, 2007 2:53 am
Contact:

Re: [help] Writing text to an image with a border

Postby Louis » Tue Jan 13, 2009 12:26 am

Here is a way you can do it. (If I understood what you asked.)
This is really sloppy, but it works. (Sorry for how messy the "config" looks.)
The font file I used was "FreeSerif", but it should work with any font.
Just edit to however you see fit. =P

EDIT: I had $font_size set twice for some reason, sorry.

Code: Select all

<?php
$im = @imagecreatetruecolor(400,50); // make the picture here (use the proper funtion if you want to use an existing picture)
imagefill($im,0,0,imagecolorallocate($im,255,255,255)); // fill the background of the picture (not needed if you use an existing picture)
$c1 = imagecolorallocate($im, 255, 0, 0); // main color
$c2 = imagecolorallocate($im, 0, 0, 0); // border color
$font_size = 30; // the size of text you want
$text = "blah blah blah 1 2 3"; // text you want to use
$font = "./FreeSerif.ttf"; // path to the font file you would like to use
$x = 5; // location X on the picture
$y = 35; // location Y on the picture
$offset = 2; // how big you want the border

/* make the border with the following for loops (MAIN PART!) */
for($a = ($x-abs($offset)); $a <= ($x+abs($offset)); $a++) {
  for($b = ($y-abs($offset)); $b <= ($y+abs($offset)); $b++) {
      imagettftext($im, $font_size, 0, $a, $b, $c2, $font, $text);
  }
}
imagettftext($im, $font_size, 0, $x, $y, $c1, $font, $text); // draw the text over the border

/* send the header, output the image, and destroy */
header("Content-type: image/png"); imagepng($im); imagedestroy($im);
?>
Last edited by Louis on Tue Jan 13, 2009 7:53 am, edited 1 time in total.
Image
Get your LastFM signature here!
mountaindew
User
User
Posts: 172
Joined: Sat Jun 30, 2007 9:07 pm
Contact:

Re: [help] Writing text to an image with a border

Postby mountaindew » Tue Jan 13, 2009 2:23 am

Syrus wrote:Here is a way you can do it. (If I understood what you asked.)
This is really sloppy, but it works. (Sorry for how messy the "config" looks.)
The font file I used was "FreeSerif", but it should work with any font.
Just edit to however you see fit. =P

Code: Select all

<?php
$im = @imagecreatetruecolor(400,50); // make the picture here (use the proper funtion if you want to use an existing picture)
imagefill($im,0,0,imagecolorallocate($im,255,255,255)); // fill the background of the picture (not needed if you use an existing picture)
$c1 = imagecolorallocate($im, 255, 0, 0); // main color
$c2 = imagecolorallocate($im, 0, 0, 0); // border color
$font_size = 30; // the size of text you want
$text = "blah blah blah 1 2 3"; // text you want to use
$font = "./FreeSerif.ttf"; // path to the font file you would like to use
$font_size = 30; // the size of text you want
$x = 5; // location X on the picture
$y = 35; // location Y on the picture
$offset = 2; // how big you want the border

/* make the border with the following for loops (MAIN PART!) */
for($a = ($x-abs($offset)); $a <= ($x+abs($offset)); $a++) {
  for($b = ($y-abs($offset)); $b <= ($y+abs($offset)); $b++) {
      imagettftext($im, $font_size, 0, $a, $b, $c2, $font, $text);
  }
}
imagettftext($im, $font_size, 0, $x, $y, $c1, $font, $text); // draw the text over the border

/* send the header, output the image, and destroy */
header("Content-type: image/png"); imagepng($im); imagedestroy($im);
?>

omg thank you so much, that's perfect :D
User avatar
Louis
User
User
Posts: 99
Joined: Mon Oct 29, 2007 2:53 am
Contact:

Re: [help] Writing text to an image with a border

Postby Louis » Tue Jan 13, 2009 8:15 am

You should come to #php on the IRC, as you would have most likely get a faster response there. =P
Image
Get your LastFM signature here!

Return to “PHP”

Who is online

Users browsing this forum: No registered users and 1 guest