Current location: Hot Scripts Forums » Programming Languages » PHP » Image size controll


Image size controll

Reply
  #1 (permalink)  
Old 04-27-04, 07:13 AM
MasQ's Avatar
MasQ MasQ is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Norway/Oslo
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Image size controll

Hey ppl...

I have this image uploadingscript that looks like this:

Code:
<form action="" method="post" enctype="multipart/form-data" name="form1"> 
<input type="file" name="imagefile"> 
<input type="submit" name="Submit" value="Submit"> 
<?php
if(isset( $_POST['Submit'] )) 
{ 
//If the Submitbutton was pressed do: 
if ($_FILES['imagefile']['type'] == "image/pjpeg" || "image/gif"){
    copy ($_FILES['imagefile']['tmp_name'], $ID .'/pics/'.$_FILES['imagefile']['name']) 
    or die ("Could not copy"); 
        echo "<br>"; 
        echo "Name: ".$_FILES['imagefile']['name']."<br>"; 
        echo "Size: ".$_FILES['imagefile']['size']."<br>"; 
        echo "Type: ".$_FILES['imagefile']['type']."<br>"; 
        echo "Copy Done...."; 
        } 
		else { 
            echo "<br>"; 
            echo "Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")"; 
			}} 
?>
</form>
The thing is, that I dont want the images that users upload to be bigger then 350 pixels in width...
Is it posible to to this?

**Schmmotch**
Reply With Quote
  #2 (permalink)  
Old 04-27-04, 09:13 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
yes you can with getimagesize();
which will return an array with the dimensions of the image ..

one note about the security of this code you have ,, it's better to use move_uploaded_file() because it won't be fooled with a file not uploaded!!
read here for more information:
http://www.php.net/manual/en/functio...oaded-file.php
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 04-28-04, 02:16 AM
MasQ's Avatar
MasQ MasQ is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Norway/Oslo
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Damnit I love it when things works out!!

PHP Code:

<form action="" method="post" enctype="multipart/form-data" name="form1"> 

<input type="file" name="imagefile"> 
<input type="submit" name="Submit" value="Submit"> 
<?php
if(isset( $_POST['Submit'] )) { 
//If the Submitbutton was pressed do: 
$size=getimagesize($_FILES['imagefile']['tmp_name']);
if (
$_FILES['imagefile']['type'] == "image/pjpeg" || "image/gif"){
if (
$size[0] > 350){echo"<br>Error: The picture is to big, please reduce width to 350 pixels or less"; exit() ;}
    
copy ($_FILES['imagefile']['tmp_name'], $ID .'/pics/'.$_FILES['imagefile']['name']) 
    or die (
"Could not copy"); 
        echo 
"<br>"
        echo 
"Name: ".$_FILES['imagefile']['name']."<br>"
        echo 
"Size: ".$_FILES['imagefile']['size']."<br>"
        echo 
"Type: ".$_FILES['imagefile']['type']."<br>"
        echo 
"Width: $size[0] <br>"
        
echo "Copy Done...."
        } 
        else { 
            echo 
"<br>"
            echo 
"Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")"
            }} 
?> </form>
It worked like a charm!

But I didnt quite get how I was suppose to use the move_uploaded_file() function... Can't I just add some php at the bottom like:

if the file $name is "whereitssupposetobe" then: echo "File is veryfied"
else: echo "Error: The files just noe there... Shit"

Won't that work in the same way?
__________________
**Schmmotch**
Reply With Quote
  #4 (permalink)  
Old 04-29-04, 05:20 AM
MasQ's Avatar
MasQ MasQ is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Norway/Oslo
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Any thoghts on the subject?
__________________
**Schmmotch**
Reply With Quote
  #5 (permalink)  
Old 04-29-04, 05:30 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
about move_uplaoded_file() ..
just use it instead of copy() !!
it's safer ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #6 (permalink)  
Old 04-29-04, 05:34 AM
MasQ's Avatar
MasQ MasQ is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Norway/Oslo
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Fatal error: Call to undefined function: move_uplaoded_file() in /customers/themarigold.net/themarigold.net/httpd.www/medlemmer/upload.php on line 24

PHP Code:

<form action="" method="post" enctype="multipart/form-data" name="form1"> 

