2012-03-05 77 views
0

我在嘗試合併文本字符串與圖像。我在Stackoverflwo上找到了關於如何執行此操作的示例,但是當我加載腳本時,所有的操作都是亂碼。我認爲這是一個字體問題,但我不知道如何解決這個問題。我已經將True type字體(arial)放在與腳本相同的目錄中。建議?這裏是我的代碼:將圖像與圖像合併

<?php 


$im = imagecreatefromjpeg('image.jpg'); 

//The numbers are the RGB values of the color you want to use 
$black = ImageColorAllocate($im, 255, 255, 255); 

//The canvas's (0,0) position is the upper left corner 
//So this is how far down and to the right the text should start 
$start_x = 10; 
$start_y = 20; 

$font = 'arial.ttf'; 


Imagettftext($im, 12, 0, $start_x, $start_y, $black, $font, 'text to write'); 

//Creates the jpeg image and sends it to the browser 
//100 is the jpeg quality percentage 

Imagejpeg($im, '', 100); 

ImageDestroy($im) 
?> 

回答

1

你不是告訴瀏覽器你實際上正在發送一個圖像,所以它試圖將二進制數據解釋爲文本。

爲了讓瀏覽器知道它即將收到圖像,您必須發送一個內容類型。您需要在imagejpeg()之前放置此行。

header('Content-Type: image/jpeg'); 
+0

謝謝,這是做的伎倆,非常豐富! – user1009453 2012-03-05 15:17:34

+0

不客氣。在附註中,以小寫形式寫出函數名稱被認爲是很好的做法。 – kba 2012-03-05 15:19:21

0

Per the documentation,第二個參數imagejpeg()應爲空,如果你不保存到文件,而不是一個空字符串:

Imagejpeg($im, NULL, 100);