/    Sign up×
Community /Pin to ProfileBookmark

Image Border – GD Library

I have a page that allows the user to specify a font, text, and size. Then the page displays it in a .png image for the font,text,and size they wanted. The image is transparent except for the text which is black. I want to be able to specify a border around the text.

Is there a way to tell PHP to look for transparent pixels and colored (black) pixels and create 2 border (red for instance) pixels on the transparent side? All that I am using is: ImageColorTransparent($image,$background_color); with the background_color being white.

I’ve reading books on the GD library and this is my first attempt so any help is appreciated!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@bokehJan 08.2007 — If you do that you will end up with a severly aliased image.

If you still want to do this run through the image with a double for loop (horizontal and vertical) pixel by pixel. Using imagecolorat() read the transparency of each pixel. If it is transparent read the eight surrounding pixels using the same function. If any are black change the color of the current pixel.
Copy linkTweet thisAlerts:
@LocalHeroauthorJan 08.2007 — Thanks for the reply, I'm not too worried about the aliased image. On my page it is only a rough sample to be viewed. I'm really new to the GD library but I understand how you suggest to solve it. Here is some of the code that I came up with:
[code=php]if($transparent_background)
ImageColorTransparent($image,$background_color) ;

header('Content-type: ' . $mime_type) ;
ImagePNG($image) ;

$pxwidth = imagesx($image);
$pxheight = imagesy($image);
$previous_color = 0;

for($yy = 0;$yy < $pxheight;++$yy){

for($xx = 0;$xx < $pxwidth;++$xx){

$current_color = ImageColorAt($image, $xx, $yy);

if($current_color !== $previous_color && $xx < $pxwidth-1){
#change $previous_color pixel to red???
}
}
}[/code]

I think I got a start, but I've probably been staring at the same page for too long. I'm not sure exactally how I should change the previous transparent pixel ? Could I trouble you to get me through the next step?

Thanks!
Copy linkTweet thisAlerts:
@LocalHeroauthorJan 08.2007 — I added [B]imagesetpixel ( $image, $xx, $yy, FF0);[/B] after the pixel color check hoping it would show me something.

It doesn't do anything, the image still gets created. I guess I didn't expect it to be that easy. My logic seems right I think. I check every x in each y row. If the first color isn't like the next, change it to red. But I know with PHP if one thing is out of place nothing happens. Any Ideas?
Copy linkTweet thisAlerts:
@bokehJan 08.2007 — Can you post an image so I can see what you are working on.
Copy linkTweet thisAlerts:
@LocalHeroauthorJan 08.2007 — Sure, the page I'm working on is www.localheroclothing.com/images/jerseyfonts.php

I want to be able to control a border around the name and number. The Page is Here:
[code=php]$fontype = $_POST['newfont'];
$fonttext = $_POST['newtext'];
$numfontype = $_POST['numfontype'];
$numfonttext = $_POST['numfonttext'];
$numfontsize = $_POST['numfontsize'];
$jerseynumber = $_POST['jerseynumber'];
$jerseyname = $_POST['jerseyname'];
<img
src="http://www.localheroclothing.com/images/fontchange1.php?time='.microtime()'&text=<? echo($fonttext);?>&font=<? echo($fontype);?>&color=000&bg_color=EFEFEF&size=<? echo($fontsize);?>"
alt="localhero" width="140" height="34" class="<?php if( !$_POST['newfont'] || !$_POST['newtext']) echo('hideme');?>"
>[/code]


The Images calls a script from another file.

Here
[code=php]<?php session_start();
/*
Dynamic Heading Generator
By Stewart Rosenberger
http://www.stewartspeak.com/headings/

*/

$textcolor = $_SESSION['textcolor'];
$backcolor = $_SESSION['backcolor'];
define('FONTPATH', '/www/htdocs/images/fonts/');
$font_file = FONTPATH.$_GET['font'];

$font_size = $_GET['size'];
$font_color = $_GET['color'];
$background_color = $_GET['bg_color'];
$transparent_background = true ;
$cache_images = false ;
$cache_folder = '' ;
$mime_type = 'image/png' ;
$extension = '.png' ;
$send_buffer_size = 4096 ;

// check for GD support
if(!function_exists('ImageCreate'))
fatal_error('Error: Server does not support PHP image generation') ;

// clean up text
if(empty($_GET['text']))
fatal_error('Error: No text specified.') ;

$text = $_GET['text'] ;
if(get_magic_quotes_gpc())
$text = stripslashes($text) ;
$text = javascript_to_html($text) ;

// check font availability
$font_found = is_readable($font_file) ;
if(!$font_found)
{
fatal_error('Error: The server is missing the specified font.') ;
}

// create image
$background_rgb = hex_to_rgb($background_color) ;
$font_rgb = hex_to_rgb($font_color) ;
$dip = get_dip($font_file,$font_size) ;
$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
$image = @ImageCreate(abs($box[2]-$box[0]),abs($box[5]-$dip)) ;
if(!$image || !$box)
{
fatal_error('Error: The server could not create this heading image.') ;
}

