Simple PHP image upload, resize and rename script?

I have a simple script that I found online that uploads and resizes a photo, but as of now it keeps the initial file name.

I just need to modify it so that it renames it to something.jpg each time, and then it will overwrite it the next time around.

Could any of you guys help me out with this?

Thanks!


<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 Image</button>
</form>
<?php
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "images/".$imagename;
              move_uploaded_file($source, $target);

              $imagepath = $imagename;
              $save = "images/" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file

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

              $modwidth = 500;

              $diff = $width / $modwidth;

              $modheight = $height / $diff;
              $tn = imagecreatetruecolor($modwidth, $modheight) ;
              $image = imagecreatefromjpeg($file) ;
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

              imagejpeg($tn, $save, 100) ;
 /*
              $save = "images/sml_" . $imagepath; //This is the new file you saving
              $file = "images/" . $imagepath; //This is the original file

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

              $modwidth = 80;

              $diff = $width / $modwidth;

              $modheight = $height / $diff;
              $tn = imagecreatetruecolor($modwidth, $modheight) ;
              $image = imagecreatefromjpeg($file) ;
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

              imagejpeg($tn, $save, 100) ; */
            echo "Large image: <img src='images/".$imagepath."'><br>";
            //echo "Thumbnail: <img src='images/sml_".$imagepath."'>";

          }
        }
?>

Note: Also, it did create thumbnails originally, but I have commented out that functionality as I do not need thumbnails in this instance.

To save it as something.jpg can’t you just edit this line:

$save = “images/” . $imagepath;

I did that but then it winds up saving two images, one with the name I choose and another that’s the same name as the original file, however the one with the new name is the original size, and the one with the old name gets resized.

I would like it to just save one copy, resized with the name I choose, but I cant get it to work.

I changed it to this when it did as I specified above.


$save = "images/filename.jpg";

When I looked through your code it seems like you are trying to save the original file in the same place as the resized file. This might be what the problem is.

Here is what I would suggest.

Move the uploaded file into a directory called “temp.” After you have finished editing it, you can delete it.

Here is the modified code. Untested.

<?php

    if(isset($_POST['submit'])){
      if (isset ($_FILES['new_image'])){
          $imagename = $_FILES['new_image']['name'];
          $source = $_FILES['new_image']['tmp_name'];
          $target = "temp/".$imagename;

          move_uploaded_file($source, $target);

          $imagepath = $imagename;
          $save = "images/" . $imagepath; //This is the new file you saving

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

          $modwidth = 500;
          $diff = $width / $modwidth;
          $modheight = $height / $diff;

          $tn = imagecreatetruecolor($modwidth, $modheight) ;
          $image = imagecreatefromjpeg($file) ;
          imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

          imagejpeg($tn, $save, 100) ;
          unlink($target); //Delete our uploaded file

        echo "Large image: &lt;img src='images/".$save."'&gt;&lt;br&gt;";
      }
    }

?>

Thanks for posting the code. I will give that a shot and see how it works.

I implemented the above code, created a writable “temp” folder, and tested it out. I get the following result and a broken image displayed.

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in C:\xampp\htdocs\projects\simple_cms\image_upload.php on line 26