Part of our cms, and has been for the last 2 versions, is that users can have their own fonts for the menu titles in the front-end. Not only that but they can still admin their menus from our cms, so dynamic texts and custom font?? how do you do it??
well i do it by generating a PNG image with the text in it, how do i do that? well first off you need some software, truetype font modules and such installed in apache, not going to go into that here..
Here are the functions tho
function get_dip($font,$size){
$test_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
$box = @imagettfbbox($size,0,$font,$test_chars) ;
return $box[3] ;
}
function hex_to_rgb($hex){
if(substr($hex,0,1) == '#')
$hex = substr($hex,1) ;
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)
$hex='000000';
$rgb['red'] = hexdec(substr($hex,0,2)); $rgb['green'] = hexdec(substr($hex,2,2)); $rgb['blue'] = hexdec(substr($hex,4,2));
return $rgb ;
}
function createMenuImg($file,$text=", $font_color='000000', $font_size=10, $background_color='#FFFFFF', $font_file='/fonts/din1451m.ttf'){
$send_buffer_size = 4096 ;
$text = stripslashes($text);
if(is_readable($font_file)){
$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){
$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) ;
imagecolortransparent($image,$background_color) ;
@unlink($file); @imagepng($image,$file); imagedestroy($image) ;
return true;
}else{
return false;
}
}else{
return false;
}
}
And here is how you call it
createMenuImg([savelocation and filename],[text],[text colour],[text size],[background colour],[TTF font to use]);
Example
createMenuImg('images/menu1.png','Hello world ','#000000',12,'#FFFFFF','din1451m.ttf');
Some notes
font has to be of the TTF variety
Watch out with special characters
Make sure your output dir is writable
Also interesting
As i said im using this script in the new CMS v6, now because its importent that the cms has to be easy and quick to install i need to avoid entering things like font colour in more than 1 place. So enter my previously mentioned css2array function
before making the font with the above function i first get the colours/sizes i need from the CSS file.
$fp=fopen('/public_html/style.css','r');
$css=fread($fp,filesize('/public_html/style.css'));
fclose($fp);
$css=explode('/* menu links */',$css);
$css=explode('/* searchfield */',$css[1]);
$css=$css[0]; $css = css2Array($css);
$fontSize = str_replace('px',",$css['menu:link']['font-size']);
$fontColour = $css['menu:link']['color'];
$fontMOColour = $css['menu:hover']['color'];
$backgroundColour = $css['menubackground']['background-color'];
Comments