Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: check image

  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts

    check image

    hi

    i have a func to check if an image is valid, works perfect for upload from pc an url:
    PHP Code:
        function checkitsaValidImage($files, array $aConfig) {
            
    $imageInfo getimagesize($files);
            
    $sResult '';
                
            if(!
    $imageInfo) {
                
    trigger_error('Error not an image.');
            }
                
            if(!empty(
    $imageInfo) && !in_array($imageInfo['mime'], $aConfig['allowed_mime_types'])) {
                
    trigger_error('Error invalid file.');
            } else if(
    $imageInfo[0] > $aConfig['max_width'] || $imageInfo[1] > $aConfig['max_height']) {
                
    trigger_error('Error image size too  big.');
            } else if(
    filesize($files) > configMaxSize($aConfig['max_filesize'])) {
                
    trigger_error('Error file size too big.');
            }
            else
            {
                
    $sResult image_type_to_extension($imageInfo[2], true);
            }
            return 
    $sResult;
        } 
    only issue is the filesize(); func WILL not check URLs/remote, erm i'm wondering can a globalFS(); func that can get remote/URLs AND pc uploads and then call it in place of filesize();?

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    filesize() can check some remote URLs but your ISP may have disabled fopen wrappers.

    If you need to check URLs, cURL may be your best bet (perhaps custom method of HEAD will still return the content length header) but that's going to be flaky too as some pages just won't tell you.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #3
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    I can do it via curl, hmm

    can i do;

    PHP Code:
    globalFS()
    {
       if(
    filesize($files)) {
       }
       else
       {
           
    // try curl
       
    }

    an then call it instead of filesizze()?

  • #4
    New Coder
    Join Date
    Jul 2011
    Location
    USA
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    This is what I use to check size of remote images and it seems to work pretty good:

    PHP Code:
    $string file_get_contents($url);
    $length strlen($string); 

  • #5
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    hi guys,

    can u tell me where nim goin wrong[;
    PHP Code:
        function handleSize($files)
        {
            if(
    filesize($files)) 
            {
                
    // It's a local file.
            
    } else 
            {
                
    // It's remote file.
                
    $ch curl_init($files); // Get the filesize using curl
                
    curl_setopt($chCURLOPT_NOBODYtrue);
                
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
                
    curl_setopt($chCURLOPT_HEADERtrue);
                
    curl_setopt($chCURLOPT_FOLLOWLOCATIONtrue); 
                
    $data curl_exec($ch);
                
    curl_close($ch);
                if (
    $data === false
                {    
                    echo 
    "<p align='center' classs='select_text'>Unable to upload your image as you didn't provide a valid URL</p>";
                    exit;
                }
        
                
    $remoteSize 'unknown';
                
    $status 'unknown';
        
                if (
    preg_match('/^HTTP\/1\.[01] (\d\d\d)/'$data$matches)) 
                {
                    
    $status = (int)$matches[1];
                }
                
                if (
    preg_match('/Content-Length: (\d+)/'$data$matches)) 
                {
                    
    $remoteSize = (int)$matches[1];
                }
            }
        } 
    im gettin
    Code:
    Unable to upload your image as you didn't provide a valid URL

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    If you have spaces in the filename:

    PHP Code:
    $url str_replace(' ''%20'$files);
    $ch curl_init($url); // Get the filesize using curl 
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #7
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Sorry Andrew, I dont follow................

  • #8
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by Chris-2k View Post
    Sorry Andrew, I dont follow................
    Spaces in the URL need to be replaced with a %20.
    mentioned in the user contributed notes of the docs.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #9
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    oh yea thx m8, some people misread my question....

    I am not looking HOW TO do this, rather A WAY to do it... I want to use curl, all funcs i've written til now are fail, my final attempt:

    PHP Code:
        function handleSize($files)
        {
            if(
    $files
            {
                
    // It's a local file.
                
    $size filesize($files);
            } else 
            {
                
    // It's remote file.
                 
    $ch curl_init($url);
            
                 
    curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
                 
    curl_setopt($chCURLOPT_HEADERTRUE);
                 
    curl_setopt($chCURLOPT_NOBODYTRUE);
            
                 
    $data curl_exec($ch);
                 
    $size curl_getinfo($chCURLINFO_CONTENT_LENGTH_DOWNLOAD);
            
                 
    curl_close($ch);
            }
            return 
    $size;
        } 
    now for my chck func can i do:

    PHP Code:
    if(handleSize($files) > $size)
    {
       
    // error


  • #10
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    anyone can help?

  • #11
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You seem to have introduced the variable $url:

    Code:
    // It's remote file.
    $ch = curl_init($url);
    If you are adopting my code sample then you need both lines.

    I am not sure about your distinction between HOW TO and A WAY to, but the documentation is quite clear in insisting that spaces should be removed from the url. But perhaps your url doesn't have any spaces.
    Last edited by AndrewGSW; 08-19-2012 at 06:31 PM.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •