---
title: "GD Library - Skriva text"
type: "forum-thread"
url: "https://www.webforum.nu/amne/php/133958-gd-library-skriva-text"
topic: "PHP"
topic_url: "https://www.webforum.nu/amne/php"
author: "Troxy"
published: "2005-09-08T16:51:26.000Z"
updated: "2005-09-08T17:19:46.000Z"
replies: 1
views: 351
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/php/133958-gd-library-skriva-text"
---

# GD Library - Skriva text

## #1 — Troxy, 2005-09-08T16:51Z

Hej,

Jag hämtar text från en databas som ska skrivas ut på en bild med hjälp av GD Library. Nu undrar jag om det på något sätt går att begränsa text till en yta. Ex. jag vill att texten ska löpa 200 px, sedan radbrytning. Jag lyckas bara skriva ut allt som en enda lång rad.

Mvh Emil

Permalänk: https://www.webforum.nu/p/133958

## #2 — Pedda, 2005-09-08T17:19Z

Detta är inget jag har testat nån gång men efter att ha letat lite i [manualen](http://se2.php.net/manual/sv/function.imagettfbbox.php) så hittade jag detta exempel:

```php
<?php
       function printWordWrapped(&$image, $top, $left, $maxWidth, $font, $color, $text, $textSize) {
               $words = explode(' ', strip_tags($text)); // split the text into an array of single words
               $line = '';
               while (count($words) > 0) {
                       $dimensions = imagettfbbox($textSize, 0, $font, $line.' '.$words[0]);
                       $lineWidth = $dimensions[2] - $dimensions[0]; // get the length of this line, if the word is to be included
                       if ($lineWidth > $maxWidth) { // if this makes the text wider that anticipated
                               $lines[] = $line; // add the line to the others
                               $line = ''; // empty it (the word will be added outside the loop)
                               }
                       $line .= ' '.$words[0]; // add the word to the current sentence
                       $words = array_slice($words, 1); // remove the word from the array
                       }
               if ($line != '') { $lines[] = $line; } // add the last line to the others, if it isn't empty
               $lineHeight = $dimensions[1] - $dimensions[7]; // the height of a single line
               $height = count($lines) * $lineHeight; // the height of all the lines total
               // do the actual printing
               $i = 0;
               foreach ($lines as $line) {
                       imagettftext($image, $textSize, 0, $left, $top + $lineHeight * $i, $color, $font, $line);
                       $i++;
                       }
               return $height;
               }

?>
```

Permalänk: https://www.webforum.nu/p/1669796

---

Tråden på webben: https://www.webforum.nu/amne/php/133958-gd-library-skriva-text
