Page 1 of 1

[SOLVED] Antialiased text on transparent png image

Posted: Wed Jun 21, 2006 7:36 am
by Symen77
Hi there,

I am trying to create a transparent png image with antialiased text on it. The text has got to have a shadow.

Code: Select all

<?
  // Set the content-type
  header("Content-type: image/png");
  
  // Create the image
  $im = imagecreatetruecolor(400, 30);
  
  // Create some colors
  $white = imagecolorallocate($im, 255, 255, 255);
  $grey = imagecolorallocate($im, 128, 128, 128);
  $black = imagecolorallocate($im, 0, 0, 0);
  imagefilledrectangle($im, 0, 0, 399, 29, $white);

  
  // The text to draw
  $text = 'Testing...';
  // Replace path by your own font path
  $font = 'arial.ttf';
  
  // Add some shadow to the text
  imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
  
  // Add the text
  imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
  
  // Using imagepng() results in clearer text compared with imagejpeg()
  imagepng($im);
  imagedestroy($im);
  ?>
This example comes directly from the PHP website, and does almost everything i need, except: The transparency

Does anyone how to modify this script to make the image transparent?

I tried using :

Code: Select all

<?
  imagecolortransparent($im, false);
?>
But could not get it to work..

Regards,

Symen

Posted: Wed Jun 21, 2006 9:29 am
by Symen77
Hi There,

Got it, It is not finished yet, still working on it.

But the basics work in this solution: (create transparent image, with antialias text & dropshadow in steps)

Code: Select all

<?php
  create_bannertext();
  
  
  /*
  * Start function
  **************************************************/
  function create_bannertext($text="Florida rentals", $typeface="arial.ttf", $textcolor="FFF", $shadowcolor="000", $shadowdistance=8)
  {
    $fontfile = "font/";
    $font     = $fontfile.$typeface;
    $h        = 20; 
    $ypad     = 5;
        
    list($r1,$g1,$b1) = hex2rgb($textcolor);
    list($r2,$g2,$b2) = hex2rgb($shadowcolor);
    
    render_banner($h, $text, $font, $shadowdistance, $r1, $g1, $b1, $r2, $g2, $b2);
  }
  
  
  /*
  * Convert short (3) or long (6) hex to RGB value
  **************************************************/
  function hex2rgb($color) 
  {
  	$color = str_replace('#','',$color);
  	$s = strlen($color) / 3;
  	$rgb[]=hexdec(str_repeat(substr($color,0,$s),2/$s));
  	$rgb[]=hexdec(str_repeat(substr($color,$s,$s),2/$s));
  	$rgb[]=hexdec(str_repeat(substr($color,2*$s,$s),2/$s));
  	return $rgb;
  }
  
  function calculate_shadowtransparency($i, $shadowdistance)
  {
    if ($i == 1)
    {
      return 0;
    }
    else
    {
      $steps = floor(127 / $shadowdistance);
      $strength = 0 + ($steps * $i);
      return $strength;
    }
  }
  
  function render_banner($h, $text, $font, $shadowdistance, $r1, $g1, $b1, $r2, $g2, $b2)
  {
    $size = imageTTFBBox($h+$shadowdistance, 0, $font, $text);
    $image = imageCreateTrueColor(abs($size[2]) + abs($size[0]), abs($size[7]) + abs($size[1]) + 20);
    imageSaveAlpha($image, true);
    ImageAlphaBlending($image, true);
    
    $textcolor    = imagecolorallocate($image, $r1, $g1, $b1);  
    //$shadowcolor  = imagecolorallocate($image, $r2, $g2, $b2);
    $tlo          = imagecolorallocatealpha($image, 220, 220, 220, 127);
    imagefill($image, 0, 0, $tlo);
    
    if (is_numeric($shadowdistance) && $shadowdistance >= 1 && $shadowdistance <=10)
    {
      for ($i=0; $i < $shadowdistance; $i++)
      {
        $shadowtransparency = calculate_shadowtransparency($i, $shadowdistance);
        $shadowcolor = imagecolorallocatealpha($image, $r2, $g2, $b2, $shadowtransparency);
        imagettftext($image, $h, 0, 0+$i, abs($size[5])+$i, $shadowcolor, $font, $text);
      }
    }
    
    imagettftext($image, $h, 0, 0, abs($size[5]), $textcolor, $font, $text);
    imagepng($image, "output.png");
    imagedestroy($image);
  }
?>
Regards,

Symen