Jiew Meng
Jiew Meng

Reputation: 88317

Any way i can check if a url points to a valid image (using JS/PHP/ZendFramework)?

i am developing a test/learning app. i wonder how can i test if an image from another site/domain ... i broke up my validation logic to the following

Upvotes: 1

Views: 351

Answers (2)

cypher
cypher

Reputation: 6992

  1. checking if url exists:

    $handle = curl_init($url); curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); $response = curl_exec($handle); $status = curl_getinfo($handle, CURLINFO_HTTP_CODE); if($status == 404) {//return true or whatever}

  2. is an image, is of valid type: determine if the file extension is valid, for example using

    preg_match('/(jp[e]?g|png|gif|etc...)$/i', $url);

  3. is of specific dimensions: use GD to create a resource and then check size by getImageSize($resource)

  4. is below max size: in addition to step 1 - $size = curl_getinfo($handle, CURLINFO_SIZE_DOWNLOAD);

Remember that you must have cURL and GD enabled.

Upvotes: 0

Daniel Egeberg
Daniel Egeberg

Reputation: 8382

getimagesize() can do all but the last of your points. For that you can use filesize(). For filesize() you'll have to actually download it though, but seeing as getimagesize() also requires that, you can just save it to a temporary file. You can use tempnam() to get a temporary file that doesn't conflict with others.

Upvotes: 4

Related Questions