Index: C:/1_work/Sites/solaas_new/modules/signwriter/signwriter.module
===================================================================
--- C:/1_work/Sites/solaas_new/modules/signwriter/signwriter.module	(revision 6)
+++ C:/1_work/Sites/solaas_new/modules/signwriter/signwriter.module	(working copy)
@@ -500,8 +500,8 @@
       return '';
     }
   }
-  $x = _signwriter_get_val($profile->xoffset, 0);
-  $y = _signwriter_get_val($profile->yoffset, 0);
+  $xoffset = _signwriter_get_val($profile->xoffset, 0);
+  $yoffset = _signwriter_get_val($profile->yoffset, 0);
 
 
   file_check_directory($cachedir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
@@ -540,14 +540,28 @@
     $textwidth = abs($box[2]) + abs($box[0]) + 5; // sometimes text is clipped. I don't know why, so I add 5px here...
     $textheight = abs($box[1]) + abs($box[7]);
 
-    // if we exceed maxwidth, then use a smaller font size
+    // LS:. Start of the multiline magic
+    drupal_set_message("textwidth: ".$textwidth." | maxwidth: ".$maxwidth);
+    $lines = array();
     if ($maxwidth && ($textwidth > $maxwidth)) {
-      $profile->fontsize = ($size * ($maxwidth / $textwidth)) - 0.5; // we take an extra 0.5 to avoid endless recursing due to actual font size not decreasing
-      return signwriter_url($profile);
+      $words = split(' ', $text);
+      $line = '';
+      foreach ( $words as $word ) {
+        $wordbox  = imagettfbbox ( $size, 0, $fontfile, $line . $word );
+        $newwidth = $wordbox[4] - $wordbox[0] + 2; // +2: dirty hack to avoid clipping on some lines
+        if ($newwidth > $maxwidth){ 
+          $lines[] = trim ($line);
+          $line = '';
+        }
+        $line .= $word . ' ';
+      }
+      $lines[] = trim($line);
+    }else{
+      $lines[] = $text;
     }
 
-    $width = $width ? $width : $textwidth + $x;
-    $height = $height ? $height : $textheight + $y;
+    $width = $width ? $width : ($maxwidth ? $maxwidth : $textwidth + $xoffset);
+    $height = $height ? $height : $textheight * count($lines) + $yoffset;
 
     // create the image
     if ($backgroundimage) {
@@ -560,18 +574,6 @@
       $im = imagecreate($width, $height);
     }
 
-    // align the text
-    switch ($align) {
-      case 'center':
-      case 'centre':
-        $x += ($width - $textwidth) / 2;
-        break;
-
-      case 'right':
-        $x = $width - $textwidth - $x;
-        break;
-    }
-
     $background = imagecolorallocate($im, $bg[0], $bg[1], $bg[2]);
     $foreground = imagecolorallocate($im, $fg[0], $fg[1], $fg[2]);
 
@@ -581,9 +583,26 @@
       // (unless you're using png).
       imagecolortransparent($im, $background);
     }
+    
+    foreach($lines as $n => $line){
+      // align the text
+      $linebox = imagettfbbox($size, 0, $fontfile, $line);
+      $linewidth = $linebox[4] - $linebox[0];
+      switch ($align) {
+        case 'center':
+        case 'centre':
+          $x = $xoffset + ($width - $linewidth) / 2;
+          break;
+        case 'right':
+          $x = $width - $linewidth - $xoffset;
+          break;
+        default:
+          $x = $xoffset;
+      }
+      $y = $yoffset + $n * $textheight;
+      imagettftext($im, $size, $angle, $x, $y + abs($box[5]), $foreground, $fontfile, $line);
+    }
 
-    imagettftext($im, $size, $angle, $x, $y + abs($box[5]), $foreground, $fontfile, $text);
-
     $imagefunction = "image$imagetype";
     $imagefunction($im, $file);
     imagedestroy($im);
