简体   繁体   English

显示来自 PHP 的 PNG 文件

[英]Displaying PNG file from PHP

I'm using a PHP library to display a small png image.我正在使用 PHP 库来显示一个小的 png 图像。 It works fine placed into its own PHP file, right here: https://www.sidesay.co/test_draw.php它可以很好地放入自己的 PHP 文件中,就在这里: https://www.sidesay.co/test_draw.php

The code is quite simple.代码非常简单。

<?php

    require './libraries/Sparkline-master/autoload.php';
    
    $sparkline = new Davaxi\Sparkline();
    $sparkline->setData(array(2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16));
    $sparkline->display();
    
?>

But when I include the above code into my larger php file for displaying with the rest of the webpage, I get junk data where the image should be, that looks like this:但是,当我将上述代码包含到我较大的 php 文件中以与网页的 rest 一起显示时,我会在图像应该出现的位置得到垃圾数据,如下所示:

�PNG IHDRPغ� pHYs���+�IDATX��OLZw�?���4H�@\'j�DOzX�d��SM��,f.��%���Poۚ�^l�fa�lz/�^�ɥä�9�AuB��C�{(�oP�):u�����}�߇���C�(��e18��q��Rĥ̡� �D�n1A�9��R�2�C)��I��ID9�A_���L_�A�// “PNG IHDRP” pHYs...+...IDATX...OLZw...?...4H...@\'j...DOzX...d...SM...,f....%...Poۚ...^l... lz/�^�ɥä�9�AuB��C�{(�oP�):u�����}�������C�(��e18��q��Rĥ̡���D�n1A �9��R�2�C)��I��ID9�A_�L_�A�//

etc.等等

Any ideas?有任何想法吗?


EDIT: I've created a quick PHP file to show the problem.编辑:我创建了一个快速 PHP 文件来显示问题。 Including a Header does not resolve the problem.包括 Header 并不能解决问题。 It only works without HTML or if I place the PHP at the top it works but my HTML will not display at all.它只能在没有 HTML 的情况下工作,或者如果我将 PHP 放在顶部它可以工作,但我的 HTML 根本不会显示。

<html>
<head>
    <title>TEST</title>
</head>

<?php

    header('Content-type: image/png');
    require './libraries/Sparkline-master/autoload.php';
    
    $sparkline = new Davaxi\Sparkline();
    $sparkline->setData(array(2,4,5,6,10,7,8,5,7,7,11,8,6,9,11,9,13,14,12,16));
    $sparkline->display();

?>


<body>
    <br>
    <p>Not displaying</p>
</body>
</html>

The output to this can be seen here: output 可以在这里看到:

https://www.sidesay.co/error_draw.php https://www.sidesay.co/error_draw.php

I have also tried using the imagepng function but the junk data persists.我也尝试过使用 imagepng function 但垃圾数据仍然存在。

imagepng($sparkline->display());

You cannot display any html in this file.您不能在此文件中显示任何 html。

But, to display the PNG, you have to add this:但是,要显示 PNG,您必须添加以下内容:

<?php
    header('Content-Type: image/png');
?>

Adding this will make the browser display only an image:添加这将使浏览器仅显示图像: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM