Jump to content

Uploading image to server and perform actions help


herghost

Recommended Posts

Hi all,

 

I have a form which very nicely creates a folder on myserver named after the userid of the user that is logged in. IE user/4 and has an upload function. The code is:

 

<?php
    session_start();
    include('include/database.php');
    include('include/auth.php');

    $userid = $_SESSION['SESS_USERID'];
    $sql = "SELECT * FROM user WHERE userid = $userid";
    $result = mysql_query($sql) or die(mysql_error());

    if ($result)
    {
        while ($row = mysql_fetch_array($result))
        {
            $userid = $row["userid"];
            if (is_dir('users/'.$userid) == FALSE)
            {
                mkdir('users/'.$userid);
            }
        } 
    }
?><br />
<br />
<br />
Upload a Picture of your band!<br />
<br />

<form enctype="multipart/form-data" action="do/bandpicdo.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">

 

 

Which of course then completes the action of uploading by loading file bandpicdo.php which currently contains:

 

<?php
session_start();
include('../include/auth.php');
include('../include/database.php');



 ?>

 

I am stumped!

 

 

What I want this to do is :

 

a) Upload the image submitted in the form

b) resize the image to 120x120

c) rename the image to 'mainpic.jpg'

4) save to the users directory

 

 

Ehm, Help!

 

 

 

Link to comment
Share on other sites

[code<form action="<?php echo $_server['php-self'];  ?>" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" />
        <button name="submit" type="submit" class="submitButton">Upload/Resize Image</button>
</form>
<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
                                  $file_name = $_FILES['new_image']['name'];
                                $getExt = explode ('.', $file_name);
                $file_ext = $getExt[count($getExt)-1];
                                // Adding extension verification
                                $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|";
if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) 
  die("Extension: $file_ext is not allowed!");             
                                // End extension verification           
                                $imagename = "mainpic.jpg";
                                
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/$id.$file_ext";
                          
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save = "what ever the user path is" . $imagepath; //This is the new file you saving
              $file = "what ever the user path is" . $imagepath; //This is the original file

              list($width, $height) = getimagesize($file) ; 

              // Adding proportionate image resizing, with max values
                          
                          /*
                          $modwidth = 200; 

              $diff = $width / $modwidth;

              $modheight = $height / $diff; 
              */
                          
                          $max_w = 120;
                          $max_h = 120;
                          
                          if($width <= $max_w && $height <= $max_h){ // if it fits
                     $modheight = $height; 
                                 $modwidth = $width;
                          }else{ // Then resize
                                $diff = ($width > $height) ? ($width/$max_w) :  ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800)
                    $modheight = $height / $diff; 
                                $modwidth = $width / $diff;
                          }
                          
                          // End 
                          
                          $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                          imagejpeg($tn, $save, 100) ; 

            
                          $sql = "UPDATE `where ever u stored the pic` SET image = 'mainpic.jpg;
mysql_query($sql) or die(mysql_error());
                          
            echo "<center>
<table width=\"50%\" cellspacing=\"0\" cellpadding=\"0\">
  <tr>
    <td width=\"4%\"><img src='images/thumbs/".$imagepath."'></td>
  </tr>
</table>
</center>"; 
             
          }
        }

Link to comment
Share on other sites

runnerjp,

 

I can not thank you enough, I never expected anyone to be kind enough to write it for me! I will test it soon and come back to you if there is anything I dont understand, and thank you for the comments in the script, I am trying to learn by doing and that probably saved me hours of trying to work out what each peice of script is doing.

 

Again, Many many thanks

Link to comment
Share on other sites

Ok mate,

 

Stumped again:

 

 <?php

$userid = $_SESSION['SESS_USERID'];
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
                                  $file_name = $_FILES['new_image']['name'];
                                $getExt = explode ('.', $file_name);
                $file_ext = $getExt[count($getExt)-1];
                                // Adding extension verification
                                $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|";
if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) 
  die("Extension: $file_ext is not allowed!");             
                                // End extension verification           
                                $imagename = "mainpic.jpg";
                                
              $source = $_FILES['new_image']['tmp_name'];
              $target = "C:/wamp/www/fanjunky/temp/$userid/$imagename";
                          
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save =  "C:/wamp/www/fanjunky/users/$userid/$imagepath"; //This is the new file you saving
              $file =  "C:/wamp/www/fanjunky/temp/$userid/$imagepath"; //This is the original file

              list($width, $height) = getimagesize($file); 

              // Adding proportionate image resizing, with max values
                          
                  //  $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; 
             
                          
                          $max_w = 120;
                          $max_h = 120;
                          
                          if($width <= $max_w && $height <= $max_h){ // if it fits
                     $modheight = $height; 
                                 $modwidth = $width;
                          }else{ // Then resize
                                $diff = ($width > $height) ? ($width/$max_w) :  ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800)
                    $modheight = $height / $diff; 
                                $modwidth = $width / $diff;
                          }
                          
                          // End 
                          
                          $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                          imagejpeg($tn, $save, 100) ; 

            
                         
             
          }
        }
?>

 

This is giving errors:

 

Warning: move_uploaded_file(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 28

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\wamp\tmp\phpF702.tmp' to 'C:/wamp/www/fanjunky/temp/6/mainpic.jpg' in C:\wamp\www\fanjunky\do\bandpicdo.php on line 28

 

Warning: getimagesize(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 34

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\fanjunky\do\bandpicdo.php on line 55

 

Warning: imagecreatefromjpeg(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 56

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 57

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 58

Link to comment
Share on other sites

 <?php
   
   $userid = $_SESSION['SESS_USERID'];
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
                                  $file_name = $_FILES['new_image']['name'];
                                $getExt = explode ('.', $file_name);
                $file_ext = $getExt[count($getExt)-1];
                                // Adding extension verification
                                $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|";
if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) 
  die("Extension: $file_ext is not allowed!");             
                                // End extension verification           
                                $imagename = "mainpic.jpg";
                                
              $source = $_FILES['new_image']['tmp_name'];
              $target = "C:/wamp/www/fanjunky/temp/$userid";
                          
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save =  "C:/wamp/www/fanjunky/users/$userid/$imagepath"; //This is the new file you saving
              $file =  "C:/wamp/www/fanjunky/temp/$userid/$imagepath"; //This is the original file

              list($width, $height) = getimagesize($file); 

              // Adding proportionate image resizing, with max values
                          
                  //  $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; 
             
                          
                          $max_w = 120;
                          $max_h = 120;
                          
                          if($width <= $max_w && $height <= $max_h){ // if it fits
                     $modheight = $height; 
                                 $modwidth = $width;
                          }else{ // Then resize
                                $diff = ($width > $height) ? ($width/$max_w) :  ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800)
                    $modheight = $height / $diff; 
                                $modwidth = $width / $diff;
                          }
                          
                          // End 
                          
                          $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                          imagejpeg($tn, $save, 100) ; 

            
                         
             
          }
        }
?>

 

 

try that

Link to comment
Share on other sites

Warning: getimagesize(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 34

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\wamp\www\fanjunky\do\bandpicdo.php on line 55

 

Warning: imagecreatefromjpeg(C:/wamp/www/fanjunky/temp/6/mainpic.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in C:\wamp\www\fanjunky\do\bandpicdo.php on line 56

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 57

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in C:\wamp\www\fanjunky\do\bandpicdo.php on line 58

Link to comment
Share on other sites

Using this:

 

 <?php
   
   $userid = $_SESSION['SESS_USERID'];
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
                                  $file_name = $_FILES['new_image']['name'];
                                $getExt = explode ('.', $file_name);
                $file_ext = $getExt[count($getExt)-1];
                                // Adding extension verification
                                $allowed_ext = "|jpg|png|gif|jpeg|svg|bmp|";
if (strpos($allowed_ext, "|".strtolower($file_ext)."|")===false) 
  die("Extension: $file_ext is not allowed!");             
                                // End extension verification           
                                $imagename = "mainpic.jpg";
                                
              $source = $_FILES['new_image']['tmp_name'];
              $target = "C:/wamp/www/fanjunky/temp/$userid";
                          
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save =  "C:/wamp/www/fanjunky/users/$userid/$imagepath"; //This is the new file you saving
              $file =  "C:/wamp/www/fanjunky/temp/$userid"; //This is the original file

              list($width, $height) = getimagesize($file); 

              // Adding proportionate image resizing, with max values
                          
                  //  $modwidth = 200; $diff = $width / $modwidth; $modheight = $height / $diff; 
             
                          
                          $max_w = 120;
                          $max_h = 120;
                          
                          if($width <= $max_w && $height <= $max_h){ // if it fits
                     $modheight = $height; 
                                 $modwidth = $width;
                          }else{ // Then resize
                                $diff = ($width > $height) ? ($width/$max_w) :  ($height/$max_h); // Check which is bigger, and fit it to that max value. This will prevent stretching (80x800)
                    $modheight = $height / $diff; 
                                $modwidth = $width / $diff;
                          }
                          
                          // End 
                          
                          $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              $image = imagecreatefromjpeg($file) ; 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                          imagejpeg($tn, $save, 100) ; 

            
                         
             
          }
        }
?>

 

It nows save to the folder without an error :)

 

However it has cropped the width to 120, but cropped the height to 14 pixels?

 

Any thoughts on that?

 

You are, by the way, my hero!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.