我想將一些文本打印到PNG文件中。我已經嘗試了一切,但沒有運氣。以JPG格式顯示文本
這裏是我的代碼:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="image/jpeg" />
</head>
<body>
<?php
// Create the image
$im = imagecreatetruecolor(100, 30);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = 'Testing...';
// Replace path by your own font path
$font = 'fonts/FRE3OF9X.TTF'; //HAVE CHECKED THIS AND FILE EXISTS
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
imagejpeg($im);
imagedestroy($im);
?>
</body>
</html>
我曾嘗試過各種字體文件,並檢查該文件正在被改變文件名,看看我得到一個錯誤發現。是否有任何額外的東西,我需要安裝在服務器上,使這項工作?
輸出爲亂碼文本(例如tuvwxyz )。
感謝您的回覆。我把它留在了原來的位置,因爲我在頁面的其餘部分需要它(未包含在代碼中)。然而,僅僅爲了測試的目的,我把它從我的代碼中移除了,並且我得到了不同的垃圾。 –
您無法從同一頁面打印圖像和html。作爲對HTTP請求的響應,您必須發回一個特定的內容類型。 「我只是得到了不同的垃圾」:這實際上是生成的圖像,將圖像標記加載到圖像標記中,您將看到,或者嘗試在新的瀏覽器會話中打開它。 –
我提供的代碼是當前我在頁面上的所有內容(取出文本/ html內容類型)。當你說加載到圖像標籤的URL,你的意思是在我的網頁上有
其中barcode.php是上面提供的代碼? –