The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 9 of 9

Thread: [PHP] Image size and dimensions

  1. #1

    [PHP] Image size and dimensions

    I made a form that sends an email with an image attachement. Now, how do I check the file size in kilobytes, dimensions (width and height) in pixels, and file type (gif or jpg)?

    Is it something like:

    $att_size = $_FILES['att']['size'];

  2. #2
    Member #2 of the "I wont critique Timmytot's designs anymore" club.

  3. #3
    How is this to be used in conjuntion with $_FILE? I must provide a address path for the getimagesize() function, but the image is not stored anywhere. Or is it? Is it located at $_FILES['att']['tmp_name'] ?

  4. #4
    size and type are all properties of your $_FILE object.

    For dimensions you would have to use the function seb posted above meaning taking the uploaded image, saving it somewhere where you can access it and use it with the gd functions.

  5. #5
    My image validation is not working. When I submit an image that doesn't meet size/dimension/type criteria, no error message shows.

    PHP Code:
    $design     $_FILES['design'];

    if(
    $design)
    {
        
    $att         $_FILES['design'];
        
    $att_path     $_FILES['design']['tmp_name'];
        
    $att_size     $_FILES['design']['size'];
                
        list(
    $width$height$type$attr) = getimagesize("$att_path");
                
        if(
    $width 640)
            
    $designStatus1 "<br />*too wide";
        if(
    $height 480)
            
    $designStatus2 "<br />*too tall";
        if((
    $type != 1) || ($type != 2))
            
    $designStatus3 "<br />*not GIF/JPG";
        if(
    $att_size 8192 150)
            
    $designStatus4 "<br />*file size too large";


  6. #6
    anyone?

  7. #7
    The first problem that I can pick out is the if statement.

    here's your variable, where you say that $design will store the design file:

    $design = $_FILES['design'];

    and here's your if statement, saying that if $design equals true:

    if ($design) {
    // statements
    }

    you won't be evaluating to see if $design is true or not, but you will be evaluating to see if $design is holding anything. you had the right idea, but just the wrong syntax. another thing is you never printed the error messages - you set the $designstatus variables to hold the particular message but never printed them on the screen.

    Also please note that my PHP skills are rudimentary and there is probably a better way to do this, but this will work for now.

    Try this:

    PHP Code:
    // set the design variable to hold something
    $design $_FILES['design'];

    // check to see if the variable is holding anything.
    // if the length of the name of the file that $design is storing is longer than 0, it is storing a variable.
    if (strlen($design) > 0) {
    echo 
    ' $design is now storing an image';
    }
    else { 
    // if the length is less than 0 then the variable is holding nothing and you print the error message.
    $design NULL;
    echo 
    '$design is not storing anything!';

    This should point you in the right direction. You can replace the echoes with your error messages.

    Hope this helps

  8. #8
    Quote Originally Posted by NeoDreamer View Post
    My image validation is not working. When I submit an image that doesn't meet size/dimension/type criteria, no error message shows.

    PHP Code:
    $design     $_FILES['design'];

    if(
    $design)
    {
        
    $att         $_FILES['design'];
        
    $att_path     $_FILES['design']['tmp_name'];
        
    $att_size     $_FILES['design']['size'];
                
        list(
    $width$height$type$attr) = getimagesize("$att_path");
                
        if(
    $width 640)
            
    $designStatus1 "<br />*too wide";
        if(
    $height 480)
            
    $designStatus2 "<br />*too tall";
        if((
    $type != 1) || ($type != 2))
            
    $designStatus3 "<br />*not GIF/JPG";
        if(
    $att_size 8192 150)
            
    $designStatus4 "<br />*file size too large";


    Your not echoing the error message.To make it show do this

    PHP Code:
    echo $designStatus1
    Member #2 of the "I wont critique Timmytot's designs anymore" club.

  9. #9
    Exactly what Seb said, but instead of using all those $designStatus variables, just predefine $designStatus as "" and then make all of those 'if' statements change $designStatus to something else if they're true.
    COLOURlovers | Member #2 of the kirupa XBox 360 Club
    "I think most people would agree that your computer breaking is a Christmas miracle." ~shane-c to Timmytots

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012