--- textimage.module-old	2010-10-13 09:18:51.000000000 +0200
+++ textimage.module	2010-10-13 09:11:02.000000000 +0200
@@ -388,7 +388,7 @@ function textimage_image_from_preset($pr
   }
 
   // Generate the textimage
-  $img = textimage_text_to_image($text, $font_size, $font_file, $font_color, $text_angle, $text_maximum_width, $text_fixed_width, $text_align);
+  $img = textimage_text_to_image($text, $font_size, $font_file, $font_color, $text_angle, $text_maximum_width, $text_fixed_width, $text_align, $text_margin);
 
   // Add a border
   if ($text_stroke['width'] && $text_stroke['color']) {
@@ -396,9 +396,9 @@ function textimage_image_from_preset($pr
   }
 
   // Add margin
-  if ($text_margin['top'] || $text_margin['right'] || $text_margin['bottom'] || $text_margin['left']) {
-    $img = textimage_image_add_margin($img, $text_margin);
-  }
+#  if ($text_margin['top'] || $text_margin['right'] || $text_margin['bottom'] || $text_margin['left']) {
+#    $img = textimage_image_add_margin($img, $text_margin);
+#  }
 
   // Place result on top of another preset's result
   if (is_numeric($background_image) && $background_image = _textimage_preset_load($background_image)) {
@@ -687,7 +687,7 @@ if (!function_exists('drupal_preg_match'
  * @return $image
  *   A GD image resource.
  */
-function textimage_text_to_image($text, $fontsize, $font, $color = array('hex' => '#000000', 'opacity' => '100'), $angle = 0, $maximum_width = 0, $fixed_width = 0, $align = ALIGN_LEFT) {
+function textimage_text_to_image($text, $fontsize, $font, $color = array('hex' => '#000000', 'opacity' => '100'), $angle = 0, $maximum_width = 0, $fixed_width = 0, $align = ALIGN_LEFT, $text_margin = array()) {
   // Set rotation angle.
   $q_angle = -$angle;
   while ($q_angle > 0 || $q_angle <= -90) {
@@ -728,13 +728,13 @@ function textimage_text_to_image($text, 
     : $box_height;
 
   // Create Image.
-  $image = imagecreatetruecolor($image_width + 1, $image_height + 1);
+  $image = imagecreatetruecolor($image_width + 1 + $text_margin['right'], $image_height + 1 + $text_margin['bottom']);
   $back = imagecolorallocatealpha($image, 0, 0, 0, 127);
   imagefill($image, 0, 0, $back);
 
   // Set text alignment left
-  $x = -$bbox[0];
-  $y = $box_height - $bbox[3];
+  $x = -$bbox[0] + $text_margin['left'];
+  $y = $box_height - $bbox[3]; # not used since changes in imagettftext() call
 
   if (($fixed_width && $maximum_width > 0) && $align != ALIGN_LEFT) {
     switch ($align) {
@@ -757,7 +757,7 @@ function textimage_text_to_image($text, 
   list($r, $g, $b) = _textimage_hex2rgb($color['hex']);
   $alpha = -($color['opacity'] - 100) / 100 * 127;
   $fore = imagecolorallocatealpha($image, $r, $g, $b, $alpha);
-  imagettftext($image, $fontsize, $q_angle, $x, $y, $fore, $font, $text);
+  imagettftext($image, $fontsize, $q_angle, $x, $fontsize + $text_margin['top'], $fore, $font, $text);
   if ($rotation != 0) {
     $image = imagerotate($image, $rotation, 0);
   }
