NEED HELP ON IMAGE THUMNAIL!!!

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 IMAGE THUMNAIL!!!

Post by lukelee »

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.

Code: Select all

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

Code: Select all

 
<?
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";
}
 
?>
 
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: NEED HELP ON IMAGE THUMNAIL!!!

Post by jaoudestudios »

hasn't this been posted once already today???
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: NEED HELP ON IMAGE THUMNAIL!!!

Post by pickle »

ALL CAPS DON'T MAKE PEOPLE HELP YOU ANY FASTER.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply