[英]PHP - echo image as jpg not ascii
我試圖在fopen之后回顯圖像並將其讀取,但是它顯示為ascii代碼而不是圖像格式。
我找到了答案:
我在代碼中實現了header()行,但似乎它嘗試將當前的php文件打開為jpg而不是圖像。
這是我的代碼:
$filename = $n.".jpg";
//echo $n.'.jpg'; prints image_name.jpg
$fp = fopen($filename, "r");
$data = fread($fp, filesize($filename));
header('Content-type: image/jpg'); //without the header line I can print the ascii
echo $data; //I want to print my $data as image format not ascii
fclose($fp);
(我不知道這是否重要,但我使用的是最新版本的XAMPP和W7)
提前致謝
如果使用fopen,請嘗試
$fp = fopen($filename, "rb");
強制執行二進制模式。 我很懶,用這個:
$filename = $n.".jpg";
header('Content-type: image/jpg'); //without the header line I can print the ascii
echo file_get_contents($filename);
我想知道為什么您想通過腳本而不是直接提供圖像來輸出圖像,因為它已經是jpg圖像了。
盡管如此,也許您應該嘗試使用fpassthrough在文件指針上輸出所有剩余數據,而不是回顯文件內容
$filename = $n.".jpg";
$fp = fopen($filename, "r");
header('Content-type: image/jpg'); //without the header line I can print the ascii
fpassthru($fp)
fclose($fp);
您也可以直接使用http://php.net/manual/en/function.readfile.php調用readfile函數,而不必使用fopen
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.