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);
?>