使用 PHP 函数 include() 包含 png 图像
好吧,尽管有最著名的做法,今天我还是决定这样做:
<img src='<? include("dir/dir/img.png"); ?>'>
使用 6 个不同的 .png 图像。
遗憾的是,这 6 个中只有 2 个在浏览器上清晰可见。
为什么 6 张图像中只显示了 2 张?也许途中存在数据丢失?
谢谢您的宝贵时间:]
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 :]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它不起作用,因为
标签的
src
属性不应包含图像的原始数据;相反,它应该包含指向图像数据的 URI。通过使用
data:
URI,您可以将图像直接嵌入到 (X)HTML 文档中。请注意,这在许多浏览器中不起作用,例如旧版本的 Internet Explorer。此外,还有一些限制,例如 IE8 对data:
URI 的 32KB 限制。使用 PHP,您的代码如下所示:
如果您使用的图像类型发生变化,请不要忘记更改 URL 的
image/png
部分。例如,如果您使用 GIF 图像,请将其更改为image/gif
。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 ondata:
URIs.Using PHP, here's what your code would look like:
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 toimage/gif
.那根本不应该起作用。
有关执行此操作的标准方法(包括 HTML 文档中内联的图像,而不是指向其 URL),请参阅
数据
URI 方案。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.include()
告诉 PHP 解析该文件。如果万一它包含,你就会遇到麻烦了。相反,请使用
readfile()
。此外,Artefacto 的答案也必须考虑。
include()
tells PHP to parse that file. If, by any chance, it contains<?
, you’ll be in real trouble. Instead, usereadfile()
.Additionally, Artefacto’s answer has to be considered as well.