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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Limiting a file upload by dimensions (wxh)

    Hi all,

    I have an image upload facility at present, but due to the design of my site I can only allow users to upload images of certain dimensions.. I've seen a lot of stuff on file size restrictions but never come across anything that limits by dimensions.

    I have in the past done resizing, but I've always found the quality turns out rubbish! Since I want to limit the image uploads to be 468(w)x60(h)... if the user uploaded lets say a square image then the end result would be terrible.

    Is it possible to actually limit files by their dimensions?

    Many thanks,

    Greens85
    Last edited by greens85; 07-07-2010 at 12:40 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can, but only after the file has been uploaded to your server. Before that you have no way of knowing anything about the file. After upload you can check the dimensions using getimagesize().

    http://us3.php.net/manual/en/function.getimagesize.php

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You can use the PHP function getimagesize to check the size of the image during validation... if the size isn't what you want, return an error instead of proceeding.

    http://php.net/manual/en/function.getimagesize.php

  • #4
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Hi guys,

    Thanks for the responses, I always have a hard time following the php manual, perhaps because i'm a newbie im not sure...

    I have tried my best to understand it and added the following to my existing code:

    PHP Code:
    $ran time();
            
    $imgExtArr explode('.'$_FILES['bannerimg']['name']);
            
    $imgExt $imgExtArr[1];
            
    $imageName $ran.'.'.$imgExt;
            
    $uploadpath 'images/ads/banners/'.$imageName;
            
    move_uploaded_file($_FILES['bannerimg']['tmp_name'],$uploadpath);
            [
    COLOR="Red"]$size getimagesize('$uploadpath');
            echo 
    $size;[/COLOR]
            exit(); 
    However when I run the script I just get an error of:

    Warning: getimagesize($uploadpath) [function.getimagesize]: failed to open stream: No such file or directory in /path/to/file on line 55

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    $size = getimagesize('$uploadpath');
    You are making silly mistakes. PHP doesn't parse the php variable inside single quotes. Remove the quotes and you'll get a different result.

    PS: I'd recommend you to check the php manual pages of getimagesize() to see the return values and usage before posting another "doesn't work" reply
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by abduraooft View Post
    You are making silly mistakes. PHP doesn't parse the php variable inside single quotes. Remove the quotes and you'll get a different result.

    PS: I'd recommend you to check the php manual pages of getimagesize() to see the return values and usage before posting another "doesn't work" reply
    Hi Abduraooft,

    I have made a fair amount of progress now, so hopefully it is fine for me to post

    This is what I have now:

    PHP Code:
    $root $_SERVER['DOCUMENT_ROOT'];
            
    $completePath $root.'/'.$uploadpath;
            
    $ImageDetails getimagesize($completePath);
            
    $width $ImageDetails[3];
            echo 
    $width
    And this displays:

    width="468" height="60"

    Which is obviously exactly what I am after, however I was expecting the width and height to be returned as different parts of the arrays...

    at which point I was going to add an if statement like so:

    PHP Code:
    if ($width !=486 && $height !=60) {
    // failed
    } else {
    // go ahead

    But now they have been returned as one, I'm slightly confused.

  • #7
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Not to worry I found a way to do it:

    For anyone who is interested or having the same problems...

    PHP Code:
    list ($width$height) = getimagesize($completePath);
            
            echo 
    $width;
            
    '<br/>';
            echo 
    $height
    That will separate the width and height

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    In the manual it tells you getimagesize() returns width in the 0 index and height in the 1 index. So you could just do:

    PHP Code:
            $ImageDetails getimagesize($completePath);
            
    $width $ImageDetails[0]; 
            
    $width $ImageDetails[1]; 
    But your solution kind of does the same thing. Just so you understand, they are returned separately for you.

  • #9
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,535
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Sorry Fumigator, just have to do this... copy/paste fail!

    PHP Code:
            $ImageDetails getimagesize($completePath);
            
    $width $ImageDetails[0]; 
            
    $height $ImageDetails[1]; 

  • #10
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Whoops


  •  

    Posting Permissions

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