Page 1 of 1
imagettftext() question
Posted: Mon Mar 20, 2006 6:56 pm
by jabbaonthedais
I'm using
imagettftext() to make text into an image. What I want to do is put another image as a background. So that it text appears on my current image.
Code: Select all
<?php
// Set the content-type
header("Content-type: image/png");
// Create the image
$im = imagecreatetruecolor(400, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Any ideas how I could do this? Not sure if its something with the function or maybe even more simple that I'm not seeing. I've been reading about it but can't find much info about this particular function.
Thanks!
Posted: Mon Mar 20, 2006 10:27 pm
by Citizen
What exactly is the problem?
Posted: Tue Mar 21, 2006 4:19 am
by onion2k
Replace "$im = imagecreatetruecolor(400, 30);" with something to open your image .. imagecreatefromjpeg() for example.
Alternatively, create the image as you are now, then open your background image using imagecreatefromjpeg(), then use imagecopy() to copy it to your new image, then write the text.
I wrote an article that covers this sort of thing for PHPGD..
http://www.phpgd.com/articles.php?article=5
Posted: Wed Mar 22, 2006 9:15 pm
by jabbaonthedais
onion2k wrote:Replace "$im = imagecreatetruecolor(400, 30);" with something to open your image .. imagecreatefromjpeg() for example.
Alternatively, create the image as you are now, then open your background image using imagecreatefromjpeg(), then use imagecopy() to copy it to your new image, then write the text.
I wrote an article that covers this sort of thing for PHPGD..
http://www.phpgd.com/articles.php?article=5
Great read! Helped a lot! I have a few questions:
1. Is there any way to cut down on the filesize? My images are going to be around 800x1200 and when I do it now the image is 1.4mb. Any easy way to get that down to 200kb?
2. How can I make the background white while the image loads (instead of black). I see in your article how to do it creating a rectagle, but since I'm using imagecreatefromjpeg() I don't see how to do it.
3. Are these function really slow? I am testing it and it seems my server (which is normally really fast) is taking a while to load.
Thanks again! Here's my current code:
Code: Select all
<?php
// Set the content-type
header("Content-type: image/png");
// Create the image
$im = imagecreatefromjpeg("01.jpg");
// Create some colors
$black = imagecolorallocate($im, 0, 0, 0);
$fill = imagecolortransparent($im);
imagefilledrectangle($im, 0, 0, 10, 200, $fill);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'arial.ttf';
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagepng($im);
imagedestroy($im);
?>
Posted: Thu Mar 23, 2006 2:32 am
by onion2k
The way to get the filesize down is to change image format .. use jpeg instead .. you'll need to change the content-type to image/jpeg, and use imagejpeg() to output it. imagejpeg() has an option to control the quality ..
Code: Select all
imagejpeg($im,"",80); //High quality image, large filesize
imagejpeg($im,"",60); //Medium quality image, medium filesize
imagejpeg($im,"",40); //Low quality image, small filesize
You can change the background colour by adding style="background-color: #ffffff;" to the img tag in your HTML .. it's not the background of the image you're seeing when the image loads, it's the background of the img tag.
These functions are fast, but they're
very memory intensive .. if you're running low then things will slooooooooooow down. It's very dependent on PHP's memory settings. Some of my image code takes 20 minutes to run!!
Posted: Thu Mar 23, 2006 9:49 am
by jabbaonthedais
onion2k wrote:These functions are fast, but they're very memory intensive .. if you're running low then things will slooooooooooow down. It's very dependent on PHP's memory settings. Some of my image code takes 20 minutes to run!!
hmm... well I'm wanting to let people create "fan signs" where they type in text and it displays over my image. This could be on blogs (hotlinked from my site), meaning a thousands could be viewing the php file as an image. I know others do this, but not sure if they have a server-side script that does it better. Basically what people put in their "img src" is mysite.com/picture.php?text=thetexttheywant and it builds it on the spot for their visitors. Do you recommend against this function, or have any other suggestions? Thanks a lot! You've been really helpfull!
Posted: Thu Mar 23, 2006 10:31 am
by onion2k
jabbaonthedais wrote:hmm... well I'm wanting to let people create "fan signs" where they type in text and it displays over my image. This could be on blogs (hotlinked from my site), meaning a thousands could be viewing the php file as an image. I know others do this, but not sure if they have a server-side script that does it better. Basically what people put in their "img src" is mysite.com/picture.php?text=thetexttheywant and it builds it on the spot for their visitors. Do you recommend against this function, or have any other suggestions? Thanks a lot! You've been really helpfull!
This is a situation where caching the image is vital..
http://www.phpgd.com/scripts.php?script=9
That's a very simple cache system .. it generates an image of the date .. If the file exists already it just sends the existing version to the user, if it doesn't exist it generates it, saves it, and sends it to the user. In the case of a text banner you'd probably save the image based on the text .. next time a user comes along you'd check if the image has already been generated and if it has you'd just send the existing data rather than building a whole new image.