// allocate colors and draw text
$background_color = @ImageColorAllocate($image,$background_rgb['red'],
$background_rgb['green'],$background_rgb['blue']) ;
$font_color = ImageColorAllocate($image,$font_rgb['red'],
$font_rgb['green'],$font_rgb['blue']) ;

ImageTTFText($image,$font_size,0,-$box[0],abs($box[5]-$box[3])-$box[1],
$font_color,$font_file,$text) ;

// set transparency
if($transparent_background)
ImageColorTransparent($image,$background_color) ;

header('Content-type: ' . $mime_type) ;
ImagePNG($image) ;

$pxwidth = imagesx($image);
$pxheight = imagesy($image);
$red = imagecolorallocate($image, 255, 0, 0);

for($yy = 0;$yy < $pxheight;++$yy){

for($xx = 0;$xx < $pxwidth;++$xx){
$previous_color = $current_color;
$current_color = ImageColorAt($image, $xx, $yy);

if($current_color !== $previous_color && $xx < $pxwidth-1){
imagesetpixel ( $image, $xx, $yy, $red);
}
}

}

// save copy of image for cache
#if($cache_images)
#{
# @ImagePNG($image,$cache_filename) ;
#}
#
ImageDestroy($image) ;
exit ;


/*
try to determine the "dip" (pixels dropped below baseline) of this
font for this size.
*/
function get_dip($font,$size)
{
$test_chars = 'abcdefghijklmnopqrstuvwxyz' .
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
'1234567890' .
'!@#$%^&*()'"\/;.,`~<>[]{}-+_-=' ;
$box = @ImageTTFBBox($size,0,$font,$test_chars) ;
return $box[3] ;
}


/*
attempt to create an image containing the error message given.
if this works, the image is sent to the browser. if not, an error
is logged, and passed back to the browser as a 500 code instead.
*/
function fatal_error($message)
{
// send an image
if(function_exists('ImageCreate'))
{
$width = ImageFontWidth(5) * strlen($message) + 10 ;
$height = ImageFontHeight(5) + 10 ;
if($image = ImageCreate($width,$height))
{
$background = ImageColorAllocate($image,255,255,255) ;
$text_color = ImageColorAllocate($image,0,0,0) ;
ImageString($image,5,5,5,$message,$text_color) ;

header('Content-type: image/png') ;
ImagePNG($image) ;
ImageDestroy($image) ;
exit ;
}
}

// send 500 code
header("HTTP/1.0 500 Internal Server Error") ;
print($message) ;
exit ;
}


/*
decode an HTML hex-code into an array of R,G, and B values.
accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
*/

function hex_to_rgb($hex)
{
// remove '#'
if(substr($hex,0,1) == '#')
$hex = substr($hex,1) ;

// expand short form ('fff') color
if(strlen($hex) == 3)
{
$hex = substr($hex,0,1) . substr($hex,0,1) .
substr($hex,1,1) . substr($hex,1,1) .
substr($hex,2,1) . substr($hex,2,1) ;
}

if(strlen($hex) != 6)
fatal_error('Error: Invalid color "'.$hex.'"') ;

// convert
$rgb['red'] = hexdec(substr($hex,0,2)) ;
$rgb['green'] = hexdec(substr($hex,2,2)) ;
$rgb['blue'] = hexdec(substr($hex,4,2)) ;

return $rgb ;
}


/*
convert embedded, javascript unicode characters into embedded HTML
entities. (e.g. '%u2018' => '‘'). returns the converted string.
*/
function javascript_to_html($text)
{
$matches = null ;
preg_match_all('/%u([0-9A-F]{4})/i',$text,$matches) ;
if(!empty($matches)) for($i=0;$i<sizeof($matches[0]);$i++)
$text = str_replace($matches[0][$i],
'&#'.hexdec($matches[1][$i]).';',$text) ;

return $text ;
}

?>[/code]


I've tried the suggestion under "//set transparency". Thanks a lot for taking a look. ?
Copy linkTweet thisAlerts:
@LocalHeroauthorJan 09.2007 — I received a couple of suggestions for some of my silly mistakes. I've fixed the code above but it still doesn't work.

Fixes:
[code=php]$current_color = ImageColorAt($image, $xx, $yy);
would need to be
PHP Code:
$previous_color = $current_color;
$current_color = ImageColorAt($image, $xx, $yy);
And
PHP Code:
imagesetpixel ( $image, $xx, $yy, imagecolorallocate($previous_color, 255, 0, 0));
would need to be
PHP Code:
imagesetpixel ( $image, $xx, $yy, $red);

PHP Code:
$red = imagecolorallocate($image, 255, 0, 0); [/code]
×

Success!

Help @LocalHero spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...