views:

51

answers:

4

Hello. I have image hosting. All image requests redirect to a PHP script with mod_rewrite. PHP script uses function fread() and displays picture from another file. I want to know, does this use a lot of processor time or not?

A: 

Yes, this is putting considerable strain on the web server, because the PHP interpreter has to be initialized for every small resource request, and passes through the data. The consensus is that this is not a good thing to do on a high-traffic website.

Why are you doing this, are you resizing images?

Pekka
No. All images are already resized. I want to control all images. E. g. I want to know image seen count, I want to disabled image and another functions.
Leonid
It would be better to do that on file system level IMO. Every access is logged in Apache, and you can filter them out from there. You can disable images by using `htaccess` directives or renaming/deleting them.
Pekka
But also I want to send a not real image file. E. g. I want send image to user with text "image is temporarily disabled" or "use small prewie image" and another. How I can do it by using htaccess?
Leonid
Check out the `-f` flag in mod_rewrite. You could write a rewrite_rule that passes through the image file if it exists, and redirects to a "image is disabled" image resource if the image doesn't exist (`!-f`).
Pekka
+1  A: 

It depends on how much you think "a lot of processor time" is, but from what you're describing, the processing time required by mod_rewrite and PHP is trivial compared to the I/O time to read the image from disk and send it over the network.

If you're concerned about speed, caching the images in memory will probably have the most benefit.

benzado
A: 

You will run out of memory before hitting CPU limit :-) Reading/writing file is not CPU-intensive task, but each apache process created for that can eat up to 50 MB of RAM.

Qwerty
A: 

If you want to send images fast and secure, you should look into X-SendFile - this allows your php scripts to tell your Webserver to send files not directly accessible by url using something like header('X-SendFile: /path/to/the/file');

For apache there is mod_xsendfile (http://tn123.ath.cx/mod_xsendfile/) though labeled beta, it has proven to be very stable in production, and the its sourcecode is rather small and can be audited easily.

roman