2010-04-09 47 views
4

我們的開發服務器最近升級到PHP v5.2.13。隨着升級,我們發現我們的PNG圖像有字距(字母間距)問題。我們嘗試了許多字體,但還沒有找到解決方案。PHP更新字體問題imagettftext()和imagefttext()函數

我們使用GD庫創建圖像,並使用字體文件和imagettftext()或imagefttext()函數將圖像寫入圖像。

有沒有其他人遇到這樣?我誤解了什麼,或者應該將它作爲錯誤提交給PHP?有沒有很酷的解決方法我還沒有想到呢?

下面是一個新的和舊的tahoma大膽的例子。其他字體(粗體和非粗體)具有相同的問題。有些字母和數字看起來像是偏離中心或類似的東西。

壞 - 新的PHP http://img651.imageshack.us/img651/4894/bade.th.jpg」邊界= '0'/> http://yfrog.com/i3badej

好 - 老PHP v5.2.11(因爲這是我們的開發服務器而另一個是活服務器) http://img651.imageshack.us/img651/2067/goodl.png'border ='0'/> http://yfrog.com/i3goodlp

提前致謝!

尼爾

編輯:嗯,試圖嵌入使用ImageShack的圖像,但沒有奏效。改爲添加鏈接。

+0

什麼的foreach'的輸出(陣列( 'GD_VERSION', 'GD_EXTRA_VERSION', 'GD_BUNDLED')爲$ c)中的回波$ C, ':',常數($ c)中, 「\ n」 個; '在兩個版本的PHP? – VolkerK 2010-04-09 19:35:38

+0

GD_VERSION:2.0.35 GD_EXTRA_VERSION:GD_BUNDLED:1 在兩臺服務器上相同。 – 2010-04-10 02:19:20

回答

3

「跟蹤」是一個類似的術語,用於設置文本的緊密或鬆散。你可能有更好的運氣,例如this result

+0

這很有趣。謝謝,我會檢查出來。我們有很多代碼,我們希望不必取代數百個函數調用,但目前可能沒有更好的選擇。 : - /當然我會在星期一的時候添加一個搜索詞,當我返回時。 – 2010-04-10 02:22:42

0

感謝我們使用的字體,Kerning沒有爲我們工作,所以我們必須爲AV,AW等特定字母組合進行手動字距調整。

/** 
* This function lets you write a string with your own letter spacing ($t) 
* and kern specific letter combinations like AV 
* 
* @param type $im An image resource, returned by one of the image creation functions 
* @param type $size The font size. Depending on your version of GD, this should be specified as the pixel size (GD1) or point size (GD2). 
* @param type $angle The angle in degrees, with 0 degrees being left-to-right reading text. Higher values represent a counter-clockwise rotation. For example, a value of 90 would result in bottom-to-top reading text. 
* @param type $t Letter Spacing 
* @param type $k Kerning Spacing 
* @param type $x The coordinates given by x and y will define the basepoint of the first character (roughly the lower-left corner of the character). This is different from the imagestring(), where x and y define the upper-left corner of the first character. For example, "top left" is 0, 0. 
* @param type $y The y-ordinate. This sets the position of the fonts baseline, not the very bottom of the character. 
* @param type $color The color index. Using the negative of a color index has the effect of turning off antialiasing. See imagecolorallocate(). 
* @param type $font The path to the TrueType font you wish to use. 
* @param type $text Text to write/print to the image 
*/ 
function ImageTTFTextWithSpacing($im, $size, $angle, $t, $k, $x, $y, $color, $font, $text) { 
    $numchar = strlen($text); 
    for($i = 0; $i < $numchar; $i++) { 
     # Assign character 
     $char[$i] = substr($text, $i, 1); 

     //Top is wider than bottom of character 
     $up = ['Y','V','W']; 
     //Bottom is wider than top of character 
     $down = ['A']; 
     //From the second letter on 
     if($i > 0 && 
       //check whether we have TOP and BOTTOM type character 
       //next to each other so we need to adjust spacing 
       ((in_array($char[$i], $up) && in_array($char[$i-1], $down)) || 
       (in_array($char[$i-1], $up) && in_array($char[$i], $down)))) { 
      $w -= $k; 
     } 

     # Write character 
     imagettftext($im, $size, $angle, ($x + $w + ($i * $t)), $y, $color, $font, $char[$i]); 

     # Get width of character 
     $width = imagettfbbox($size, $angle, $font, $char[$i]); 
     $w = $w + $width[2]; 
    } 
}