使用 PHP 将图像分割为像素 div
这很难解释,但出于好奇,我想用 PHP 抓取一个图像,将其分成 1 个像素部分,然后执行 foreach 并将每个像素分配给一个 div 作为图像/背景图像。因此,当图片显示在屏幕上时,它看起来是完整的,但实际上它是很多 1px div。
使用GD或类似的库,这可能吗?
(另外,如果使用 Javascript 和 Canvas 更容易,那么我有兴趣知道)
This is tricky to explain, but for curiosities sake, I'd like to grab an image with PHP, split it into 1 pixel parts and do a foreach and assign each pixel to a div as an image/background image. So when the picture is displayed on the screen it looks whole, but its actually lots of 1px divs.
Using GD or a similar library, is this possible?
(Also, if this is easier with Javascript and Canvas then i'd be interested to know)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如其他人所说,您需要使用 imagecolorat 函数来选择每个像素的图像,然后根据需要循环并渲染元素。
只是因为我发现这比实用更有趣,所以我尝试了一下。
下面是一些快速组合在一起的代码,演示了其中的几个输出。 image.jpg 是一张 50x39px 的猴子图像,相当于 1950 个 1x1px 图像。下图显示了输出 - 第一个图像是原始图像,第二个图像是以 1x1px 图像矩阵作为 div 背景的图像,第三个图像是从原始图像中选取的十六进制颜色,然后
background:#xxxxxx; 在那个 1x1px div 上。请注意,如果您对此进行测试,请使用小图像!因为显然它会为原始图像中的每个像素生成一个图像。
对于未来的读者来说,这更多的是一个概念证明,不应该在生产环境中使用!
编辑:屏幕截图中的第三张图像显然未正确渲染 - 但由于 minitech 的建议,现在可以正常显示。下面的代码已被修改以反映此更改。
只是稍微玩了一下,这里也实现了相同的效果画布和 jQuery。不过,没有 jQuery 也可以轻松完成。
As others have said, you'd need to use the imagecolorat function to select the image at each pixel, then loop through and render the elements as necessary.
Just because I found this interesting more so than practical I had a play around with it.
Below is some quickly thrown together code which demonstrates a couple of outputs from this. image.jpg is a 50x39px image of a monkey which amounts to 1950 1x1px images. The image below shows the output- the first image is the original, the second is the one with a matrix of 1x1px images as div backgrounds, the third is the hex colour picked from the original then
background:#xxxxxx;
on that 1x1px div. Be warned, if you test this use a small image! as obviously it generates an image for each pixel in the original image.For future readers, this is more a proof of concept and should not be used in a production environment!
Edit: The third image in the screenshot obviously isn't rendered correctly- however this now works thanks to a suggestion by minitech. The code below has been amended to reflect this change.
Just had a bit more of a play around with it and here's the same achieved with canvas and jQuery. It could easily be done without jQuery though.
当然。您所需要的只是 GD 的
imagecolorat()
。结果将是一个非常非常大的文件,使得除了实验之外的几乎所有事情都变得不切实际。Sure. All you need is GD's
imagecolorat()
. The result is just going to be a really, really big file, making this impractical for almost everything beyond an experiment.我对你的问题很感兴趣,所以我尝试制作一些东西来创建带有 html 标签的图像,但尺寸太大了,无法使用。我花了一天剩下的时间,通过创建一个动态样式表将任何网络安全的十六进制颜色压缩为 3 个字符版本,调整多个连续像素的宽度以跨越该数量的列,并删除图像的边框,从而使其尽可能小用 CSS 填充替换它。
从 300x300 像素 png 86kb 创建一个 1.1mb 等效的 html
使用仅包含几种颜色的图像可以获得更好的结果,例如 Twitter 徽标,“仅”以 78kb 输出
这是 40x40 像素 twitter 徽标的输出
I was intrigued with your question so I had a go at making something that would create an image with html tags but the size was so huge it was unusable. I spent the rest of the day making it as small as possible by creating a dynamic stylesheet compressing any web safe hex colors into 3 character versions, adjusting the width of multiple consecutive pixels to span that number of columns and removing the border of an image and replacing it with css padding.
From a 300x300 pixel png 86kb it creates a 1.1mb html equivalent
Far better results can be made with images containing only a few colors like the twitter logo which "only" comes out at 78kb
Here's the output of a 40x40 pixel twitter logo
哎呀,这是函数:
http://www.php.net/manual/en/function.imagecolorat.php
但这样做会非常非常非常低效,因为不是每个像素传输一个数字,而是使用 div 传输一个字符串,所以不要指望加载时间会更短。
Jep, here's the function:
http://www.php.net/manual/en/function.imagecolorat.php
But it would be very very VERY inefficent to do this, because instead of transfering one number per pixel you are transferring a string with the div, so don't expect a shorter loadingtime.