Click to See Complete Forum and Search --> : This PHP script I downloaded does not work.


Craig McPherson
09-16-2001, 07:50 AM
I've been looking around for quite a while for a program that does what this PHP script claims to do: take a string of text and then create a PNG image containing that text using a specified font and size. However, this script I found to do the job isn't working. I'm not even sure where I downloaded it; Netscape crashed right after I saved it and I couldn't find where I'd gotten it from again.


<?
/*
To draw the PNG - call this script with a URL like the following:
http://www.yoursite.com/text_png.php3?msg=testing+class&rot=15&size=48&font=fonts/ARIAL.TTF
*/

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

class textPNG {
var $font = 'fonts/TIMES.TTF'; //default font. directory relative to scr
ipt 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, $off
set_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 need
ed).
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();
?>



This is the error the script returns:

Fatal error: Call to undefined function: imagettfbbox() in /my/web/site/png.php

One thing I noticed is that the script mentions PHP3 but the Apache module I have inserted is PHP4. Would this make a difference; would I need to load the PHP3 module or make changes to the script?

If anybody knows of any other text -> PNG (or GIF) program, I'd love to hear about it -- it doesn't have to be any sort of web script; this PHP script just happened to be the first thing I found.

[ 16 September 2001: Message edited by: Craig McPherson ]

[ 16 September 2001: Message edited by: Craig McPherson ]

Craig McPherson
09-16-2001, 07:55 AM
I found out that I got the script from here, if it helps any:
http://www.phpbuilder.com/snippet/detail.php?type=snippet&id=57

Craig McPherson
09-16-2001, 02:12 PM
Okay. $5.00US gets Paypalled to the first person to fix this or point me towards a functional alternative to this script, preferably some nice command-line program.

Bribery never fails.

[ 16 September 2001: Message edited by: Craig McPherson ]

Sweede
09-16-2001, 02:46 PM
Craig, through all your magical wisdom, i cant believe you didnt figure it out.

your error..

Fatal error: Call to undefined function: imagettfbbox() in /my/web/site/png.php

the function http://www.php.net/manual/en/function.imagettfbbox.php

the text..
"This function requires both the GD library and the FreeType library"

Also look at http://www.php.net/manual/en/function.imagettftext.php

But (since i know your using debian) debian doesnt have a ttf enabled php package, you have to remove all that and compile it yourself.

besure to install libfreetype6-dev and all the other development packages you'll need (libpng2-dev, zlib1g-dev , libgd-dev and what ever they require)