簡體   English   中英

header('內容類型:圖片/png'); 使瀏覽器全黑

[英]header('Content-type: image/png'); makes the browser all black

這是來自 PHP.net 的腳本

// Create the image handle, set the background to white
$im = imagecreatetruecolor(100, 100);
imagefilledrectangle($im, 0, 0, 100, 100, imagecolorallocate($im, 255, 255, 255));    
// Draw an ellipse to fill with a black border
imageellipse($im, 50, 50, 50, 50, imagecolorallocate($im, 0, 0, 0));
// Set the border and fill colors
$border = imagecolorallocate($im, 0, 0, 0);
$fill = imagecolorallocate($im, 255, 0, 0);
// Fill the selection
imagefilltoborder($im, 50, 50, $border, $fill);
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);

一行 HEADER 使瀏覽器全黑! 為什么? 怎么了?

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

強制您的瀏覽器將您的輸出解釋為 png 類型的圖像。

即使您的代碼很好並且不會產生錯誤。 您很可能缺少 GD 或您的環境配置不當並返回錯誤。 但是由於header ,錯誤輸出被解釋為圖像。

嘗試刪除它並確保顯示的字符串是正確的原始 png 數據。

編輯:使用 OP 進行調試后,發現他的腳本在聲明打開的 php 標簽<?php之前有一個空格字符。 這個小東西也與正確的 png 數據一起發送,並弄亂了瀏覽器解釋它的整個數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM