2015-08-28 95 views
0

我正在運行此代碼,它創建一個640x1136透明PNG,並在左上角廣告一個黑色文本的白色框。PHP使文本透明PNG

運行時創建的代碼PNG但這返回輸出

?PNG 

IHDR?z?? 
     IDATx???1 ?Om 
??>g???IEND?B`? 

做任何一個碰巧知道這是什麼?

與此同時,任何人都可以想到更短的方式?

$im = imagecreatetruecolor(640, 1136); 
imagesavealpha($im, true); 
$color = imagecolorallocatealpha($im, 0, 0, 0, 127); 
imagefill($im, 0, 0, $color); 
imagepng($im); 
imagesavealpha($im, true); // important to keep the png's transparency 
$text_color = imagecolorallocate($im, 0, 0, 0); 
$width = 640; // the width of the image 
$height = 1136; // the heighst of the image 
$font_size = 20; // font size 
$box_color = imagecolorallocate($im, 255, 255, 255); 
// Set the offset x and y for the text position 
$offset_x = 0; 
$offset_y = 20; 
$dims = imagettfbbox($font_size, 0, $font, $text); 
$text_width = $dims[4] - $dims[6] + $offset_x; 
$text_height = $dims[3] - $dims[5] + $offset_y; 
// Add text background 
imagefilledrectangle($im, 0, 0, $text_width, $text_height, $box_color); 
// Add text 
imagettftext($im, $font_size, 0, $offset_x, $offset_y, $text_color, $font,$text); 
imagepng($im, $img, 0); 

回答

1

您應該與您的數據發送正確的header

header('Content-Type: image/png'); 

或者,如果你從一個控制檯努力,並希望直接輸出到一個PNG文件,你要麼跑它作爲php <yourscript>.php > filename.png或將imagepng($im);更改爲imagepng($im, 'filename.png');您的腳本正在執行的操作是將PNG輸出到輸出,並且您會看到原始PNG數據。

+0

問題依然存在。 –

+0

確保您將'header()'調用放在腳本的頂部。 – uri2x

+0

問題仍然存在 –