Jump to content

need help on multiple image upload with resize function


lukelee

Recommended Posts

  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?!:

<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>

 

<?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>";
        }
    
}
}


?>

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.