Jump to content

I have a resize/upload image script.Want to modify for multiple upload.


poleposters

Recommended Posts

Hi all,

 

I have this really handy image resize and upload script. It works great,but i'd like to modify it to handle multiple uploads on the one form.

 

Any ideas?

<?php
require_once ('config.inc.php'); 
include ('header.html');
include "mysql_connect.php";
if(!isset($_SESSION['first_name'])){
print "You must be logged in to add a slideshow";}	

else{
if(isset($_POST['Submit'])){



  	     $size = 240; // the thumbnail height
  	     $filedir = 'slide/'; // the directory for the original image
  	     $thumbdir = 'slide/'; // the directory for the thumbnail image
  	     $prefix = 'slide_'; // the prefix to be added to the original name
  	     $maxfile = '2000000';
  	     $mode = '0666';
  	     $userfile_name = $_FILES['image']['name'];
  	     $userfile_tmp = $_FILES['image']['tmp_name'];
  	     $userfile_size = $_FILES['image']['size'];
  	     $userfile_type = $_FILES['image']['type'];
  	     $userfile_error=$_FILES['image']['error'];
  	     $business_id=$_SESSION['business_id'];


  	  	if($userfile_name)
  	  	{
	         $prod_img = $filedir.$userfile_name;
	         $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
         move_uploaded_file($userfile_tmp, $prod_img);
	         chmod ($prod_img, octdec($mode));
  	         $sizes = getimagesize($prod_img);
	         $aspect_ratio = 4/3; 
  	         	if ($sizes[1] <= $size)
	         	{
	             $new_width = $sizes[0];
             $new_height = $sizes[1];
	         	}
	         	else
	         	{
	             $new_height = $size;
	             $new_width = abs($new_height*$aspect_ratio);
	         	}
	         $destimg=ImageCreateTrueColor($new_width,$new_height)
	         or die('Problem In Creating image');
         $srcimg=ImageCreateFromJPEG($prod_img)
	         or die('Problem In opening Source Image');
  	         	if(function_exists('imagecopyresampled'))
  	         	{
  	             imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
  	             or die('Problem In resizing');
  	         	}
  	         	else
  	         	{
  	             				Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
  	             or die('Problem In resizing');
  	         	}
  	         ImageJPEG($destimg,$prod_img_thumb,90)
  	             or die('Problem In saving');
  	         imagedestroy($destimg);
  	         $slide=$prod_img_thumb;
  	       }
  	   else
  	   {
  	   $coupon_image="one.gif";
  	       
  	    }
  	     

       $query="INSERT INTO slideshow(slide_id,business_id,slide) VALUES ('','$business_id','$slide')";
	   $result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());
}

else{
  	     echo '
  	     <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
  	     <table id="quote">


							<td ><input type="file" name="image"></td>

							</tr>

							<tr>
							<td ><input type="Submit" name="Submit" value="Submit"></td>

							</tr>		

							</table>
  	     
  	   
  	     </form>';
  	 }		
}





?>

I've read the manual and searched the web. I'm still completely stumped. I know I need to use a while loop. But I cant figure out where to put it.

 

I've seen some examples on the web, but all the scripts I've seen have some complicated additional features, which means I find the code really confusing. Can anyone help? Even if someone can explain what I need to do in plain english I'm sure I can work out the code from there.

 

Cheers.

 

 

Let's say you want to upload 5 images and resize them. You start by giving all of the input file fields the same name e.g:

<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input type="file" name="image[]" />

 

Then you check if the form has been submitted using the isset function like you're using it now:

<?php 
// Your script logic 
.... 

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

             // These are used for each uploaded image so they only have the be set once
  	     $size = 240; // the thumbnail height
  	     $filedir = 'slide/'; // the directory for the original image
  	     $thumbdir = 'slide/'; // the directory for the thumbnail image
  	     $prefix = 'slide_'; // the prefix to be added to the original name
  	     $maxfile = '2000000';
  	     $mode = '0666';
?> 

 

Then comes the piece of code that handles each to be uploaded image individually:

<?php
...

// See if no errors occurred for each image
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        // Get image information of this particular image
        $userfile_name = $_FILES['image']['name'][$key];
  	$userfile_tmp = $_FILES['image']['tmp_name'][$key];
  	$userfile_size = $_FILES['image']['size'][$key];
  	$userfile_type = $_FILES['image']['type'][$key];
  	$business_id=$_SESSION['business_id'];

       // Your PHP code that handles/process each image individually
       ...... 
    }
}
?>

 

So you get a script that kinda looks like this

<?php
// Your logic
...

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

             // These are used for each uploaded image so they only have the be set once
  	     $size = 240; // the thumbnail height
  	     $filedir = 'slide/'; // the directory for the original image
  	     $thumbdir = 'slide/'; // the directory for the thumbnail image
  	     $prefix = 'slide_'; // the prefix to be added to the original name
  	     $maxfile = '2000000';
  	     $mode = '0666';

             // See if no errors occurred 
            foreach ($_FILES["pictures"]["error"] as $key => $error) {
                if ($error == UPLOAD_ERR_OK) {
                      // Get image information of this particular image
                     $userfile_name = $_FILES['image']['name'][$key];
                     $userfile_tmp = $_FILES['image']['tmp_name'][$key];
                     $userfile_size = $_FILES['image']['size'][$key];
                     $userfile_type = $_FILES['image']['type'][$key];
                     $business_id=$_SESSION['business_id'];

                     // Code that handles/process each image individually
                     ...... 
             }
      }

?>

 

 

Assuming also that you rename each image with a unique name so they won't override each other.

 

Hope that helps you a little bit

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.