如何使用PHP将大图像输出到浏览器?
我有一个用 PHP 动态生成的非常大的图像并输出到浏览器。 (它宽 5000 像素,高 1000-2000 像素。这是我网站上的每日用户活动图)。
问题是现在的情节太大了,PHP 脚本给出了内存耗尽的错误(尽管生成的 PNG 本身很小),因此我无法获取图像。
有没有办法使用 GD 以 PNG 格式以多种方式输出这个大图像?
(ps:我运行网站的主机使用安全模式,所以我无法修改配置,我认为他们正在使用默认的 PHP 安装。)
EDIT1:这是一个管理脚本。除了我之外没有用户看到它。
EDIT2:示例图像可以在这里看到: http://users.atw.hu /calmarius/trash/wtfb2/x.png (我还可以选择按 IP 地址对轨迹进行分组。)
每个用户+IP 对在绘图上都有自己的 24 小时轨迹。每个绿色标记表示一次用户活动。正如您所看到的,该图像可以逐轨输出。并且无需一次性输出和生成整个内容。
该网站将是一个在线策略游戏,我希望将来使用该图来更轻松地检测多帐户。 (那些试图通过注册多个帐户来获得优势的用户,而不是那些只有 1 个帐户的用户。)但这是一个不同的问题。
我使用 PHP 脚本是因为我懒得从数据库导出请求日志、下载它并将数据提供给可以为我绘制绘图的程序。 ;)
I have a very large image generated on the fly with PHP and outputted to the browser. (it's 5000px wide and 1000-2000px tall. It's a plot of the daily user activity on my site).
The problem is that nowadays the plot is too big and the PHP script gives memory exhausted errors (tough the generated PNG itself is quite small) and I can't get the image due to this.
Is there way to output this large image in multiple parts somehow using GD in PNG format?
(ps: the host where I run the site uses safe mode, so I can't modify the configuration and I think they're using the default PHP installation.)
EDIT1: It's an admin script. No users see it except me.
EDIT2: and example image can be seen here: http://users.atw.hu/calmarius/trash/wtfb2/x.png
(I also have the option to group the tracks by IP address.)
Every user+IP pair has its own 24 hour track on the plot. And every green mark denotes an user activity. As you can see this image can be output track by track. And there is no need to output and generate the whole thing all once.
This website will be an online strategy game and I want to use this graph in the future to make detecting multiaccounts easier. (Users who are trying to get advantage by registering multiple accounts over those ones who only have 1.) But this is a different problem.
I'm using PHP script because I'm too lazy to export the requestlog from the database, download it and feed the data to a program that would make the plot for me. ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在处理图像之前将内存限制设置为无限制。
Set the memory limit to unlimited before processing the image.
说明如何生成图像(GD 库、ImageMagick)以及如何输出它会有所帮助。您是否将文件保存到目录然后使用 readfile() 输出它?如果是,fopen / fread / echo 组合比使用 readfile() 将文件输出到浏览器快约 50%-60%。您使用 gzip 压缩吗? php执行的时间限制是多少?您收到的确切错误消息是什么?
It'd help to say how you're generating the image (GD library, ImageMagick) and how you're outputting it. Are you saving the file to a directory and then using readfile() to output it? If yes, fopen / fread / echo combination is about 50%-60% faster than using readfile() to output files to the browser. Are you using gzip compression? What's the time limit on php execution? What's the exact error message you're getting?