Jump to content

Multiple upload and Resize


cgrant

Recommended Posts

I would like some help on my script I have the for my index.php

 

////

 

<html>

 

<head>

<!-- Include the javascript -->

<script src="multifile_compressed.js"></script>

</head>

 

<body>

 

<!-- This is the form -->

<form enctype="multipart/form-data" action="upload.php" method = "post">

<!-- The file element -- NOTE: it has an ID -->

<input id="my_file_element" type="file" name="file_1" >

<input type="submit">

</form>

Files:

<!-- This is where the output will appear -->

<div id="files_list"></div>

<script>

<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->

var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );

<!-- Pass in the file element -->

multi_selector.addElement( document.getElementById( 'my_file_element' ) );

</script>

</body>

</html>

 

//////

 

For my upload.php

 

but this script is for only one image

 

///

 

<?php

if(isset($_POST['submitimg'])){

if (isset ($_FILES['new_image'])){

$imagename = $_FILES['new_image']['name'];

$source = $_FILES['new_image']['tmp_name'];

$target = "../Dphotos/".$imagename;

move_uploaded_file($source, $target);

 

$imagepath = $imagename;

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

$file = "../Dphotos/" . $imagepath; //This is the original file

 

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

 

$modheight = 350;

 

$diff = $height / $modheight;

 

$modwidth = $width / $diff;

$tn = imagecreatetruecolor($modwidth, $modheight) ;

$image = imagecreatefromjpeg($file) ;

imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

 

imagejpeg($tn, $save, 100) ;

 

$save = "../Dphotos/thumbs/" . $imagepath; //This is the new file you saving

$file = "../Dphotos/" . $imagepath; //This is the original file

 

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

 

$modheight = 100;

 

$diff = $height / $modheight;

 

$modwidth = $width / $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='../Dphotos/".$imagepath."'><br>";

echo "Thumbnail: <img src='../Dphotos/thumbs/".$imagepath."'>";

 

}

}

?>

 

 

/////

 

Please help thanks

Link to comment
https://forums.phpfreaks.com/topic/192213-multiple-upload-and-resize/
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td><strong>multiple Files Upload </strong></td>
</tr>
<tr>
<td>Select file
<input name="new_image[]" type="file" id="new_image[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="new_image[]" type="file" id="new_image[]" size="50" /></td>
</tr>
<tr>
<td>Select file
<input name="new_image[]" type="file" id="new_image[]" size="50" /></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>

 

now my php inside upload.php

 

foreach($_FILES['new_image'] as $image) {
$imagename = $_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "photos/".$imagename;
              move_uploaded_file($source, $target);

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

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

              $modheight = 350; 

              $diff = $height / $modheight;

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

              imagejpeg($tn, $save, 100) ; 

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

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

              $modheight = 100; 

              $diff = $height / $modheight;

              $modwidth = $width / $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='photos/".$imagepath."'><br>"; 
            echo "Thumbnail: <img src='photos/thumbs/".$imagepath."'>"; 
}

I am trying to simplify it to work I keep on getting errors and I don't know why?

 

Code is as follows

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form  method="post" action="upload.php" enctype="multipart/form-data">
    <input type="file" name="photos[]"><br />
    <input type="file" name="photos[]"><br />
    <input type="file" name="photos[]"><br />
    <input type="submit" value="Upload" />
</form>
</body>
</html>

 

upload.php

 

<?php

foreach ($_FILES[photos][name] as $key => $value) {
       $uploadfile = $uploaddir . basename($_FILES['photos']['name'][$key]);

// This is the temporary file created by PHP
$uploadedfile = $_FILES['photos']['tmp_name'];

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width,$height)=getimagesize($uploadedfile);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth=600;
$newheight=($height/$width)*600;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "images/". $_FILES['photos']['name'];
imagejpeg($tmp,$filename,100);

// For our purposes, I have resized the image to be
// 150 pixels high, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max height other than
// 150, simply change the $newheight variable
$newheight=150;
$newwidth=($width/$height)*150;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
$filename = "images/thumb_". $_FILES['photos']['name'];
imagejpeg($tmp,$filename,100);

imagedestroy($src);
imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
// has completed.

       //echo $uploadedfile;
       if (move_uploaded_file($_FILES['photos']['tmp_name'][$key], $uploadedfile)) { echo $value . ' uploaded<br>'; }
}

?> 

 

 

Then  there are my errors

 

 

Warning: imagecreatefromjpeg(Array) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /home/personal/public_html/multiple/upload.php on line 10

 

Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in /home/personal/public_html/multiple/upload.php on line 13

 

