Solved

Upload picture check size

Posted on 2006-10-29
4
268 Views
Last Modified: 2010-04-16
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
Comment
Question by:avo42
  • 2
4 Comments
 
LVL 6

Expert Comment

by:0xC0DEB07
ID: 17830268
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
 

Author Comment

by:avo42
ID: 17832057
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
 
LVL 6

Accepted Solution

by:
0xC0DEB07 earned 250 total points
ID: 17835271
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
 
LVL 8

Assisted Solution

by:YoungBonzi
YoungBonzi earned 250 total points
ID: 17972725
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now