簡體   English   中英

使用PHP在html文檔中顯示帶有原始圖像數據的PNG

[英]Display PNG with raw image data within an html document using PHP

使用此代碼,我嘗試從JSON創建簽名圖像,然后直接在瀏覽器中顯示圖像而不保存它。 我遇到的問題是我想在HTML文檔中顯示此圖像,因此標題信息已被修改。 有沒有辦法在HTML文檔中顯示原始圖像數據中的圖像? 或者圍繞這個問題的任何其他方式?

$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json);  //get image resource from json

    // HERE I WANT TO DISPLAY THE IMAGE BUT GET Cannot modify header information
header('Content-Type: image/png');
imagepng($img, null, 0, PNG_NO_FILTER);
imagedestroy($img);

當然,你可以一起使用兩件事。

HTML Image標記支持base64編碼圖像。 對於html來說它可能很大,但會起作用。 您還可以使用ob_start和ob_end函數將圖像輸出到緩沖區。 http://us2.php.net/manual/pt_BR/function.ob-start.php

所以對於PHP文件,你可以這樣做:

$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json);  //get image resource from json

ob_start(); // Start buffering the output
imagepng($img, null, 0, PNG_NO_FILTER);
$b64 = base64_encode(ob_get_contents()); // Get what we've just outputted and base64 it
imagedestroy($img);
ob_end_clean();

// Print the HTML tag with the image embedded
echo '<img src="data:image/png;base64,'.$b64.'"/>';

這將生成帶有嵌入圖像的HTML Img標記。 希望這就是你想要的。

PS:請記住,這會增加HTML數據的加載時間,因為如果您只是鏈接它,瀏覽器會打開另一個下載圖像的線程。

你可以試試這個

<img src="/path/to/yourimage.php">

在yourimage.php中。

$json = $ticket->sig; // get the json representing the signature
$img = sigJsonToImage($json);  //get image resource from json

    // HERE I WANT TO DISPLAY THE IMAGE BUT GET Cannot modify header information
header('Content-Type: image/png');
imagepng($img, null, 0, PNG_NO_FILTER);
imagedestroy($img);

暫無
暫無

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

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