2016-04-25 32 views
0

我想在動態生成的PHP圖像中使用these icons,這樣人們就可以在網頁上嵌入天氣。如何在PHP生成的圖像中使用天氣圖標Web字體?

現在我正在使用met.no的天氣服務,它提供了一個稱爲「符號」的整潔屬性並且是一個整數。 1 =晴朗,2 =多雲等天氣圖標回購的Pull Request包括met.no的映射是很酷的,因爲我可以撥打echo "<i class='wi wi-yrno-$symbol'></i>";並始終獲得正確的圖標。

但我不知道如何使用PHP的圖像創建功能來做到這一點。在Javascript中,你可以這樣做:Programmatically get FontAwesome unicode value by name,但我將如何在PHP中做到這一點?我想我只需要製作一組array("wi-yrno-1 => "(unicode escape code here)", "wi-yrno-2" => "(another escape code here) ...,但我想知道是否有更好的方法。

回答

0

我假設您正在服務器上生成一個映像,然後將該內容發送到瀏覽器。

您提供的JavaScript示例是無關緊要的。主要是因爲字體已經加載並映射到類名。在PHP中,你將不得不重新創建這個映射。將符號簡單映射到十進制值就可以工作。例如。

$symbols = [0 => '&#xf00d;', 1 => ..., 2 => ..., ...]; 

設置完映射後,您需要使用的字體文件的絕對路徑。在知道您將使用imagettftext在給定的ttf字體文件的圖像上繪製文本後。它必須是一個ttf字體文件(因此是該函數的名稱)。

// the symbol response from the met.no request 
$symbolIntFromApi = 0; 

// the mapping for the decimal characters 
$symbols = [0 => '&#xf00d;']; 

// create the image 
$image = imagecreatetruecolor(1000, 1000); 

// set background color to white 
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255)); 

// write the text using the given font 
imagettftext($image, 12, 0, 20, 20, 0, realpath('./font/weathericons-regular-webfont.ttf'), $symbols[$symbol]); 

// save the image 
imagepng($image, 'test.png'); 
+0

謝謝。我認爲是這樣,但有時當我正在尋找我遇到的問題的答案時,我看到W語言可以做X,Y和Z,當他們看起來像不兼容的技術時,我會感到很失望。 – DrFrankly