Solved

Upload picture check size

Posted on 2006-10-29
4
282 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 109
Using VBscript & RegEx, how can I match lines starting with Oracle word 17 75
VB.net and sql server 4 45
C qsort compare function issue 6 16
A short article about a problem I had getting the GPS LocationListener working.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

840 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