0

I have been ripping my hair out with this and have tried many many solutions on here to no avail.

I am trying to add some text to an image, but all it is doing is showing my background image, is there anything glaringly obvious that I'm doing wrong here?

Thanks in adcance

<?
header('Content-Type: image/jpeg');

$fbid = $_POST['fbid'];
$background_img = $_POST['background'];
$message = $_POST['text'];
$ts = $_POST['ts'];

$filename = $fbid . "-" . $ts . ".jpg";

$image_canvas = imagecreatetruecolor(640,400);

$background = imagecreatefromjpeg($background_img);
$overlay    = imagecreatefrompng("../images/image-overlay.png");

imagecopyresampled($background, $overlay, 0, 0, 0, 0, imagesx($overlay), imagesy($overlay), imagesx($overlay), imagesy($overlay));

imagefilledrectangle($image_canvas, 0,0,150,30, $background);

$white = imagecolorallocate($background, 255, 255, 255);

imagettftext($image_canvas, 25, 0, 50, 50, $white, "arial.TTF", $message);

imagejpeg($background,"../created/" . $filename, 100);

imagedestroy($background);
Shane Jones
  • 885
  • 1
  • 9
  • 27

2 Answers2

0

You're missing the canvas. Start the build with imageCreateTrueColor.

$imageCanvas = imageCreateTrueColor($width, $height);
//your code
$background = imagecreatefromjpeg($background_img);
//more of your code
imagefilledrectangle($imageCanvas, 0, 0, 150, 30, $background);
//now do the same for the text only us imag
imagettftext($imageCanvas, 25, 0, 50, 50, $white, "arial.TTF", $message);

Your merging the jpeg and the text elements on the $imageCanvas.

rwhite
  • 430
  • 2
  • 11
0

Have a look at this. It works, and the link to the page is below. Based on your original post, I believe this is what your looking for.

 /* first composite the canvas with the background */
 $background_img="../img/adfuba_october.png";
 $compositeString = "composite.png";

 list($width,$height) = getimagesize($background_img);
 $image_canvas = imagecreatetruecolor($width,$height);
 $background = imagecreatefrompng($background_img);
 imagecopyresampled($image_canvas,$background,0,0,0,0,$width,$height,$width,$height);

 /* now add the text */
 $fontPath = "path/to/your/fontFile/ARIAL.TTF";
 $fontSize = 24;
 $percent = 0.25;
 $txt_x = abs($width*$percent);
 $txt_y = abs($height*$percent);
 $color = "008844";
 $message = "This is User Text";
 imageTTFtext($image_canvas, $fontSize, 0, $txt_y, $txt_y, $color, $fontPath, $message);

 /* now generate the file */
 imagepng($image_canvas, $compositeString, 0) or die("error saving png");

 ?>
 <p>This is a composite image:<br><img src="<?php echo $compositeString;?>"></p>

You can see the composite image here. Couple things to keep in mind. The path to your TrueType font face should be absolute even if the TrueType font file is located in the same directory as your script.

Also, the canvas is your background object, then you layer images or text over top the canvas.

Finally, (and you may have figured this out) your layered elements are order dependent form canvas to text. Meaning canvas -> background -> another graphic -> then the text. Otherwise, you could end up covering up an element you meant to render in front. Hope that helps.

rwhite
  • 430
  • 2
  • 11
  • Meant to add this to my post. I'm assuming $_POST['background'] is a string and contains the path/name of an image file already stored on the server. If $_POST['background'] is an image file, then you'll need to go through the [file upload process](http://www.php.net/manual/en/features.file-upload.post-method.php), before you try and build your new image file. – rwhite Jun 19 '13 at 14:50