Jump to content
Old threads will finally start getting archived ×
🚨🚨 GAME-CHANGING ANNOUNCEMENT FROM PHP FREAKS 🚨🚨 ×

wordwrap() - Wrap by pixels, rather than by characters? Fit text specifically?


Recommended Posts

Hey guys 'n' girls!  :D

 

I have a web app going that works really well so far.

 

It basically takes some text from the user, and puts it onto a 468x60px image. I am using GDLibrary commands to put the text into the image.

 

If the text is longer than the edge of the image when displayed in a size twelve font, it goes off the edge.

 

So I used the wordwrap() function to wrap the text at 32 characters to stop it going over the edge.

 

From the manual:

 

<?php string wordwrap  ( string $str  [, int $width= 75  [, string $break= "\n"  [, bool $cut= false  ]]] )?>

 

The code I have been using:

 

<?php

//Format the string
$fstring = wordwrap($string, 32, "\n", true);
//Write the string to the image with GDLibrary
imagettftext($img, 12, 0, 10, 30, $text_color, 'userimages/arial.ttf', $fstring);

?>

 

This fits perfectly when I wrote out a string of entirely WWWWWWWs and MMMMMMs, the widest character in the font 'Arial'. It fits beautifully and wraps nicely with the GD Library placing it on the image.

 

The image MUST be a set size, I can't change the width of the image based on the text.

 

When I use a generic string, i.e. "The quick brown fox jumps over the lazy dog, how razorback jumping frogs can level six piqued gymnasts" it cuts at 32 characters still - but in that string 32 characters is only several words, "the quick brown fox jumps over" - it then wraps to the next line, leaving a whole load of white space on the right side of the image.

 

Basically, I need a way to fit a string (that won't ever be longer than 160 characters) into my image. If the text is too long, it should wrap, but it should wrap at the edge of the image and not based on a certain character count.

 

Maybe I'm asing for too much here, but thanks!

I've tried to glean as much from that as I can, but I'm still stuck.

 

I got this far:

 

<?php
$size = imagettfbbox(12, 0, 'userimages/arial.ttf', $string);
$dx = (imagesx($img)) - (abs($size[2]-$size[0])) - 20; 

imagettftext($img, 12, 0, $dx, 30, $text_color, 'userimages/arial.ttf', $tweet);
?>

 

But now all the text just lines up to the right, pushing the beginning over to the left.

 

I'm sorry I'm being useless here.  :facewall:

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.