使用PHP函数include()包含png图像 [英] Using PHP function include() to include a png image

查看:462
本文介绍了使用PHP函数include()包含png图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好人,尽管有最著名的做法,但今天我还是决定这样做:

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

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

具有6张不同的.png图像.

With 6 diferent .png images.

可悲的是,在浏览器中只能看到6个中的2个.

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

为什么只显示6张图片中的2张?途中是否可能有数据丢失位?

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

谢谢您的时间:]

推荐答案

它不起作用,因为<img>标记的src属性不应包含图像的原始数据.而是应该包含一个指向图像数据的URI.

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.

通过使用data: URI,您可以将图像直接嵌入(X)HTML文档中.请注意,这在许多浏览器(例如较旧版本的Internet Explorer)中将不起作用.还有一些限制,例如IE8在data: URI上的限制为32KB.

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.

使用PHP,这是您的代码的样子:

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")); ?>'>

如果您使用的图像类型发生变化,请不要忘记更改URL的image/png部分.例如,如果您使用GIF图像,请将其更改为image/gif.

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.

这篇关于使用PHP函数include()包含png图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