2012-07-20 88 views
1

時完全看文本PHP的imagettftext創建一個有趣的看文本,當我創建一個角度的文本。由於某種原因,使用PHP和imagettftext和角度

下面的源代碼。我無法發佈圖片,因爲我沒有足夠的聲望點,但文字看起來像是部分字母被切掉。

幫助!!!


$text = 'My Text Is Messed Up!!!'; 
$font = './fonts/arial.ttf'; 
$font_size = 20; 
$font_multiplier = 0.5; 

$x=10; 
$y=190; 
$angle=45; 
$width= ($font_size * $font_multiplier) * strlen($text); 
echo $width; 
$height=200; 

$size = imageTTFBBox($font_size, $angle, $font, $text); 
$img = imageCreateTrueColor($width, $height); 
imageSaveAlpha($img, true); 
ImageAlphaBlending($img, false); 

$transparentColor = imagecolorallocatealpha($img, 200, 200, 200, 127); 
imagefill($img, 0, 0, $transparentColor); 
$white = imagecolorallocate($img, 255, 255, 255); 

// Add the text 
imagettftext($img, $font_size, $angle, $x, $y, $white, $font, $text); 

// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($img, 'welcome-phrase.png'); 
imagedestroy($img); 

編輯:這裏是輸出的一個例子(我改變文字顏色從白色到黑色,使其在白色背景上可見 - AG):

enter image description here

+2

我附上一個示例輸出給你。必須同意你的看法:這真的很奇怪。 – 2012-07-20 17:58:32

+0

呃。任何機會你可以使用ImageMagick? – 2012-07-21 19:33:20

+0

確實。你嘗試過其他字體嗎? – 2012-07-21 19:41:04

回答

1

它似乎有一個問題,它旋轉每個字符,並留下不旋轉的種類的「面具」,然後模糊它周圍的文字,導致你看到的問題。當您關閉透明圖像填充時,它更加明顯。

解決方法可能是旋轉圖像而不是文本。你將不得不解決您的座標,但這樣的事情似乎工作:

// Add the text 
imagettftext($img, $font_size, 0, $x, $y, $black, $font, $text); 


$img = imagerotate($img, $angle, $transparentColor); 
imageSaveAlpha($img, true); 
ImageAlphaBlending($img, false); 

因此,完整的代碼是:

$text = 'My Text Is Messed Up!!!'; 
$font = './fonts/arial.ttf'; 
$font_size = 20; 
$font_multiplier = 0.5; 

$x=10; 
$y=190; 
$angle=45.0; 
$width = ($font_size * $font_multiplier) * strlen($text); 
echo $width; 
$height=200; 

$size = imageTTFBBox($font_size, $angle, $font, $text); 
$img = imageCreateTrueColor($width, $height); 


$transparentColor = imagecolorallocatealpha($img, 200, 200, 200, 127); 
imagefill($img, 0, 0, $transparentColor); 
$white = imagecolorallocate($img, 255, 255, 255); 

// Add the text 
imagettftext($img, $font_size, 0, $x, $y, $white, $font, $text); 


$img = imagerotate($img, $angle, $transparentColor); 
imageSaveAlpha($img, true); 
ImageAlphaBlending($img, false); 
// Using imagepng() results in clearer text compared with imagejpeg() 
imagepng($img, 'welcome-phrase.png'); 
imagedestroy($img); 

我感動的imageSaveAlpha和ImageAlphaBlending至底部照顧所有的在旋轉發生之後。這不是最好的解決方案,但一些調整將提供正確的結果。