PhpSnip.com

User Stats

Remote image checker

If you have a message board which allows people to post inline images with the IMG tag, you can use this function to run a few checks on the remote image. Simply pass it the URI to the image, and it checks the file exits; that it is a recognised image format; and that it is smaller than a predeterminted size. This stops mischief-makers linkng your page to a 4TB file.

Info

 Download  View Source (print view)
 Rating : 4.7  Views : 322

Source Code ( 30 lines )

function validate_image($l, $maxsize = 20000) {

    // Check for the existance and hopefully the size and type of that
    // image. We'll return an HTML string if successful, and an integer if
    // not. Returns true if the image is okay, false if not

    $ret = true;

    if (!$ih = @fopen($l, "r"))
        $ret = false;

    // Looks like the image exists. If we've got the mighty cURL, we can do
    // a bit more checking

    if (function_exists("curl_init")):
        $c = curl_init();
        curl_setopt($c, CURLOPT_URL, $l);
        curl_setopt ($c, CURLOPT_NOBODY, TRUE);
        curl_exec($c);
        $size = curl_getinfo($c, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
        $type = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
        curl_close($c);

        if ($size > $maxsize || !ereg("^image/", $type))
            $ret = false;

    endif;

    return $ret;
}

Search

Subscribe

  Rss Feeds

Sponsors

Advertise