Jump to content

NEED HELP ON IMAGE THUMNAIL!!!


lukelee

Recommended Posts

I work on this for 24 hrs already! just could solve it! Here is the code, it resize an image then upload both big image and thumbnail to server. what I want is make it multiple images upload, it could upload 4 images at once, then save the name into database. this should not be so hard, but I just could fix it.

 

<form id="ratioimg" name="ratioimg" enctype="multipart/form-data" method="post" action="ratioimg_upload.php">
  <label>
  <input type="file" name="file" />
  </label>
  <p>
    <label>
    <input type="submit" name="Submit" value="upload" />
    </label>
  </p>
</form>

<?
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)
        {
                //echo $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 "thumbnail successful!";

                        }
                        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 "thumbnail successful!";

                        }

                }
                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 "thumbnail successful!";

                        }
                        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 "thumbnail successful!";

                        }
                }

        }

       
        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);
			echo $new_small;
        }
}

$tempimgname = strtolower($_FILES["file"][name]);

$tempimgname = mb_convert_encoding(   $tempimgname,   "gb2312",   "utf-8");

$tmpfiletype = substr(strrchr($tempimgname,"."),1);

if($tmpfiletype=="jpg" || $tmpfiletype=="gif" || $tmpfiletype=="png")
{
        if(copy($_FILES["file"]["tmp_name"],strtolower("image/".date("Y-m-d-h-m-s",time()).$tempimgname)))
        {
	    $image1 = basename(strtolower("image/".date("Y-m-d-h-m-s",time()).$tempimgname));
		$class = new resizeimage("image/".date("Y-m-d-h-m-s",time()).$tempimgname, 150, 150, 1);

            echo "file".strtolower($_FILES["file"]["name"])."<br>"."size".$_FILES ["file"]["size"]."<br>"."file type is".$_FILES["file"]["type"]."< br>"."success!"."<br>";

		echo $image1;
        }
        else
        {
            echo "failed"."<br>";
        }
    
}
else
{
    echo "only allows jpg,gif,png";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/136845-need-help-on-image-thumnail/
Share on other sites

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.