need help on multiple image upload with resize function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

need help on multiple image upload with resize function

Post by lukelee »

My code is working for 1 image resize and then upload to hosting, 1 is thumbnail image, another one is large image, then save the names into database. but now i am trying to change to codes to make it support multiple image resize, upload and save into database.
I have done something in the codes, but has no idea how to continue.
ppl can upload 3 images, each image will be resized, and create a small image, both original image and thumbnail will be saved into upload foleder, thumbnail image file name will be saved into database as imagedata1, and large one will be in imagedata, here is my codes, can anyone help?!:

Code: Select all

 
<tr>
<td>First Image:</td>
<td><input name="ufile[]" type="file" id="ufile[]" size="20" /></td>
</tr>
<tr>
<td>Second Image:</td>
<td><input name="ufile[]" type="file" id="ufile[]" size="20" /></td>
</tr>
<tr>
<td>Third Image:</td>
    <td><input name="ufile[]" type="file" id="ufile[]" size="20" /></td>
</tr>
 

Code: Select all

 
<?php
require_once('db.php'); 
$address = $_POST[house_address];
$description = $_POST[description];
 
$filesize1=$_FILES['ufile']['size'][0];
$filesize2=$_FILES['ufile']['size'][1];
$filesize3=$_FILES['ufile']['size'][2];
 
class resizeimage
{
        var $type;
        var $width;
        var $height;
        var $resize_width;
        var $resize_height;
        var $cut;
        var $srcimg;
        var $dstimg;
        var $im;
 
        function resizeimage($img, $wid, $hei,$c)
        {
 
                $this->srcimg = $img;
                $this->resize_width = $wid;
                $this->resize_height = $hei;
                $this->cut = $c;
 
                $this->type = substr(strrchr($this->srcimg,"."),1);
 
                $this->initi_img();
 
                $this -> dst_img();
 
                $this->width = imagesx($this->im);
                $this->height = imagesy($this->im);
 
                $this->newimg();
                ImageDestroy ($this->im);
        }
        function newimg()
        {
 
 
                $resize_ratio = ($this->resize_width)/($this->resize_height);
            
                $ratio = ($this->width)/($this->height);
                if(($this->cut)=="1")
                
                {
                        if($ratio>=$resize_ratio)
                       
                        {
                                 $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
 
                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
 
                       
                                ImageJpeg ($newimg,$this->dstimg);
                                echo " ";
                                
                        }
                        if($ratio<$resize_ratio)
                    
                        {
                                $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
 
                                ImageJpeg ($newimg,$this->dstimg);
                                echo " ?";
                                
                        }
 
                }
                else
         
                {
                        if($ratio>=$resize_ratio)
                        {
                                $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
 
 
 
                                ImageJpeg ($newimg,$this->dstimg);
                                echo " ";
                                
                        }
                        if($ratio<$resize_ratio)
                        {
                                $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
                                imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
 
 
                                
 
                                ImageJpeg ($newimg,$this->dstimg);
                                echo " ";
                                
                        }
                }
 
                
        }
              
        function initi_img()
        {
                if($this->type=="jpg")
                {
                        $this->im = imagecreatefromjpeg($this->srcimg);
                }
                if($this->type=="gif")
                {
                        $this->im = imagecreatefromgif($this->srcimg);
                }
                if($this->type=="png")
                {
                        $this->im = imagecreatefrompng($this->srcimg);
                }
        }
      
        function dst_img()
        {
                $full_length  = strlen($this->srcimg);
                $type_length  = strlen($this->type);
                $name_length  = $full_length-$type_length;
                $name         = substr($this->srcimg,0,$name_length-1);
                $this->dstimg = $name."_small.".$this->type;
        $new_small = basename($this->dstimg);
 
 
 
        }
}
 
$tempimgname1 = strtolower($_FILES["ufile"][name][0]);
$tempimgname2 = strtolower($_FILES["ufile"][name][1]);
$tempimgname3 = strtolower($_FILES["ufile"][name][2]);
 
$tempimgname1 = mb_convert_encoding(   $tempimgname1,   "gb2312",   "utf-8");
$tempimgname2 = mb_convert_encoding(   $tempimgname2,   "gb2312",   "utf-8");
$tempimgname3 = mb_convert_encoding(   $tempimgname3,   "gb2312",   "utf-8");
 
$tmpfiletype1 = substr(strrchr($tempimgname1,"."),1);
$tmpfiletype2 = substr(strrchr($tempimgname2,"."),1);
$tmpfiletype3 = substr(strrchr($tempimgname3,"."),1);
 
$qu = mysql_query("SELECT address FROM house where thumb='1'");
while($r = mysql_fetch_array($qu)) {
if($r['address']==$address){
echo "ERROR...you already have this address in your database.click <a href='http://www.propertyw1.com.au/admin_add_new.php'>here</a> to go back";
exit;
}
 
else if(($tmpfiletype1 && $tmpfiletype2 =="jpg") && ($tmpfiletype3 !="jpg"))
{
 
        if(copy($_FILES["ufile"]["tmp_name"][0],strtolower("upload/".date("Y-m-d-h-m-s",time()).$tempimgname1)))
        {
                $image1 = basename(strtolower("upload/".date("Y-m-d-h-m-s",time()).$tempimgname1));
            $image2 = basename(strtolower("upload/".date("Y-m-d-h-m-s",time()).$tempimgname2));
            $class = new resizeimage("upload/".date("Y-m-d-h-m-s",time()).$tempimgname1, 150, 150, 1);
            
            $query = mysql_query("insert into house (address,imagedata1,imagedata,description,thumb) values ('$address','$____    ','$image1','$description','1')");
            $query = mysql_query("insert into house (address,imagedata1,imagedata,description,thumb) values ('$address','$____    ','$image1','$description','2')");
            $query = mysql_query("insert into house (address,imagedata1,imagedata,description,thumb) values ('$address','no_img ','no_img','$description','3')");
                    
        }
        else
        {
            echo "Upload failed"."<br>";
        }
    
}
}
 
 
?>
 
 
jon23d
Forum Newbie
Posts: 10
Joined: Wed Jun 07, 2006 3:47 am

Re: need help on multiple image upload with resize function

Post by jon23d »

I have a program that does this.

I give my users the option of doing two things, either uploading a single image or multiple, like this:

Code: Select all

 
  foreach ($images as $image):
     <form action='uploader.php' target='#' method='post' class='uploader'>
         Label: <input type='text' name='label'  /><br />
         Filename: <input type='file' name='filename' /><input type='submit' value='Upload' />
     </form>
  endforeach;
 
So, the user can press upload and have one of the forms submit (I send it to another frame and use javascript to parse the response).

I have another button that the user can press 'upload all'. This button runs a javascript function (it uses prototype, but...)

Code: Select all

function multiUpload() {
  showWaitDialog(true);
  $$('form.uploader').each(function(form){
    form.action = 'uploader.php?type=multi';
    forum.submit();
  });
}
So in uploader.php, I check for $_GET['type'] == 'multi', and I keep track of the number of images processed.
Once all of them have been processed, I send back the response: top.showWaitDialog(false); Responses from individual uploads are queued and show if needed at the end.
Post Reply