• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

Upload picture check size

Hi all

can anyone please give me an example

on my page i have a browse button and code to upload a image to my server (see code bellow)

what i would like to do is ammend this code so that it will check the size is NOT bigger than 450 px wide and 450 px high

if it is to return a false

if its within 450 x 450 to return a true

all help appriciated

[CODE]
      //add image
            $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/'.$cats.'/fur/images/';
//            
            $filename = $_FILES['image']['name'];
            $uploadfile = $uploaddir . $_FILES['image']['name'];
          if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {
            rename ($uploadfile, $uploaddir . $last_id . ".jpg");
            chmod ($uploaddir . $last_id . ".jpg", intval(0644, 8));
            }
[/CODE]
0
avo42
Asked:
avo42
  • 2
2 Solutions
 
0xC0DEB07Commented:
Hi

Try this one

<?php
...

function isSizeGood($fname){
      
      $arr = getimagesize($fname);
      
      if($arr[0]<450 && $arr[1]<450)
            return true;
      return false;
}
...
?>      

You can check out the php documentation  for more info on getimagesize()
Hope this helps
0
 
avo42Author Commented:
Hi Thanks


That will work good if the file is allready on my server .
but how would i do it via this form

<form id="form1" name="form1" method="post" action="<?=$PHP_SELF?>">
<table width="500" border="0">
  <tr>
    <td width="236"><input name="image" type="file" id="image" /></td>
    <td width="254">
        <input type="submit" name="Submit" value="Submit" />
    </td>
  </tr>
</table>
</form>

thanks again all help appriciated
0
 
0xC0DEB07Commented:
Hi avo42

PHP is a server-side thing you can't get the size of a image that's not your server.
However you could use the temporary $_FILES['image']['tmp_name'] to perform the image
size check if the function returns true you can upload the file with:

move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)

if it returns false you wont upload it and the temporary file will be destroyed.
0
 
YoungBonziCommented:
This is tested in FF2.0 an IE7:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function getImgSize(imgSrc, flag){
      if(!flag){
            var newImg = new Image();
            imgSrc="file://localhost/"+imgSrc;
            newImg.src =imgSrc;
            }
      else
            var newImg = imgSrc;
            if (typeof newImg.complete=='boolean'&&(newImg.complete&&newImg.height*1>0)){
                  var height = newImg.height;
                  var width = newImg.width;
                  if(width>450 || height>450) {alert ('The image size is too big');}           //check img dimensions

                  else if (typeof newImg.complete=='boolean'){
                        setTimeout(function(){getImgSize(newImg,'x')}, 3000);
                        document.write('uploading...');                                                    //replace with your upload code                        
                  }
                  else alert('Image cannot be evaluated for size');                                   //haven't tested other browsers
            }
      }
</script>



</head>
<body>
<input type="file" onchange="getImgSize(this.value)">
</body>
</html>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now