Page 1 of 1
Accessing the GD library
Posted: Tue Sep 30, 2008 1:06 pm
by rlparker25
Hi,
I am trying to create a user defined tag that takes a piece of text and turns it into an image. I have a script that works fine on its own but if I put it into a user defined tag it stops working. I am guessing that this is because it can't access the GD library functions as if I just put simple php in the tag works fine.
Does anyone know how i can access the GD library funcations?
This is my code
$font = "impact.ttf";
$size = 120;
$text = 'hello ';
if (function_exists("imagecreate")
&& ($area = @imagettfbbox($size, 0, $font, $text)) !== false)
{
$textwidth = $area[2] - $area[0] + 2;
$textheight = $area[1] - $area[7] + 2;
$textx = 1;
$texty = -$area[7];
$ok = true;
$image = imagecreate($textwidth, $textheight);
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
imagefilledrectangle($image, 0, 0, $textwidth, $textheight, $white);
imagettftext($image, $size, 0, $textx, $texty, $black, $font, $text);
$width = imagesx($image);
$height = imagesy($image);
$new_width = 968;
$new_height = $height * ($new_width/$width);
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_resized);
imagedestroy($image_resized);
}
else
{
echo 'error';
}
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 1:12 pm
by alby
rlparker25 wrote:
I am trying to create a user defined tag that takes a piece of text and turns it into an image. I have a script that works fine on its own but if I put it into a user defined tag it stops working. I am guessing that this is because it can't access the GD library functions as if I just put simple php in the tag works fine.
What is the error or print 'error'?
Alby
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 1:21 pm
by rlparker25
it doesn't generate an error, I have just made it write the word error to the screen if it cannot find the function imagecreate. This was just to show me that it was actually doing something as if it doesn't create the image it doesn't output anything
Rachael
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 2:47 pm
by Russ
I've only ever done it when you write a file and create from a base image.
//Write that file
header("Content-type: image/png");
and perhaps something like...
$imagecreate = imagecreatefrompng("/pathtoimage/image.png");
Hope this helps,
Russ
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 2:58 pm
by rlparker25
Thanks but the problem I am having isn't with creating an image. If I just run my code in a normal php page it works fine. The problem is if i put the code in a user defined tag it can't seem to access the GD library functions
I need to be able to take the page title and turn it into an image to be used elsewhere on the page
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 3:08 pm
by alby
rlparker25 wrote:
Thanks but the problem I am having isn't with creating an image. If I just run my code in a normal php page it works fine. The problem is if i put the code in a user defined tag it can't seem to access the GD library functions
I need to be able to take the page title and turn it into an image to be used elsewhere on the page
1. Are you sure that imagettfbbox load your font impact.ttf?
Insert a check for this
2. Have you seen
truetypetext module?
Alby
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 3:09 pm
by Russ
Sorry, didn't quite get what you meant. It can be done, there are modules, maybe paths in the code?
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 3:26 pm
by rlparker25
Hi,
I think I had the font file in the wong place, I have put it in the root and now I get a huge amunt of rubbish written out on the screen
e.g.
����JFIF��>CREATOR: gd-jpeg v1.0 (using IJG JPEG v62), default quality ��C $.' ",#(7),01444'9=82<.342��C 2!!22222222222222222222222222222222222222222222222222��`�"�� ���}!1AQa"q2���#B��R��$3br� %&'()*
aflmanchester.dns-systems.net/john-smith/
I am not sure that text module would work for me. I am trying to take the persons name and turn it into an image lke the john smith one shown at the bottom of the page. Once the image has been creted I need to resize it so they are always the same width regardless of how long the persons name is.
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 3:37 pm
by alby
rlparker25 wrote:
I think I had the font file in the wong place, I have put it in the root and now I get a huge amunt of rubbish written out on the screen
This is better

Now, you can follow the first part of Russ advice (Reply #3) ....
Alby
Re: Accessing the GD library
Posted: Tue Sep 30, 2008 4:20 pm
by rlparker25
sorry I don't undersand what advie you mean?
the post about creating an image based on an existing one doesn't apply
Do you know why I am getting that error when I know the code works outside of the cms?
Re: Accessing the GD library
Posted: Tue Oct 07, 2008 8:02 am
by robsta
Hi
I gave up on the whole server side gd/truetype text/image replacement a while back... my 2c follow...
I've been using siFR 3.0 [
http://novemberborn.net/sifr3/beta2 ] for all my text replacement for the last two years and it works really well... There's no dicking around and it can be applied directly to a cmsms page without requiring a UDT or a module... and you can use multiple fonts, typography control, multiple links and style them via css accordingly.
It does need flash to work ... but with no flash you get html text instead... which you can style as well - which is much better than an h1 wrapping an images alt attribute.
also it doesn't require server side work...
www.designworks.co.nz
www.leapltd.co.nz
www.kordiasolutions.com
all use it throughout
R
Re: Accessing the GD library
Posted: Tue Oct 07, 2008 8:16 am
by rlparker25
thanks, will remember that for the future.
i ended up putting my code in an include file and it seemed to work fine!
Rachael