r0b0t Posted November 17, 2006 Share Posted November 17, 2006 I'm trying to write a function that creates 24bit PNG's with transparent backgrounds from strings. I used [url=http://www.alistapart.com/articles/dynatext]A List Apart's dynamic text replacement function[/url] as a starting point but the edges were aliasing on the edges didn't work with the multicolored background images on the site that the titles were intended for.I simplified and modified the code and [url=http://www.uventus.com/dynamic_text.html]the resulting image[/url] looked better around the edges of the font but left blocks of the black background generated by the imagecreatetruecolor function. I tried the function in two different environments: PHP 5.1.1/Windows XP/Apache 2/GD 2.0.28 and PHP 4.3.2/Fedora Enterprise 3/GD 2.0.12 and received the same results.Code and example image link are below. Thanks in advance for any advice.-ChrisExample Image: [url=http://www.uventus.com/dynamic_text.html]http://www.uventus.com/dynamic_text.html[/url][img]http://www.uventus.com/dyanamic_text.png[/img]Code: [code] php:function toPng($str, $font_file, $font_size, $font_color, $destination) { $font_rgb = hex_to_rgb($font_color) ; $dip = get_dip($font_file,$font_size) ; $box = imagettfbbox($font_size,0,$font_file,$str) ; $image = imagecreatetruecolor(abs($box[2]-$box[0]),abs($box[5]-$dip)) ; imagesavealpha($image, true); imagealphablending($image, false); $background_color = imagecolorallocate($image, 0, 0, 0) ; $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, $str) ; imagecolortransparent($image, $background_color) ; imagepng($image, $destination) ; imagedestroy($image) ; } function get_dip($font,$size) { $test_chars = 'abcdefghijklmnopqrstuvwxyz' . 'abcdefghijklmnopqrstuvwxyz' . '1234567890' . '!#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ; $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) { } $rgb['red'] = hexdec(substr($hex,0,2)) ; $rgb['green'] = hexdec(substr($hex,2,2)) ; $rgb['blue'] = hexdec(substr($hex,4,2)) ; return $rgb ; } ?>[/code] Link to comment https://forums.phpfreaks.com/topic/27614-dynamic-image-from-text-and-24bit-png-transparency/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.