Warning: Division by zero in /home/personal/public_html/multiple/upload.php on line 21

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/personal/public_html/multiple/upload.php on line 22

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/personal/public_html/multiple/upload.php on line 26

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/personal/public_html/multiple/upload.php on line 31

 

Warning: Division by zero in /home/personal/public_html/multiple/upload.php on line 39

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /home/personal/public_html/multiple/upload.php on line 40

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /home/personal/public_html/multiple/upload.php on line 44

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /home/personal/public_html/multiple/upload.php on line 49

 

Warning: imagedestroy(): supplied argument is not a valid Image resource in /home/personal/public_html/multiple/upload.php on line 51

 

Warning: move_uploaded_file(Array) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/personal/public_html/multiple/upload.php on line 56

 

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpsWvecl' to 'Array' in /home/personal/public_html/multiple/upload.php on line 56

 

----------------------------

 

any help would be appreciated

Current working code

 

<?
foreach($_FILES['new_image'] as $image) {
$imagename = $image['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $target = "photos/".$imagename;
              move_uploaded_file($source, $target);

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

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

              $modheight = 350; 

              $diff = $height / $modheight;

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

              imagejpeg($tn, $save, 100) ; 

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

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

              $modheight = 100; 

              $diff = $height / $modheight;

              $modwidth = $width / $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='photos/".$imagepath."'><br>"; 
            echo "Thumbnail: <img src='photos/thumbs/".$imagepath."'>"; 
}
?>

Try this, fixed an obvious issue, and added some checking to make sure that the getimgsize actually works.

 

foreach($_FILES['new_image'] as $image) {
              $imagename = $image['name'];
              $source = $image['tmp_name'];
              $target = "photos/".$imagename;
              move_uploaded_file($source, $target);

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

              $modheight = 350; 
              $imgsize = getimagesize($file);
              if ($imgsize !== false) { 
                      $width = $imgsize[0];
                      $height = $imgsize[1];
                      $diff = $height / $modheight;
                      $diff = ($diff) > 0 : $diff ? 1;                      
                      $modwidth = $width / $diff; 
                      $tn = imagecreatetruecolor($modwidth, $modheight) ; 
                      $image = imagecreatefromjpeg($file) ; 
                      imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                      imagejpeg($tn, $save, 100) ;  
                      $save = "photos/thumbs/" . $imagepath; //This is the new file you saving
                      $file = "photos/" . $imagepath; //This is the original file
                      list($width, $height) = getimagesize($file) ; 

                      $modheight = 100;  
                      $diff = $height / $modheight;
                      $diff = ($diff) > 0 : $diff ? 1;        
                      $modwidth = $width / $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: 
"; 
                       echo "Thumbnail: ";
              } else {
                 echo "Unable to get image dimensions.";
              }   
}
?>

<form  method="post" action="upload.php" enctype="multipart/form-data">
  <input type="file" name="new_image[]" />
  <br />
    <input type="file" name="new_image[]"><br />
    <input type="file" name="new_image[]"><br />
    <input type="submit" value="Upload" />
</form>

 

<?php
foreach($_FILES['new_image'] as $image) {
              $imagename = $image['name'];
              $source = $image['tmp_name'];
              $target = "photos/".$imagename;
              move_uploaded_file($source, $target);

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

              $modheight = 350; 
              $imgsize = getimagesize($file);
              if ($imgsize !== false) { 
                      $width = $imgsize[0];
                      $height = $imgsize[1];
                      $diff = $height / $modheight;
                      $diff = max($diff, 1);                    
                      $modwidth = $width / $diff; 
                      $tn = imagecreatetruecolor($modwidth, $modheight) ; 
                      $image = imagecreatefromjpeg($file) ; 
                      imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
                      imagejpeg($tn, $save, 100) ;  
                      $save = "photos/thumbs/" . $imagepath; //This is the new file you saving
                      $file = "photos/" . $imagepath; //This is the original file
                      list($width, $height) = getimagesize($file) ; 

                      $modheight = 100;  
                      $diff = $height / $modheight;
                      $diff = max($diff, 1);        
                      $modwidth = $width / $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='photos/".$imagepath."'><br>"; 
                       echo "Thumbnail: <img src='photos/thumbs/".$imagepath."'>";
              } else {
                 echo "Unable to get image dimensions.";
              }   
}
?>

I am trying to make this to a multiple upload for those wondering....

 

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

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

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

              $modheight = 350; 

              $diff = $height / $modheight;

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

              imagejpeg($tn, $save, 100) ; 

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

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

              $modheight = 100; 

              $diff = $height / $modheight;

              $modwidth = $width / $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='photos/".$imagepath."'><br>"; 
            echo "Thumbnail: <img src='photos/thumbs/".$imagepath."'>"; 

          }
        }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.