3

I have a page that loads images dynamically via a file called thumb.php

This file takes the large image file, makes a thumbnail and outputs a jpg like so:

<img src="thumb.php?width=100&height=100&cropratio=1:1&image=/photos/45_Jumps-059.jpg" />

I have a problem on my server where when I reach around 60 requests (requests thumb.php 60+ times for all the images), the response I get back from the server is:

Remote server closed the connection before sending response header

and the image fails to load.

Is this like apache or php running out of memory or something? It stalls my whole server for about a minute before it starts working again.

Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • Looks like a memory problem. – diegoperini Oct 03 '13 at 17:23
  • would that be apache memory or php though? – Ronnie Oct 03 '13 at 17:24
  • I believe it is PHP. What do you use to output (echo, gd?) your image? – diegoperini Oct 03 '13 at 17:26
  • The script generates a cached file and saves it to an `/imagecache` folder. It will then use `$image = ob_get_contents()` to read the image file, set headers and then `echo $image` – Ronnie Oct 03 '13 at 17:34
  • Benjamin has provided a great optimization feature in this topic before: http://stackoverflow.com/questions/1851849/output-an-image-in-php – diegoperini Oct 03 '13 at 17:43
  • Hmm, it's a godaddy shared host so I doubt I'll have the ability to activate mod_xsendfile if it isn't already. I'll look into it, thanks. – Ronnie Oct 03 '13 at 17:53
  • I still can't figure this out. I increased the memory limit from 60M to 512M via php.ini and same thing happens – Ronnie Oct 03 '13 at 22:49
  • Do you use ob_end_clean after ob_get_contents to clean the buffer? Or try ob_get_flush instead. – Makc Oct 05 '13 at 08:22
  • It's worth double-checking to see if the memory limit was actually increased. (You can check most things with [phpinfo](http://php.net/manual/en/function.phpinfo.php)). Bear in mind that GoDaddy may limit the memory whatever you put in your php.ini, and also that you may need to [restart things](http://support.godaddy.com/help/article/5647/why-isnt-my-phpini-file-taking-effect) for changes to take effect. – Matt Gibson Oct 06 '13 at 20:34

1 Answers1

1

The issue is most likely the maximum connection limit in Apache. It's set so the server can continue to function when it's being hit with too many simultaneous connections. It's easily configurable, but you mention you are on a shared host so it's unlikely you'll be able to change this.

In your scenario I would spread out the image requests by loading a few on page load, then use JavaScript to watch when the load is complete and add more images. Example with JQuery (untested):

<img src="" data-src="thumb.php?width=100&height=100&cropratio=1:1&image=/photos/45_Jumps-059.jpg" />
<img src="" data-src="thumb.php?width=100&height=100&cropratio=1:1&image=/photos/46_Jumps-059.jpg" />
<img src="" data-src="thumb.php?width=100&height=100&cropratio=1:1&image=/photos/47_Jumps-059.jpg" />

<script>
    $("img[src='']").load(function() {
       var next = $("img[src='']:first");
       next.attr('src', next.data('src');
    });
    var first = $("img[src='']:first");
    first.attr('src', first.data('src');
</script>
Matt S
  • 14,976
  • 6
  • 57
  • 76
  • Well, this may solve on a single page only. But, apache must be getting request from many clients machines. There should be more sophisticated solutions to this issue! – Gaurang Jadia Oct 06 '13 at 23:57
  • It's also possible the shared host limits simultaneous connections from a single IP to help prevent DDOS attacks. This will solve that problem. – Matt S Oct 07 '13 at 00:53
  • I think you're right matt about apache limiting the connections. I think your solution could have worked, but I ended up paginating the images placing about 40 per page and it all seems well now – Ronnie Oct 07 '13 at 16:13