Author Topic: add multiple image fileds..  (Read 534 times)

0 Members and 1 Guest are viewing this topic.

Offline techkerTopic starter

  • Devotee
  • Posts: 569
    • View Profile
add multiple image fileds..
« on: March 15, 2010, 04:28:42 PM »
Hey guys i have this image uploader and resize script..it currently only uploads one image..i would like to mod it to upload 3 images..

here is the original code and below it is what i think to change

<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<
html>
<
head>
<
title></title>
<?
php


//load the config file
include("config.php");

//if the for has submittedd
if (isset($_POST['upForm'])){

       
$file_type $_FILES['imgfile']['type'];
       
$file_name $_FILES['imgfile']['name'];
       
$file_size $_FILES['imgfile']['size'];
       
$file_tmp $_FILES['imgfile']['tmp_name'];
	
   
$Box $_POST['Box'];
	
   
$CAR_ID $_GET['ID'];

       
//check if you have selected a file.
       
if(!is_uploaded_file($file_tmp)){
          echo 
"Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit(); 
//exit the script and don't do anything else.
       
}
       
//check file extension
       
$ext strrchr($file_name,'.');
       
$ext strtolower($ext);
       if ((
$extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo 
"Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
          exit();
       }
       
//get the file extension.
       
$getExt explode ('.'$file_name);
       
$file_ext $getExt[count($getExt)-1];

       
//create a random file name
       
$rand_name md5(time());
       
$rand_namerand(0,999999999);
       
//get the new width variable.
       
$ThumbWidth $img_thumb_width;

       
//keep image type
       
if($file_size){
          if(
$file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               
$new_img imagecreatefromjpeg($file_tmp);
           }elseif(
$file_type == "image/x-png" || $file_type == "image/png"){
               
$new_img imagecreatefrompng($file_tmp);
           }elseif(
$file_type == "image/gif"){
               
$new_img imagecreatefromgif($file_tmp);
           }
           
//list width and height and keep height ratio.
           
list($width$height) = getimagesize($file_tmp);
           
$imgratio=$width/$height;
           if (
$imgratio>1){
              
$newwidth $ThumbWidth;
              
$newheight $ThumbWidth/$imgratio;
           }else{
                 
$newheight $ThumbWidth;
                 
$newwidth $ThumbWidth*$imgratio;
           }
           
//function for resize image.
           
if (function_exists(imagecreatetruecolor)){
           
$resized_img imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die(
"Error: Please make sure you have GD library ver 2+");
           }
           
imagecopyresized($resized_img$new_img0000$newwidth$newheight$width$height);
           
//save image
           
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           
ImageDestroy ($resized_img);
           
ImageDestroy ($new_img);
           
//print message
           
echo "<br>Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>";
        }

        
//upload the big image
        
move_uploaded_file ($file_tmp"$path_big/$rand_name.$file_ext");

        echo 
"<br>Image Big: <a href=\"$path_big/$rand_name.$file_ext\">$path_big/$rand_name.$file_ext</a>";

        echo 
"<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

}
///////////////////////////insert part added 
mysql_connect("localhost""techker_techker""techker") or die(mysql_error()) ;
mysql_select_db("techker_iapps") or die(mysql_error()) ;


$DateHeuredate("Y-m-d H:i:s");

$query "INSERT INTO AddOnImages ( CAR_ID, Box , Name, date  )  
VALUES ( NULL,'
$CAR_ID','$Box','$rand_name.$file_ext','$DateHeure')" ;
mysql_query($query) or die('Error, add album failed : ' mysql_error());

//$id= mysql_insert_id();
    
	
	
//echo "<p>This file has the following Database ID: <b>$id</b>";
echo "You'll be redirected to Home Page after (4) Seconds";
          echo 
"<meta http-equiv=Refresh content=4;url=ViewPost.php?ID=>";





//
echo"
</body>
</html>"
;

?>



SO for shure in the form i need to add 2 more image  file fields.called the imagefile2,imagefile3.since the first one is called imagefile.

them im almost shure i need to add this
$file_type3 = $_FILES['imgfile2']['type'];
       $file_name2 = $_FILES['imgfile2']['name'];
       $file_size2 = $_FILES['imgfile2']['size'];
       $file_tmp2 = $_FILES['imgfile2']['tmp_name'];
      
      $file_type 3= $_FILES['imgfile3']['type'];
       $file_name 3= $_FILES['imgfile3']['name'];
       $file_size 3= $_FILES['imgfile3']['size'];
       $file_tmp3 = $_FILES['imgfile3']['tmp_name'];


To get the other images.

Afther:
  $ext = strrchr($file_name,'.');
to
 
$ext = strrchr($file_name,$file_name2,$file_name3,'.');

and
$getExt = explode ('.', $file_name);

to

$getExt = explode ('.', $file_name,$file_name2,$file_name3);

Afther im lost...is this good up to know?
« Last Edit: March 15, 2010, 04:33:54 PM by techker »

Offline techkerTopic starter

  • Devotee
  • Posts: 569
    • View Profile
Re: add multiple image fileds..
« Reply #1 on: March 16, 2010, 06:23:20 AM »
is this hard?or is it my post?lol

Offline karpagam

  • Irregular
  • Posts: 13
    • View Profile
Re: add multiple image fileds..
« Reply #2 on: March 16, 2010, 09:11:22 AM »
Hi...

        See the attached folder.. I think that will be helpful for you........:)


Thanks :D

[attachment deleted by admin]

Offline techkerTopic starter

  • Devotee
  • Posts: 569
    • View Profile
Re: add multiple image fileds..
« Reply #3 on: March 16, 2010, 06:56:09 PM »
nice it uploads but it dows not resize?

Offline karpagam

  • Irregular
  • Posts: 13
    • View Profile
Re: add multiple image fileds..
« Reply #4 on: March 17, 2010, 01:52:05 AM »

Offline techkerTopic starter

  • Devotee
  • Posts: 569
    • View Profile
Re: add multiple image fileds..
« Reply #5 on: March 17, 2010, 06:32:28 AM »
nice thanks!!

its all good but the thumbs dont work?


<?php
function resize($img$thumb_width$newfilename)
{  
$max_width=$thumb_width;    //Check if GD extension is loaded    
if (!extension_loaded('gd') && !extension_loaded('gd2'))     {      
trigger_error("GD is not loaded"E_USER_WARNING);      
return 
false;   
}
//Get Image size info    
list($width_orig$height_orig$image_type) = getimagesize($img);        
switch (
$image_type)   
{        
case 
1:$im imagecreatefromgif($img);break;       
case 
2$im imagecreatefromjpeg($img); break;        
case 
3:$im imagecreatefrompng($img); break;        
default:  
trigger_error('Unsupported filetype!'E_USER_WARNING); break;  
}    

/*** calculate the aspect ratio ***/   
$aspect_ratio = (float) $height_orig $width_orig;   
/*** calculate the thumbnail width based on the height ***/    
$thumb_height round($thumb_width $aspect_ratio);

 while(
$thumb_height>$max_width)   
 {        
 
$thumb_width-=10;       
 
$thumb_height round($thumb_width $aspect_ratio);   
 }       
 
$newImg imagecreatetruecolor($thumb_width$thumb_height);

/* Check if this image is PNG or GIF, then set if Transparent*/      
if(($image_type == 1) OR ($image_type==3))   
{        
imagealphablending($newImgfalse);        
imagesavealpha($newImg,true);        
$transparent imagecolorallocatealpha($newImg255255255127);        
imagefilledrectangle($newImg00$thumb_width$thumb_height$transparent);   
}  
imagecopyresampled($newImg$im0000$thumb_width$thumb_height$width_orig$height_orig);

 
//Generate the file, and rename it to $newfilename    
 
switch ($image_type)     
 {       
 case 
1imagegif($newImg,$newfilename); break;        
 case 
2imagejpeg($newImg,$newfilename);  break;       
 case 
3imagepng($newImg,$newfilename); break;        
 default:  
trigger_error('Failed resize image!'E_USER_WARNING);  break;    
 }    
 return 
$newfilename;
 }

//This stuff is outside of the function. It operates with our images    
if(isset($_POST[submit])){    
$imgNumb=1//This the "pointer" to images     
$DestinationDir="upimg/";  //Place the destination dir here    
$ThumbDir="upimg/thumbs";  //Place the thumb dir here      
do{             
$Unique=microtime(); // We want unique names, right?             
$destination=$DestinationDir.md5($Unique).".jpg";             
$thumb=$ThumbDir.md5($Unique).".jpg";              
$IMG=getimagesize($_FILES["img$imgNumb"][tmp_name]);            
echo 
resize($_FILES["img$imgNumb"][tmp_name], $IMG[0], $destination);          
$imgNumb++;        
}
while(
$_FILES["img$imgNumb"][name]);    }?>






<!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 action="" method="post" enctype="multipart/form-data" >
<input type="file" name="img1"/>
<input type="file" name="img2"/>
<input type="file" name="img3"/>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>