PHP HTML image output

后端 未结 4 1227
终归单人心
终归单人心 2021-02-08 08:02

In the PHP manual for base64_encode() I saw the following script for outputting an image.



        
相关标签:
4条回答
  • 2021-02-08 08:29

    Because imagepng outputs bool or image stream directly to output.
    So, in order to get image data you should use output buffers like this:

    ob_start();
    imagepng($im);
    $image = ob_get_contents();
    ob_end_clean();
    echo '<img src="data:image/png;base64,'.base64_encode($image).'" />';
    
    0 讨论(0)
  • 2021-02-08 08:30

    Ok, sorry, I was thinking too fast :)

    imagepng() will output raw data stream directly to the browser, so you must use ob_start() and other output buffering handles to obtain it.

    Here you are:

    ob_start();
    imagepng($yourGdImageHandle);
    $output = ob_get_contents();
    ob_end_clean();
    

    That is - you need to use $output variable for you base64_encode() function.

    0 讨论(0)
  • 2021-02-08 08:45

    Most likely because the data: URI scheme is extremely limited and good to use unless there is absolutely no way around it.

    In Internet Explorer, for example, this doesn't work at all until IE 8; and in there, there is a global 32 kilobyte limitation for data: URIs.

    0 讨论(0)
  • 2021-02-08 08:48

    You have to save your image as a PNG first, and then read from it to get its contents as a value.

    http://www.php.net/manual/en/function.imagepng.php

    imagepng() does not return the PNG file. It outputs it directly to the browser and then returns a boolean meaning success or failure.

    (from php.net:) PHP internally works with a temporary file when sending the image to the browser, so you'll gain nothing by calling imagepng() twice.

    0 讨论(0)
提交回复
热议问题