Jump to content

GD Library Help


Local Hero

Recommended Posts

Hello,
This is about my first attempt at the GD library. I am allowing the user to type their name in a font to display on a jersey. www.localheromx.com/designlab.php and I would like to allow for a pixel outline around the text. It had been suggested to me that I try to search the image for colors that are different and change the color when I find it. My code looks close, but it may need some tweaking and it may be in the wrong spot. Can anyone get me going again? Thanks
The page calls an image like this:
[code]<img src="http://www.localheroclothing.com/images/fontchange1.php?time='.microtime()'&text=<? echo($numfonttext);?>&font=<? echo($numfontype);?>&color=<? echo($numfontcolor);?>&bg_color=<? echo($numfontcolor);?>&size=<? echo($numfontsize);?>">[/code]and the font/image script is:
[code]<?php session_start();
$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) ;

// look for cached copy, send if it exists
#$hash = md5(basename($font_file) . $font_size . $font_color .
#            $background_color . $transparent_background . $text) ;
#$cache_filename = $cache_folder . '/' . $hash . $extension ;
#if($cache_images && ($file = @fopen($cache_filename,'rb')))
#{
#    header('Content-type: ' . $mime_type) ;
#    while(!feof($file))
#        print(($buffer = fread($file,$send_buffer_size))) ;
#  fclose($file) ;
#    exit ;
#}

// 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) ;

// this is where I try and create an outline, but it won't do anything
      $pxwidth = imagesx($image);
      $pxheight = imagesy($image);
      $red = imagecolorallocate($image, 255, 0, 0);
     
      for($yy = 0;$yy < $pxheight;++$yy){
         
          for($xx = 0;$xx < $pxwidth;++$xx){

              $current_color = ImageColorAt($image, $xx, $yy);
                $previous_color = $current_color;
                   
              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' => '&#8216;'). 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]
Link to comment
https://forums.phpfreaks.com/topic/34102-gd-library-help/
Share on other sites

I think the position of $previous_color = $current_color;
is wrong on your code.. it should be after the conditional statement
try the code below.. hope that helps
[CODE]
$pxwidth = imagesx($image);
      $pxheight = imagesy($image);
      $red = imagecolorallocate($image, 255, 0, 0);
     
      for($yy = 0;$yy < $pxheight;++$yy){
          $previous_color = ImageColorAt($image, 0, $yy);   
          for($xx = 1;$xx < $pxwidth;++$xx){
               
                $current_color = ImageColorAt($image, $xx, $yy);
               
                   
                      if($current_color !== $previous_color && $xx < $pxwidth-1)
{
                      imagesetpixel ( $image, $xx, $yy, $red);
              }
                       
    $previous_color = $current_color;
                        }

      }
[/CODE]
Link to comment
https://forums.phpfreaks.com/topic/34102-gd-library-help/#findComment-160395
Share on other sites

SUCCESS!!!! I have been working on that single problem for weeks. I tried your mod and nothing happened, then I moved the code just after the image is created (before the transparancy) and it worked. I do have a couple of tweaks I'd like your ideas on...
Right now I have 1 px on the left and right, but not top and bottom. I know that I have to do another double for() checking up and down. What's the easiest way to put that in? Just copy & paste with the $xx and $yy changed?
I also wanted a thicker border, maybe 3 or 4 px but only on the transparent side. If it goes on the text side it might cover the text. Any ideas on how to tell it if it finds a pixel that isn't the same as $previous the make the last 3 $red?

Thanks again
www.localheromx.com/designlab.php
Link to comment
https://forums.phpfreaks.com/topic/34102-gd-library-help/#findComment-160592
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.