使用PHP函数include()包含png图像

使用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.