For some reason,
this is not wordwrapping after 17 characters like I want it to. Any ideas? (Also, I want the height of the image to be the height of the text, however many lines it ends up being)
Here's my code:
PHP Code:
<?php
// Path to our font file
$font = 'AvenirLTStd-Heavy.otf';
$fontsize = 27;
//Text to be written
$helloworld = isset($_GET['text']) ? $_GET['text'] : "hello World";
// word wrap
$text2 = wordwrap($helloworld,17,true);
// create a bounding box for the text
$dims = imagettfbbox($fontsize, 0, $font, $text2);
// make some easy to handle dimension vars from the results of imagettfbbox
// since positions aren't measures in 1 to whatever, we need to
// do some math to find out the actual width and height
$width = $dims[4] - $dims[6]; // upper-right x minus upper-left x
$height = $dims[3] - $dims[5]; // lower-right y minus upper-right y
// Create image
$image = imagecreatetruecolor(345,$height);
imagesavealpha($image, true);
$trans_colour = imagecolorallocatealpha($image, 204, 213, 218, 127);
imagefill($image, 0, 0, $trans_colour);
// pick color for the text
$fontcolor = imagecolorallocate($image, 50, 50, 50);
// fill in the background with the background color
imagefilledrectangle($image, 0, 0, $width, $height, $trans_colour);
// x,y coords for imagettftext defines the baseline of the text: the lower-left corner
// so the x coord can stay as 0 but you have to add the font size to the y to simulate
// top left boundary so we can write the text within the boundary of the image
$x = 0;
$y = $fontsize;
imagettftext($image, $fontsize, 0, $x, $y, $fontcolor, $font, $text2);
// tell the browser that the content is an image
header('Content-type: image/png');
header("Expires: Mon, 01 Jul 2003 00:00:00 GMT"); // Past date
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Consitnuously modified
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Pragma: no-cache"); // NO CACHE
// output image to the browser
imagepng($image);
// delete the image resource
imagedestroy($image);
?>
See link:
http://johnmilesart.com/test3.php?te...of%20breaking?
I'm new to PHP, so this script is basically an amalgamation of some stuff I've found around the web. I'm sure the solution is simple.