How to convert a font to an image (PNG) using php

Post Reply
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

How to convert a font to an image (PNG) using php

Post by Neo » Fri Apr 02, 2010 3:04 pm

You can use following code to do that. To draw the PNG - call this script with a URL like the following:

Code: Select all

http://www.yoursite.com/text_png.php3?msg=testing+class&rot=15&size=48&font=fonts/ARIAL.TTF

Code: Select all

<?
Header("Content-type: image/png");

class textPNG {
        var $font = 'fonts/TIMES.TTF'; //default font. directory relative to script directory.
        var $msg = "undefined"; // default text to display.
        var $size = 24;
        var $rot = 0; // rotation in degrees.
        var $pad = 0; // padding.
        var $transparent = 1; // transparency set to on.
        var $red = 0; // white text...
        var $grn = 0;
        var $blu = 0;
        var $bg_red = 255; // on black background.
        var $bg_grn = 255;
        var $bg_blu = 255;

function draw() {
        $width = 0;
        $height = 0;
        $offset_x = 0;
        $offset_y = 0;
        $bounds = array();
        $image = "";
        
        // determine font height.
        $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
        if ($this->rot < 0) {
                $font_height = abs($bounds[7]-$bounds[1]);                
        } else if ($this->rot > 0) {
                $font_height = abs($bounds[1]-$bounds[7]);
        } else {
                $font_height = abs($bounds[7]-$bounds[1]);
        }

        // determine bounding box.
        $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
        if ($this->rot < 0) {
                $width = abs($bounds[4]-$bounds[0]);
                $height = abs($bounds[3]-$bounds[7]);
                $offset_y = $font_height;
                $offset_x = 0;
                
        } else if ($this->rot > 0) {
                $width = abs($bounds[2]-$bounds[6]);
                $height = abs($bounds[1]-$bounds[5]);
                $offset_y = abs($bounds[7]-$bounds[5])+$font_height;
                $offset_x = abs($bounds[0]-$bounds[6]);
                
        } else {
                $width = abs($bounds[4]-$bounds[6]);
                $height = abs($bounds[7]-$bounds[1]);
                $offset_y = $font_height;;
                $offset_x = 0;
        }
        
        $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
        
        $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
        $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
        
        if ($this->transparent) ImageColorTransparent($image, $background);
        ImageInterlace($image, false);
        
        // render it.
        ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
        
        // output PNG object.
        imagePNG($image);
}
}

$text = new textPNG;

if (isset($msg)) $text->msg = $msg; // text to display
if (isset($font)) $text->font = $font; // font to use (include directory if needed).
if (isset($size)) $text->size = $size; // size in points
if (isset($rot)) $text->rot = $rot; // rotation
if (isset($pad)) $text->pad = $pad; // padding in pixels around text.
if (isset($red)) $text->red = $red; // text color
if (isset($grn)) $text->grn = $grn; // ..
if (isset($blu)) $text->blu = $blu; // ..
if (isset($bg_red)) $text->bg_red = $bg_red; // background color.
if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
if (isset($tr)) $text->transparent = $tr; // transparency flag (boolean).

$text->draw();
?>
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to convert a font to an image (PNG) using php

Post by Rksk » Mon Apr 19, 2010 8:28 am

Will it work with unicode?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to convert a font to an image (PNG) using php

Post by Neo » Mon Apr 19, 2010 7:29 pm

You are asking a question that I must ask you. You are the UNICODE man here so give a try and report to all of us :)
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to convert a font to an image (PNG) using php

Post by Rksk » Thu Sep 09, 2010 5:14 pm

After a long time;-)

I tested above code with unicode.

It works only with numbers. texts aren't working. it seems a bug in php, we can't use unicode in php. it returns only ????

[ Post made via Mobile Device ] Image
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to convert a font to an image (PNG) using php

Post by Neo » Thu Sep 09, 2010 7:19 pm

I have no time to test it. If you could put up a little example file with above code that prints a text in unicode, I can test on my server. Make sure you attach the font as well.
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to convert a font to an image (PNG) using php

Post by Rksk » Thu Sep 09, 2010 8:27 pm

There is it.
test.zip
(71.11 KiB) Downloaded 308 times
test.zip
(71.11 KiB) Downloaded 308 times
[ Post made via Mobile Device ] Image
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: How to convert a font to an image (PNG) using php

Post by Neo » Fri Sep 10, 2010 1:06 pm

I had a look on the files and as you said this doesn't seems to work for unicode fonts. I have two doubts. I'm not sure php or the rendering function support unicode. There are lots of discussions going on on the internet and I didn't see any feasible solution. The other point is whether the font is really unicode. When I open it on windows just by double clicking I see the same "question mark" symbols. I'll investigate further when I have time.
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to convert a font to an image (PNG) using php

Post by Rksk » Fri Sep 10, 2010 2:02 pm

I don't know that font is realy unicode. but kaputa.com tells it is unicode 1.

thank u.

[ Post made via Mobile Device ] Image
Post Reply

Return to “PHP & MySQL”