Solved

Upload picture check size

Posted on 2006-10-29
4
288 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
[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
  • 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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
Basic Java Case or If-Else statement... 3 64
Definitions and default visual studio colors 5 74
ejb message driven bean mdb creation steps 2 35
Is online banking safe? 11 87
A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn different types of Android Layout and some basics of an Android App.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

749 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