views:

34

answers:

2

Working on a prebuilt system that grabs remote images and saves them to a server.

Currently there is no checking on the image as to whether it indeed exists at that remote location, and it is of a certain file type (jpg, jpeg, gif) and I'm tasked with doing both.

I thought this was quite trivial as I'd simply use a simple regex and getimagesize($image):

$remoteImageURL = 'http://www.exampledomain.com/images/image.jpg';

if(@getimagesize($remoteImageURL) && preg_match("/.(jpg|gif|jpeg)$/", $remoteImageURL) )
{
    // insert the image yadda yadda.
}

The problem occurs when I don't have any control over the url that I'm grabbing the image from, for example:

http://www.exampledomain.com/images/2?num=1

so when it comes to this, both the regex and getimagesize() will fail, is there a better way of doing this?

A: 

Store the remote file on your server then run getimagesize on the local one.

fabrik
+7  A: 

You could do

$headers = get_headers( 'http://www.exampledomain.com/images/2?num=1' );

The $headers variable will then contain something like

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Thu, 14 Oct 2010 09:46:18 GMT
    [2] => Server: Apache/2.2
    [3] => Last-Modified: Sat, 07 Feb 2009 16:31:04 GMT
    [4] => ETag: "340011c-3614-46256a9e66200"
    [5] => Accept-Ranges: bytes
    [6] => Content-Length: 13844
    [7] => Vary: User-Agent
    [8] => Expires: Thu, 15 Apr 2020 20:00:00 GMT
    [9] => Connection: close
    [10] => Content-Type: image/png
)

This will tell you whether the resource exists and what content-type (which is not necessarily also the image type) it is.

EDIT as per Pekka's comment, you still might want to determine it's mime-type after downloading. See

Some of the given approaches work on remote files too, so you can probably skip the get_headers pre-check altogether. Decide for yourself which one suits your needs.

Gordon
+1 that was easy.
Sarfraz
+1 this is going to be totally sufficient for a first check. `getimagesize()` (that actually parses the file's header bytes) can be the second check after it's downloaded
Pekka
This is great, thank you. Means I can get rid of the regex now as well and check for 200 OK to ensure image exists before going any further.
creminsn