Using PHP function include() to include a png image

不问归期 提交于 2020-01-02 05:35:12

问题


Ok people, despite the best-known-practices, today I decided to do this:

<img src='<? include("dir/dir/img.png"); ?>'>

With 6 diferent .png images.

Sadly, only 2 of the 6 were nicely visible on the browser.

Why only 2 of the 6 images were shown? Maybe there were data losses bits on the way?

Thank you for your time :]


回答1:


It does not work because src attribute of an <img> tag is not supposed to contain the raw data of an image; rather, it is supposed to contain a URI that points to the image data.

By using data: URIs, you can embed the image directly in your (X)HTML document. Note that this will not work in many browsers such as older versions of Internet Explorer. As well, there are limits, such as the 32KB limit IE8 places on data: URIs.

Using PHP, here's what your code would look like:

<img src='data:image/png;base64,<?php echo base64_encode(file_get_contents("dir/dir/img.png")); ?>'>

Don't forget to change the image/png part of the URL if the type of image that you are using changes. For example, if you use a GIF image, change it to image/gif.




回答2:


That was not supposed to work at all.

For a standard way to do that (including images inline in the HTML document instead of pointing to their URL), see the data URI scheme.




回答3:


include() tells PHP to parse that file. If, by any chance, it contains <?, you’ll be in real trouble. Instead, use readfile().

Additionally, Artefacto’s answer has to be considered as well.




回答4:


< img src='< ?php echo 'data:image/png;base64,' . base64_encode(file_get_contents('dir/dir/img.png')) ; ?> ' >


来源:https://stackoverflow.com/questions/3463598/using-php-function-include-to-include-a-png-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!