472,371 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,371 software developers and data experts.

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

245 100+
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
2 3360
dlite922
1,584 Expert 1GB
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
neovantage
245 100+
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

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

Similar topics

2
by: Astra | last post by:
Hi All I know the subject may sound weird so hopefully the below will explain. 1) I'm creating an estate agent web site, which will display a 1 x jpg pic and say 4 text descriptions for each...
8
by: djmanmaster | last post by:
Am I right in thinking that images with transparent areas (GIFs or PNGs) should allow clicks to "pass through" to any underlying elements behind them? I have had no luck getting this to work in...
4
by: Dale | last post by:
I am creating GIF images with transparent backgrounds on-the-fly for a web app and rendering them by using System.Drawing.Image.Save(Response.OutputStream, ImageType.GIF). I am confident that...
1
by: Jeff | last post by:
Hey ASP.NET 2.0 I'm wondering if it's possible to create dynamic transparent images in asp.net 2.0? This is some code (see below) from my web project. In this code below I create a...
9
by: Chuck Anderson | last post by:
Is it possible to overlay a transparent watermark on an image - dynamically? I'd like the result to look like this example: <http://www.cycletourist.com/temp/photo.php> That is a bit of...
1
by: avicalc | last post by:
I am using HTML checkboxes and JavaScript to toggle on and off, a number of absolutely positioned transparent images located on directly top of each other with different z-indexes. In addition, all...
7
by: active | last post by:
In control panel/Display/Appearance/Effects if : 'Use the following method to smooth edges of screen fonts' is checked and ClearType is selected in the combobox (no problem if Standard is...
1
by: gau.tai | last post by:
I have a form in which I am generating dynamic fields. I can do this fine, but when I go to validate them there are errors. The non- dynamic fields validate correctly. Below is the html and js. ...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.