<input type="file" name="imagefile"> 
<input type="submit" name="Submit" value="Submit"> 
<?php
if(isset( $_POST['Submit'] )) { 
//If the Submitbutton was pressed do: 
$size=getimagesize($_FILES['imagefile']['tmp_name']);
if (
$_FILES['imagefile']['type'] == "image/pjpeg" || "image/gif"){
if (
$size[0] > 350){echo"<br>Error: The picture is to big, please reduce width to 350 pixels or less"; exit() ;}
    
move_uplaoded_file ($_FILES['imagefile']['tmp_name'], $ID .'/pics/'.$_FILES['imagefile']['name']) 
    or die (
"Could not copy"); 
        echo 
"<br>"
        echo 
"Name: ".$_FILES['imagefile']['name']."<br>"
        echo 
"Size: ".$_FILES['imagefile']['size']."<br>"
        echo 
"Type: ".$_FILES['imagefile']['type']."<br>"
        echo 
"Width: $size[0] <br>";
        echo 
"Copy Done...."
        } 
        else { 
            echo 
"<br>"
            echo 
"Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")"
            }} 
?> </form>
That didnt work... :/
__________________
**Schmmotch**
Reply With Quote
  #7 (permalink)  
Old 04-29-04, 05:37 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
sorry I mis-spelled the function name !!
it's: move_uploaded_file()
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #8 (permalink)  
Old 04-29-04, 05:43 AM
MasQ's Avatar
MasQ MasQ is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Norway/Oslo
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
LOL!
Its silly that I didn't notice it tho
Thanks for all the help NeverMind!
__________________
**Schmmotch**
Reply With Quote
  #9 (permalink)  
Old 05-05-04, 03:24 AM
MasQ's Avatar
MasQ MasQ is offline
Newbie Coder
 
Join Date: Feb 2004
Location: Norway/Oslo
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
Shit, CHMOD my ***...

Uhmme... Im having trouble CHMOD'ing the uploaded file!
I just tryed to use the CHMOD() function, but there is a problem with the permissions...

Upon uploading the file "jokkos.jpg" i get the following error message:
Warning: chmod(): Unable to access /pics/jokkos.jpg in /customers/themarigold.net/themarigold.net/httpd.www/medlemmer/upload.php on line 35

Script is looking like this:
PHP Code:

<form action="" method="post" enctype="multipart/form-data" name="form1"> 

<input type="file" name="imagefile"> 
<input type="submit" name="Submit" value="Submit"> 
<?php
if(isset( $_POST['Submit'] )) { 
//If the Submitbutton was pressed do: 
$size=getimagesize($_FILES['imagefile']['tmp_name']);
if (
$_FILES['imagefile']['type'] == "image/pjpeg" || "image/gif"){
// Makes sure that the width of the picture is less then 350 pixels
if ($size[0] > 350){echo"<br>Error: The picture is to big, please reduce width to 350 pixels or less"; exit() ;}
// Stops if the picture is a .bmp file, since these files are to big to use on a webpage
if ($_FILES['imagefile']['type'] == "image/bmp"){echo"<br>Error: This format is not supported (.bmp)"; exit() ;}
    
move_uploaded_file ($_FILES['imagefile']['tmp_name'], $ID .'/pics/'.$_FILES['imagefile']['name']) 
    or die (
"Could not copy"); 
        echo 
"<br>"
        echo 
"Name: ".$_FILES['imagefile']['name']."<br>"
        echo 
"Size: ".$_FILES['imagefile']['size']."<br>"
        echo 
"Type: ".$_FILES['imagefile']['type']."<br>"
        echo 
"Width: $size[0] <br>";
        echo 
"Copy Done....";
        
chmod('/pics/'.$_FILES['imagefile']['name'], 0644); 
        } 
        else { 
            echo 
"<br>"
            echo 
"Could Not Copy, Wrong Filetype (".$_FILES['imagefile']['name'].")"
            }} 
?> </form>
As you can see I just added the chmod() function at the end of the script...
Maby I should just use your simple upload script NeverMind, you seem to know what your dooing
__________________
**Schmmotch**
Reply With Quote
  #10 (permalink)  
Old 05-05-04, 05:12 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
try and remove the slash before pics! I mean like this:
PHP Code:

chmod('pics/'.$_FILES['imagefile']['name'], 0644); 

or instead put the realpath..
relative path is better anyway..


btw I don't chmod files after they are uploaded maybe I will add this in the future as a feature which can be enabled and disabled
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 05-05-04 at 05:15 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need thumbnail image show large image separately, on mouseover Cre8tvnrg JavaScript 12 03-05-08 11:01 AM
how to change text dynamically with image? rabbit51 JavaScript 1 02-23-04 08:35 AM
How to change text dynamically with image? rabbit51 Script Requests 0 02-07-04 12:14 PM
mySQL database - Image size reduction within DB trigger_my_passion PHP 0 01-29-04 04:24 PM
Display full size image from thumb without page reloading borgo JavaScript 5 08-12-03 02:30 AM


All times are GMT -5. The time now is 10:30 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.