0

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. ;)

Community
  • 1
  • 1
Calmarius
  • 18,570
  • 18
  • 110
  • 157
  • Specify what kind of errors you get???... – Arun David Mar 29 '11 at 09:19
  • "Fatal error: Allowed memory size of 25165824 bytes exhausted (tried to allocate 40840 bytes) in getactivityplot.php on line 67" (deliberately increased the image size to provoke it.) – Calmarius Mar 29 '11 at 09:29
  • Why generate an image that's so many times larger than the highest screen resolution that any of your users is likely to be running? – Mark Baker Mar 29 '11 at 09:34
  • No users see it. It's an admin script. I download it every day and open it in an image viewer and I'm scroll it around. – Calmarius Mar 29 '11 at 10:24
  • Do you (a) want to workaround the memory exhausted error or (b) generate an image with in constrained memory limit? – Salman A Mar 29 '11 at 11:02
  • Also, do you get the error while manipulating the image (using GD drawing functions) or do you get the error when trying to export it (using imagepng). – Salman A Mar 29 '11 at 11:03
  • First I determine the size of the image and I got the error when I call imagecreatetruecolor. – Calmarius Mar 31 '11 at 09:34
  • You could also generate a SVG vectorial image. To crate a SVG file you don't need any library, and you won't get to the memory limit so fast. You only have to output XML text and the browser will do the rest. http://www.w3schools.com/svg/ . Of course it's not that easy like GD. – Falk Apr 17 '15 at 07:09

2 Answers2

2

Set the memory limit to unlimited before processing the image.

ini_set('memory_limit', '-1');
Arun David
  • 2,714
  • 3
  • 18
  • 18
  • It will be good for a short term solution. But maybe in the future it's possible that the entire system memory would be too small to make the image... – Calmarius Mar 29 '11 at 09:37
0

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?

Michael J.V.
  • 5,499
  • 1
  • 20
  • 16
  • Added an example image. I'm using the GD library. I create the image using imagecreatetruecolor and output it directly to the browser using imagepng. I asked this question because it seems there is no (or I overlooked sg) gd fn that can output an image in multiple parts, only the whole thing once. – Calmarius Mar 29 '11 at 10:45
  • PHP time limit is 10secs and it's not a problem (yet). – Calmarius Mar 29 '11 at 10:50
  • How about outputting the whole thing to a file instead of browser if you are not doing it already and then just access the image directly? Another thing that might be happening is that your server is under load and there really isn't sufficient memory to allocate to output the darned thing :) – Michael J.V. Mar 29 '11 at 11:44
  • The error occurs when I generate the image. Even imagecreatetruecolor can't execute because the image is too big. – Calmarius Mar 31 '11 at 09:36
  • That indicates that the server you're using is under heavy load and can't allocate resources you need. – Michael J.V. Mar 31 '11 at 09:42
  • As Arun David said there is a memory limit in the php.ini. That's why I get the error. I inserted the line he provided (it works now by the way) but turning of the memory limit is just a symptomatic treatment. Output buffer can't be a problem, because the png image after the compression is only several kilobytes. – Calmarius Mar 31 '11 at 09:51
  • To be honest, I've never used GD lib for image manipulation purposes due to billions of errors it produces - unluckily for you - you hit one. Just to be on the safe side when it comes to PHP, try to see whether you have Suhosin installed on top of everything. Suhosin is a separate patch with its own config that overrides PHP's default one so it might be that you have Suhosin not giving your script resources it needs. – Michael J.V. Mar 31 '11 at 10:00