To defend versus Remote File Inclusion where attackers try to abuse image files, I usually recommend to never use include to include image files into PHP code.
Sometimes though, the avoidance of image includes may be not possible at all (for whatever reasons, doesn't matter).
In such case I usually pickup the images somewhere in the upload process and convert them to another image format (sometimes im combination with a lossy compression) to hopefully destroy any malicious code possibly contained in the original image.
This works, but I'm not quite satisfied with it. Mainly because of the additional server load such processing produces and possible image quality decreases that sometimes may happen.
Are the any smarter ways or best practices on that?
EDIT
To clarify: I'm talking about a siutation where the attacker injects PHP code into an image file to get the injected code executed on server side after uploading the image. Forums for example allow users to upload avatars (small image files) for personalisation.
include? Images are HTTP resources in their own right, sothey're not "included" as such within an HTML (source) page. (You could usefopen/fpassthroughdirectly, for example.)I'd also be concerned about the harm you can do to your users: there is a certain responsibility in sending your users one of your pages with items that you don't necessarily trust, especially if they're ultimately sent by your server.
– Bruno May 18 '11 at 20:14includean image file (like, a JPEG or something) and execute its contents. That seems absurd. It's not going to cause anything useful to happen. It seems pointless. If the goal is to display an image to the user of the web site, why aren't you including it in the HTML with<IMG SRC="/path/to/the.jpg">? – D.W. May 19 '11 at 06:29