[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Get dimensions of an image on upload can this be done

Posted on 2007-03-17
5
Medium Priority
?
588 Views
Last Modified: 2008-03-25
Is it possible without the use of imagemagick or GD or such like with php alone to get the dimensions of an image that is being uploaded?

I mean, lets say some user wants to upload an image 1000w x 600h pixels and we dont want an image that large on our server could this upload code we are using be modified to terminate the upload and give a warning dialog  stating the max dimensions we will allow?

Here is our code

<?

$i=0;




    if (is_dir ("gallery/".$userdata['username']."")) {
       
    }
      else
      {
         $mypath="gallery/".$userdata['username']."";
           mkdir($mypath,0777);
      }



if ($_FILES["file"]["size"] < 20000000)
      {
              if ($_FILES["file"]["error"] > 0)
                     {
                      echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
                }
             else
                {
                        echo "Uploaded: " . $_FILES["file"]["name"]. $i . "<br>";
                        while (file_exists("gallery/".$userdata['username']."/" . $_FILES["file"]["name"]. $i))
                            {
                                    $i++;
                                  }

                        move_uploaded_file($_FILES["file"]["tmp_name"], "gallery/".$userdata['username']."/" . $_FILES["file"]["name"] . $i);
                        echo "Stored in: " . "".$userdata['username']."/" . $_FILES["file"]["name"] . $i;
                        echo '<br><br>';
                }
              }
 
  $url =  "gallery/".$userdata['username']."/" . $_FILES["file"]["name"]. $i;
 
 
$uploadimage = 'INSERT INTO gallery (`user_name`, `user_id`, `image_url`, `image_desc`, `image_name`, `image_website`) VALUES (\''.$_POST['username'].'\',\''.$userdata['user_id'].'\', \''.$url.'\', \''.$_POST['desc'].'\', \''.$_POST['name'].'\', \''.$_POST['web'].'\');';
 mysql_query($uploadimage);

0
Comment
Question by:vbMarkO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Accepted Solution

by:
babuno5 earned 2000 total points
ID: 18742705
check this function out
http://in2.php.net/getimagesize
0
 
LVL 14

Expert Comment

by:raja_ind82
ID: 18742870
------------------------------------------------------------------------
<?php
//$a_imagesize = getimagesize($_FILES['uploaded_filename']['tmp']);
$a_imagesize = getimagesize("Sunset.jpg");
print_r($a_imagesize);
?>
-------------------------------------------------------------------------------------
[0] = width
[1] = height
[2] = type
[3] = "width=x heigh=y" for the <img> tag.

Returns an array with 4 elements.

Index 0 contains the width of the image in pixels.
Index 1 contains the height.
Index 2 is a flag indicating the type of the image:
 1 = GIF,
 2 = JPG,
 3 = PNG,
 4 = SWF,
 5 = PSD,
 6 = BMP,
 7 = TIFF(intel byte order),
 8 = TIFF(motorola byte order),
 9 = JPC,
 10 = JP2,
 11 = JPX,
 12 = JB2,
 13 = SWC,
 14 = IFF,
 15 = WBMP,
 16 = XBM.
 These values correspond to the IMAGETYPE constants that were added in PHP 4.3.0.
Index 3 is a text string with the correct height="yyy" width="xxx" string that can be used directly in an IMG tag.

NOTE: Just because getimagesize can return types in a lot of images doesn't mean it can process them. TIFF files is a good example!

i hope this will help you.

Regards,
M.Raja
0
 
LVL 1

Expert Comment

by:gobinathm
ID: 18792392
list($width, $height, $type, $attr) = getimagesize($file)

$file is nothing but the name of the file you have uploaded. By this method you can get it in seperate variables instead of getting all the values in an array.

Now you will have you width in the variable $width & height in the variable $height.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21207274
Forced accept.

Computer101
EE Admin
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question