Jump to content

Multiple upload resize image script help?


vivabensmith

Recommended Posts

Hi!

 

Im trying to upload multiple images and resize them, I can get the code to work with one upload but not 2?

 

Your Help would be great

 

Ben

England

 

<?php  

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

{  
     $size = 50; // the thumbnail height  

     $filedir = 'images/'; // the directory for the original image  
     $thumbdir = 'images/'; // the directory for the thumbnail image  
     $prefix = 'small_'; // the prefix to be added to the original name  

     $maxfile = '2000000';  
     $mode = '0666';  
       
     $userfile_name = $_FILES['image1']['name'];  
     $userfile_tmp = $_FILES['image1']['tmp_name'];  
     $userfile_size = $_FILES['image1']['size'];  
     $userfile_type = $_FILES['image1']['type'];  
       
     if (isset($_FILES['image']['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 = $sizes[1]/$sizes[0];  

         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');  
         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); 
          
         } 
     $userfile_name2 = $_FILES['image2']['name'];  
     $userfile_tmp2 = $_FILES['image2']['tmp_name'];  
     $userfile_size2 = $_FILES['image2']['size'];  
     $userfile_type2 = $_FILES['image2']['type'];  
       
     if (isset($_FILES['image2']['name']))  
     {  
         $prod_img2 = $filedir.$userfile_name2;  

         $prod_img_thumb2 = $thumbdir.$prefix.$userfile_name2;  
         move_uploaded_file($userfile_tmp2, $prod_img2);  
         chmod ($prod_img2, octdec($mode));  
           
         $sizes2 = getimagesize($prod_img2);  

         $aspect_ratio2 = $sizes[1]/$sizes[0];  

         if ($sizes[1] <= $size)  
         {  
             $new_width2 = $sizes[0];  
             $new_height2 = $sizes[1];  
         }else{  
             $new_height2 = $size2;  
             $new_width2 = abs($new_height2/$aspect_ratio2);  
         }  

         $destimg2=ImageCreateTrueColor($new_width2,$new_height2) or die('Problem In Creating image');  
         $srcimg2=ImageCreateFromJPEG($prod_img2) or die('Problem In opening Source Image');  
         ImageCopyResized($destimg2,$srcimg2,0,0,0,0,$new_width2,$new_height2,ImageSX($srcimg2),ImageSY($srcimg2)) or die('Problem In resizing');  
         ImageJPEG($destimg2,$prod_img_thumb2,90) or die('Problem In saving');  
         imagedestroy($destimg2);     
          
     }  

echo '  

<img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'"> '; 
  
unlink($prod_img);  





}else{  

echo ' 

     <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">  
     <input type="file" name="image1"><p> 
     <input type="file" name="image2"><p>  
     <input type="Submit" name="Submit" value="Submit">  
     </form>';  
}  

?>

Link to comment
https://forums.phpfreaks.com/topic/61095-multiple-upload-resize-image-script-help/
Share on other sites

No none of the uploads image1 or image2 work?

 

This is the code for one upload hich works fine:

 

<?php 

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

{ 
     $size = 50; // the thumbnail height 

     $filedir = 'images/'; // the directory for the original image 
     $thumbdir = 'images/'; // the directory for the thumbnail image 
     $prefix = 'small_'; // 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']; 
      
     if (isset($_FILES['image']['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 = $sizes[1]/$sizes[0]; 

         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'); 
         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);
         
     } 

echo ' 

<img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'"> '; 
unlink($prod_img); 
}else{ 

echo '

     <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> 
     <input type="file" name="image"><p> 
     <input type="Submit" name="Submit" value="Submit"> 
     </form>'; 
} 

?>

 

I think there must be a problem with the second upload code somewhere??? Thanks for your help, much appreciated!

Ive outputted the errors: can figure out what to do next???

 

Errors with foreach included in code:

Cant figure out what to do next? Thanks for your help!

 

name: carbg.jpg

type: image/jpeg

tmp_name: /private/tmp/phpMnMjir

error: 0

size: 28501

name: header.jpg

type: image/jpeg

tmp_name: /private/tmp/phpAZX49b

error: 0

size: 20876

 

Notice: Undefined variable: sizes in /Users/bensmith/Sites/resizetest/uploadnew2.php on line 69

 

Notice: Undefined variable: sizes in /Users/bensmith/Sites/resizetest/uploadnew2.php on line 69

 

Warning: Division by zero in /Users/bensmith/Sites/resizetest/uploadnew2.php on line 69

 

Notice: Undefined variable: sizes in /Users/bensmith/Sites/resizetest/uploadnew2.php on line 71

 

Notice: Undefined variable: sizes in /Users/bensmith/Sites/resizetest/uploadnew2.php on line 73

 

Notice: Undefined variable: sizes in /Users/bensmith/Sites/resizetest/uploadnew2.php on line 74

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in /Users/bensmith/Sites/resizetest/uploadnew2.php on line 80

 

PHP Code:
<?php 

ini_set('display_errors', 1); 
error_reporting(E_ALL); 

foreach( $_FILES as $file_name => $file_array ) 
   { # begin foreach 

   #foreach file uploaded - do the following... 

   echo "name: ".$file_array['name']."<br />\n"; 
   echo "type: ".$file_array['type']."<br />\n"; 
   echo "tmp_name: ".$file_array['tmp_name']."<br />\n"; 
   echo "error: ".$file_array['error']."<br />\n"; 
   echo "size: ".$file_array['size']."<br />\n"; 



   } # end foreach 
    
if(isset($_POST['Submit'])) 

{ 
     $size = 50; // the thumbnail height 

     $filedir = 'images/'; // the directory for the original image 
     $thumbdir = 'images/'; // the directory for the thumbnail image 
     $prefix = 'small_'; // the prefix to be added to the original name 

     $maxfile = '2000000'; 
     $mode = '0666'; 
       
     $userfile_name = $_FILES['image1']['name']; 
     $userfile_tmp = $_FILES['image1']['tmp_name']; 
     $userfile_size = $_FILES['image1']['size']; 
     $userfile_type = $_FILES['image1']['type']; 
       
     if (isset($_FILES['image']['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 = $sizes[1]/$sizes[0]; 

         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');
         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); 
          
         } 
     $userfile_name2 = $_FILES['image2']['name']; 
     $userfile_tmp2 = $_FILES['image2']['tmp_name']; 
     $userfile_size2 = $_FILES['image2']['size']; 
     $userfile_type2 = $_FILES['image2']['type']; 
       
     if (isset($_FILES['image2']['name'])) 
     { 
         $prod_img2 = $filedir.$userfile_name2; 

         $prod_img_thumb2 = $thumbdir.$prefix.$userfile_name2; 
         move_uploaded_file($userfile_tmp2, $prod_img2); 
         chmod ($prod_img2, octdec($mode)); 
           
         $sizes2 = getimagesize($prod_img2); 

         $aspect_ratio2 = $sizes[1]/$sizes[0]; 

         if ($sizes[1] <= $size) 
         { 
             $new_width2 = $sizes[0]; 
             $new_height2 = $sizes[1]; 
         }else{ 
             $new_height2 = $size2; 
             $new_width2 = abs($new_height2/$aspect_ratio2); 
         } 

         $destimg2=ImageCreateTrueColor($new_width2,$new_height2) or die('Problem In Creating image2');
         $srcimg2=ImageCreateFromJPEG($prod_img2) or die('Problem In opening Source Image');
         ImageCopyResized($destimg2,$srcimg2,0,0,0,0,$new_width2,$new_height2,ImageSX($srcimg2),ImageSY($srcimg2)) or die('Problem In resizing');
         ImageJPEG($destimg2,$prod_img_thumb2,90) or die('Problem In saving'); 
         imagedestroy($destimg2);     
          
     } 

echo ' 

<img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'"> '; 

unlink($prod_img); 





}else{ 

echo ' 

     <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data"> 
     <input type="file" name="image1"><p> 
     <input type="file" name="image2"><p> 
     <input type="Submit" name="Submit" value="Submit"> 
     </form>'; 
} 

?>

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.