--- textactions.inc	2009-02-17 01:50:07.000000000 +1300
+++ ../textactions.inc	2009-02-16 12:35:54.000000000 +1300
@@ -39,6 +39,8 @@ function textactions_text2canvas_form($a
     'fontfile' => 'liberation-fonts-1.04/LiberationSans-Regular.ttf',
     'text' => 'Hello World!',
     'evaluate_text' => FALSE,
+    'width' => 0,
+    'lineheight' => 14,
   );
   $action = array_merge($defaults, (array)$action);
   $form = array(
@@ -78,6 +80,20 @@ function textactions_text2canvas_form($a
       '#description' => t('Enter an offset in pixels or use a keyword: <em>top</em>, <em>center</em>, or <em>bottom</em>. Syntax like <em>bottom-20</em> is also valid.'),
       '#size' => 10,
     ),
+    'width' => array(
+      '#type' => 'textfield',
+      '#title' => t('Width'),
+      '#default_value' => $action['width'],
+      '#description' => t('Enter a width for the text. It will be automatically wrapped. Enter 0 if you do not want to wrap your text.'),
+      '#size' => 10,
+    ),
+    'lineheight' => array(
+      '#type' => 'textfield',
+      '#title' => t('Line height'),
+      '#default_value' => $action['lineheight'],
+      '#description' => t('Enter the line height of the text. This value will be ignored if wrapping is turned off.'),
+      '#size' => 10,
+    ),
     'RGB' => imagecache_rgb_form($action['RGB']),
     'fontfile' => array(
       '#type' => 'textfield',
@@ -224,7 +240,19 @@ function textactions_text2canvas_image(&
   $action['alpha'] = ($action['alpha'] / 100); //Convert to decimal between 0 and 1
   $action['RGB']['alpha'] = ((1 - $action['alpha']) * 127); //convert opacity to proper alpha value (0 = opaque, 127 = transparent)
 
-  return imageapi_image_overlaytext_alpha($image, $text, $action['size'], $x_ins, $y_ins, $action['RGB'], $fontpath, $action['angle']);
+  // if a width is specified, wrap the text and then draw each line of text separately
+  if ($action['width'] > 0) {
+	  $lines = textactions_wrap_text($text, $action['size'], $action['width'], $fontpath, $action['angle']);
+	  
+	  foreach( $lines as $nr=>$line ) {
+		imageapi_image_overlaytext_alpha($image, implode(' ', $line), $action['size'], $x_ins, $y_ins, $action['RGB'], $fontpath, $action['angle']);
+		$y_ins += $action['lineheight'];
+	  }
+
+	  return true;
+  } else {
+	  return imageapi_image_overlaytext_alpha($image, $text, $action['size'], $x_ins, $y_ins, $action['RGB'], $fontpath, $action['angle']);
+  }
 }
 
 /**
@@ -509,3 +537,48 @@ function textactions_find_font($fontpath
   // otherwise, just return what we had and hope it's in a system library
   return $fontpath;
 }
+
+
+/**
+ * Given a text, its fontface, size and desired width, break it apart in lines that fit this width.
+ * Code taken from: http://kore-nordmann.de/blog/image_creation_with_php_texts.html
+ *
+ * @param $text the text that should be wrapped
+ * @param $size the size in pixels (GD) or points (GD2) of the text
+ * @param $width the width of the text in pixels
+ * @param $fontfile the name of the TTF file containing the fontface
+ * @param $angle specify a non-zero value to draw rotated text
+ *
+ * $return an Array of Arrays. Each array specifies one line of wrapped text, each element is a word.
+ * Use the 'implode' function to construct the lines again.
+ */
+function textactions_wrap_text($text = 'Hello World!', $size = 18, $width = 200, $fontfile = 'MgOpenModernaBold', $angle = 0) {
+	$lines = array( array() );
+	$tokens = preg_split( '/\s+/', $text ); 
+	
+	$line = 0;
+	foreach( $tokens as $nr => $token ) {
+		$selectedLine = $lines[$line];
+		$selectedLine[] = $token;
+
+		$boundings = imagettfbbox($size, $angle, $fontfile, implode( ' ', $selectedLine ));
+		$boundingsWidth = $boundings[2] - $boundings[0];
+	
+		// Check if line is too long 
+		if ( $boundingsWidth > $width ) { 
+			if ( count( $selectedLine ) == 1 ) { 
+				// Return false if one single word does not fit into one line 
+				// Scale down font size to fit this word in one line 
+				return array( array("Single","word","doesn't","fit") ); // $width / $boundingsWidth; 
+			} else { 
+				// Put word in next line instead and reduce available height by used space 
+				$lines[++$line][] = $token;  
+			} 
+		} else { 
+			// Everything is ok - put token in this line 
+			$lines[$line][] = $token; 
+		} 
+	}
+
+	return $lines;
+}
\ No newline at end of file
