Solved

Upload picture check size

Posted on 2006-10-29
4
255 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
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 …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 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

18 Experts available now in Live!

Get 1:1 Help Now