Zend - The PHP Company




Graphics

Add Code


Remote image checker  

Type: code fragment
Added by: robF
Entered: 16/06/2003
Last modified: 06/12/2002
Rating: - (fewer than 3 votes)
Views: 4200
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.


<?php
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($cCURLOPT_URL$l);
        
curl_setopt ($cCURLOPT_NOBODYTRUE);
        
curl_exec($c);
        
$size curl_getinfo($cCURLINFO_CONTENT_LENGTH_DOWNLOAD);
        
$type curl_getinfo($cCURLINFO_CONTENT_TYPE);
        
curl_close($c);

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

    endif;

    return 
$ret;
}
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories