/    Sign up×
Community /Pin to ProfileBookmark

imagettftext problem

I’m using the following code to generate font images from text entered on a form. If line returns are entered, a new line is started, & displays well. But if the textarea box on the form does the wrapping, the text displays all on one line, and sticks out on either side of the rectangle background. Is there a way to split the text string up into it’s individual words, and generate those individually, and wrapped?

[code=php]<?php
// ######################################
// This was written by Michael Morgan of
// CodingTuts (http://www.codingtuts.com)
// ######################################

$imageWidth = 500;
$imageHeight = 360;
$textFont = $_POST[‘font_type’];
$textSize = 30;
$textString = $_POST[‘text_msg’];

header(“Content-type: image/png”);

// Create the image
$image = imagecreatetruecolor($imageWidth, $imageHeight);

// Create some colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);

// Fill the background of our image
imagefilledrectangle($image, 0, 0, $imageWidth, $imageHeight, $white);

// Get box info
$box = imagettfbbox($textSize, 0, $textFont, $textString);

//Find out the width and height of the text box
$textW = $box[2] – $box[0];
$textH = $box[5] – $box[3];

// Calculate the positions
$positionLeft = ($imageWidth – $textW)/2;
$positionTop = (($imageHeight – $textH)/2);

// Add some text
imagettftext($image, $textSize, 0, $positionLeft, $positionTop, $black, $textFont, $textString);

// Output the image to the browser
imagepng($image);

imagedestroy($image);
?>[/code]

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Taren421 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...