Jump to content

how to save my thumbnail image name into database


lukelee

Recommended Posts

Now I can save both thumbnail and original image into folder, but i can only save the original image name into database. I think the problem is thumbnail code is within a class, and 'insert into' is outside of the class, see the red line. can anyone help me with that?

 

 

<?php

require_once('db.php'); 

$address = $_POST[house_address];

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

            echo "Image has been edited, will go back to previous page in 3 sencond";

 

        }

}

 

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

 

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

 

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

 

if($tmpfiletype=="jpg" || $tmpfiletype=="gif" || $tmpfiletype=="png")

{

        if(copy($_FILES["image1"]["tmp_name"],strtolower("upload/".date("Y-m-d-h-m-s",time()).$tempimgname)))

        {

       

          $image1 = basename(strtolower("upload/".date("Y-m-d-h-m-s",time()).$tempimgname));

        $class = new resizeimage("upload/".date("Y-m-d-h-m-s",time()).$tempimgname, 260, 260, 1);

        $query = mysql_query("insert into house (address,thumb,imagedata1,imagedata) values ('$address','1','$new_small','$image1')");

                     

        }

        else

        {

            echo "Upload failed"."<br>";

        }

 

}

else

{

    echo "image only allows jpg,gif,png";

}

?>

Yes, since the $new_small variable is local inside of the class you can not access it as you are.

 

However; looking at the code the variable "dstimg" is holding the new images name.

 

So update your new_image variable to this:

$query = mysql_query("insert into house (address,thumb,imagedata1,imagedata) values ('$address','1','{$class->dstimg}','$image1')");

 

See if that does the trick.

Yes, since the $new_small variable is local inside of the class you can not access it as you are.

 

However; looking at the code the variable "dstimg" is holding the new images name.

 

So update your new_image variable to this:

$query = mysql_query("insert into house (address,thumb,imagedata1,imagedata) values ('$address','1','{$class->dstimg}','$image1')");

 

See if that does the trick.

 

thanks for help man!

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.