I’m trying to merge a textstring with an image. I’ve found examples here on Stackoverflwo on how to do this, but all I get i jibberish when I load the script. I think it’s a font problem, but I don’t know how to solve this. I’ve placed the True type font (arial) in the same directory as the script. Suggestions? Here’s my code:
<?php
$im = imagecreatefromjpeg('image.jpg');
//The numbers are the RGB values of the color you want to use
$black = ImageColorAllocate($im, 255, 255, 255);
//The canvas's (0,0) position is the upper left corner
//So this is how far down and to the right the text should start
$start_x = 10;
$start_y = 20;
$font = 'arial.ttf';
Imagettftext($im, 12, 0, $start_x, $start_y, $black, $font, 'text to write');
//Creates the jpeg image and sends it to the browser
//100 is the jpeg quality percentage
Imagejpeg($im, '', 100);
ImageDestroy($im)
?>
You’re not telling the browser that you’re actually sending it an image, so it tries to interpret the binary data as text.
In order to let the browser know that it’s about to receive and image, you have to send it a content type. You need to put this line before
imagejpeg().