By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

how to fix edges of dynamic transparent generated PNG images from text

100+
P: 245
Hey all,
I have created transparent PNG images from text dynamically. But it edges are pixel-ate or we can say edges are distorted.
Here is my LINK which shows my generated transparent PNG image. Can someone help me out to sort out my problem i will be very grateful to him as i am stuck bad with this issue and i am not figuring out how to fix this.

Here is my Code which generate transparent PNG images

Expand|Select|Wrap|Line Numbers
  1. <img src="generate-headings.php?text=Company Profile&font_color=F67538" alt="Company Profile" border="0" />
Generate-headings.php file code

Expand|Select|Wrap|Line Numbers
  1. <?
  2.     //$font_file  = 'fonts/arial.ttf' ;
  3.     //$font_file  = 'fonts/EurostileLTStd-Demi.otf';
  4.     if($_GET['font_file']){$font_file  = "fonts/".$_GET['font_file'].".ttf";}else{$font_file = 'fonts/arial.ttf';}
  5.     if($_GET['font_size']){$font_size  = $_GET['font_size'] ;}else{$font_size = 18 ;}
  6.     if($_GET['font_color']){$font_color  = '#'.$_GET['font_color'];}else{$font_color = '#ffffff';}
  7.     if($_GET['bg_color']){$background_color = '#'.$_GET['bg_color'];}else{$background_color = '#ffffff';}
  8.  
  9.     $transparent_background  = true ;
  10.     $cache_images = true ;
  11.     $cache_folder = 'cache' ;
  12.  
  13.     $mime_type = 'image/png' ;
  14.     $extension = '.png' ;
  15.     $send_buffer_size = 4096 ;
  16.  
  17.     $text = $_GET['text'];
  18.     //$text = "Mohsin";
  19.  
  20.     if(get_magic_quotes_gpc()){
  21.         $text = stripslashes($text) ;
  22.     }
  23.     $text = javascript_to_html($text) ;
  24.  
  25.     // look for cached copy, send if it exists
  26.     $hash = md5(basename($font_file) . $font_size . $font_color . $background_color . $transparent_background . $text) ;
  27.     $cache_filename = $cache_folder . '/' . $hash . $extension ;
  28.     if($cache_images && ($file = @fopen($cache_filename,'rb'))){
  29.         header('Content-type: ' . $mime_type) ;
  30.         while(!feof($file))
  31.             print(($buffer = fread($file,$send_buffer_size))) ;
  32.         fclose($file) ;
  33.         exit ;
  34.     }
  35.  
  36.     // check font availability
  37.     $font_found = is_readable($font_file) ;
  38.     if(!$font_found){
  39.         //fatal_error('Error: The server is missing the specified font.') ;
  40.     }
  41.  
  42.     // create image
  43.     $background_rgb = hex_to_rgb($background_color) ;
  44.     $font_rgb = hex_to_rgb($font_color) ;
  45.     $dip = get_dip($font_file,$font_size) ;
  46.     $box = @imagettfbbox($font_size,0,$font_file,$text);
  47.     if($_GET['width']){
  48.         $width=$_GET['width'];
  49.     }else{
  50.         $width=(abs($box[2]-$box[0])+4);
  51.     }
  52.     $image = @imagecreate($width,abs($box[5]-$dip)) ;
  53.     if(!$image || !$box){
  54.         fatal_error('Error: The server could not create this heading image.') ;
  55.     }
  56.  
  57.     // allocate colors and draw text
  58.     $background_color = @ImageColorAllocate($image,$background_rgb['red'],$background_rgb['green'],$background_rgb['blue']) ;
  59.     $font_color = ImageColorAllocate($image,$font_rgb['red'],$font_rgb['green'],$font_rgb['blue']) ;   
  60.     ImageTTFText($image,$font_size,0,-$box[0],abs($box[5]-$box[3])-$box[1],$font_color,$font_file,$text) ;
  61.  
  62.     // set transparency
  63.     if($transparent_background)
  64.         imagecolortransparent($image,$background_color) ;
  65.  
  66.     header('Content-type: ' . $mime_type) ;
  67.     ImagePNG($image) ;
  68.  
  69.     // save copy of image for cache
  70.     if($cache_images){
  71.         @imagepng($image,$cache_filename) ;
  72.     }
  73.  
  74.     imagedestroy($image) ;
  75.     exit ;
  76.  
  77.     /*$im = imagecreatetruecolor(300, 18);
  78.     $white = imagecolorallocate($im, 255, 255, 255);
  79.     $black = imagecolorallocate($im, 0, 0, 0);
  80.     imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), gd_bkg());
  81.     $text = $_GET[hd];
  82.     $font = "fonts/Abduction.ttf";
  83.     imagettftext($im,14,0,0,13,$black,$font,$text);
  84.     imagepng($im);
  85.     imagedestroy($im);
  86.  
  87.     $background_rgb = hex_to_rgb($background_color) ;
  88.     $background_color = @ImageColorAllocate($image,$background_rgb['red'],*/
  89.  
  90.  
  91.     //=============================== FUNCTIONS =================
  92.     function javascript_to_html($text){
  93.         $matches = null ;
  94.         preg_match_all('/%u([0-9A-F]{4})/i',$text,$matches) ;
  95.         if(!empty($matches)) for($i=0;$i<sizeof($matches[0]);$i++)
  96.             $text = str_replace($matches[0][$i],'&#'.hexdec($matches[1][$i]).';',$text) ;
  97.         return $text ;
  98.     }
  99.     function hex_to_rgb($hex){
  100.         // remove '#'
  101.         if(substr($hex,0,1) == '#')
  102.             $hex = substr($hex,1) ;
  103.  
  104.         // expand short form ('fff') color
  105.         if(strlen($hex) == 3){
  106.             $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) ;
  107.         }
  108.  
  109.         if(strlen($hex) != 6)
  110.             fatal_error('Error: Invalid color "'.$hex.'"') ;
  111.  
  112.         // convert
  113.         $rgb['red'] = hexdec(substr($hex,0,2)) ;
  114.         $rgb['green'] = hexdec(substr($hex,2,2)) ;
  115.         $rgb['blue'] = hexdec(substr($hex,4,2)) ;
  116.  
  117.         return $rgb ;
  118.     }
  119.     function get_dip($font,$size){
  120.         $test_chars = 'abcdefghijklmnopqrstuvwxyz' .
  121.                       'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
  122.                       '1234567890' .
  123.                       '!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
  124.         $box = @ImageTTFBBox($size,0,$font,$test_chars) ;
  125.         return $box[3] ;
  126.     }
  127. ?>
  128.  
kind regards,
Mohsin Rafique
Dec 27 '09 #1
Share this Question
Share on Google+
2 Replies


dlite922
Expert 100+
P: 1,584
Make your background color the same as what the background of the image is, in your link it's black, so set this to black

Expand|Select|Wrap|Line Numbers
  1. else{$background_color = '#000;}

Dan
Dec 28 '09 #2

100+
P: 245
i dont want to use Background color. My background color is not soild it's gradiant and it vary from page to page. I want to generate transparent PNG
Dec 30 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.