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
Generate-headings.php file code
kind regards,
Mohsin Rafique
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
Code:
<img src="generate-headings.php?text=Company Profile&font_color=F67538" alt="Company Profile" border="0" />
Code:
<?
//$font_file = 'fonts/arial.ttf' ;
//$font_file = 'fonts/EurostileLTStd-Demi.otf';
if($_GET['font_file']){$font_file = "fonts/".$_GET['font_file'].".ttf";}else{$font_file = 'fonts/arial.ttf';}
if($_GET['font_size']){$font_size = $_GET['font_size'] ;}else{$font_size = 18 ;}
if($_GET['font_color']){$font_color = '#'.$_GET['font_color'];}else{$font_color = '#ffffff';}
if($_GET['bg_color']){$background_color = '#'.$_GET['bg_color'];}else{$background_color = '#ffffff';}
$transparent_background = true ;
$cache_images = true ;
$cache_folder = 'cache' ;
$mime_type = 'image/png' ;
$extension = '.png' ;
$send_buffer_size = 4096 ;
$text = $_GET['text'];
//$text = "Mohsin";
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);
if($_GET['width']){
$width=$_GET['width'];
}else{
$width=(abs($box[2]-$box[0])+4);
}
$image = @imagecreate($width,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) ;
// save copy of image for cache
if($cache_images){
@imagepng($image,$cache_filename) ;
}
imagedestroy($image) ;
exit ;
/*$im = imagecreatetruecolor(300, 18);
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, imagesx($im), imagesy($im), gd_bkg());
$text = $_GET[hd];
$font = "fonts/Abduction.ttf";
imagettftext($im,14,0,0,13,$black,$font,$text);
imagepng($im);
imagedestroy($im);
$background_rgb = hex_to_rgb($background_color) ;
$background_color = @ImageColorAllocate($image,$background_rgb['red'],*/
//=============================== FUNCTIONS =================
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 ;
}
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 ;
}
function get_dip($font,$size){
$test_chars = 'abcdefghijklmnopqrstuvwxyz' .
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
'1234567890' .
'!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
$box = @ImageTTFBBox($size,0,$font,$test_chars) ;
return $box[3] ;
}
?>
Mohsin Rafique
Comment