diff -urp textimage/textimage.module textimage/textimage.module --- textimage/textimage.module 2008-03-08 14:11:19.000000000 +1100 +++ textimage/textimage.module 2008-11-10 15:55:42.000000000 +1100 @@ -1000,56 +1000,78 @@ function textimage_wrap_text($text, $fon * A GD image resource. */ function textimage_text_to_image($text, $fontsize, $font, $foreground_color = '#000000', $background_color = NULL, $angle = 0, $maximum_width = 0, $fixed_width = 0, $align = ALIGN_LEFT) { + $rad = deg2rad($angle); + $sin = sin($rad); + $cos = cos($rad); + // Perform text wrapping, if necessary. if ($maximum_width > 0) { $text = textimage_wrap_text($text, $fontsize, $font, $maximum_width); } // Get exact dimensions of text string - $box = imageTTFBbox($fontsize, $angle, $font, $text); + $bbox = imagettfbbox($fontsize, 0, $font, $text); + // Calculate text width and height - $iw = abs($box[4] - $box[0]) + 4; - $ih = abs($box[5] - $box[1]) + 4; - - if($fixed_width) { - $background_width = $maximum_width; + if ($angle == 0) { + $width = ($fixed_width) + ? $maximum_width + : $bbox[2] - $bbox[0]; + + $height = $bbox[1] - $bbox[7]; } + else { - $background_width = $iw; + $width = ($fixed_width) + ? round(abs($maximum_width * $cos) + abs(($bbox[1] - $bbox[7]) * $sin)) + : round(abs(($bbox[2] - $bbox[0]) * $cos) + abs(($bbox[1] - $bbox[7]) * $sin)); + + $height = ($fixed_width) + ? round(abs(($bbox[1] - $bbox[7]) * $cos) + abs($maximum_width * $sin)) + : round(abs(($bbox[1] - $bbox[7]) * $cos) + abs(($bbox[2] - $bbox[0]) * $sin)); } // Use a transparent background color if (empty($background_color)) { - $img = _textimage_create_transparent_image($background_width, $ih); + $image = _textimage_create_transparent_image($width, $height); } + // Use a solid background color else { - $img = imagecreatetruecolor($background_width, $ih); + $image = imagecreatetruecolor($width, $height); + list($r, $g, $b) = _textimage_hex2rgb($background_color); - $back = imagecolorallocate($img, $r, $g, $b); - imagefill($img, 0, 0, $back); + $back = imagecolorallocate($image, $r, $g, $b); + + imagefill($image, 0, 0, $back); } - - // Create the text image - list($r, $g, $b) = _textimage_hex2rgb($foreground_color); - $fore = imagecolorallocate($img, $r, $g, $b); - switch($align) { + // Calculate text alignment + switch ($align) { case ALIGN_RIGHT: - $x = $maximum_width - $iw; + $tx = $bbox[6] + ($maximum_width / 2) - ($bbox[0] + $bbox[2]); break; + case ALIGN_CENTER: - $x = ($maximum_width - $iw) / 2; + $tx = $bbox[6] - (($bbox[0] + $bbox[2]) / 2); break; + case ALIGN_LEFT: default: - $x = 0; + $tx = -($bbox[6] + ($maximum_width / 2)); break; } - imagettftext($img, $fontsize, $angle, $x, abs($box[5]), $fore, $font, $text); - - return $img; + $ty = -$bbox[7] - ((-$bbox[7] + $bbox[3]) / 2); + $py = imagesy($image) / 2; + $px = imagesx($image) / 2; + + $x = intval($tx * $cos + $ty * $sin + $px); + $y = intval(-$tx * $sin + $ty * $cos + $py); + + imagettftext($image, $fontsize, $angle, $x, $y, $fore, $font, $text); + + return $image; } /**