Jump to content

can anyone help me to fix my codes


lukelee

Recommended Posts

<?php
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;
	}
	}
}
require_once('db.php');	
$address = $_POST[house_address];
$file1 = strtolower($_FILES["image1"][name]);
$new_name1 = "upload/" . md5(uniqid(rand(), true)) . substr($file1, strrpos($file1, "."));
$filesize1=$_FILES['image1']['size'];

if($filesize1>0){
$temp1 = basename($new_name1);
$class = new resizeimage("upload/".date("Y-m-d-h-m-s",time()).$file1, 260, 260, 1);

$query = mysql_query("insert into house (address,imagedata1,image) values ('$address','$simage1','$temp1)"); 	
echo "We have received your files, you will be redirecting to previous page in 3 seconds";
}
else {
echo "ERROR..... you cant leave the image empty";
}
?>

 

here is my code, i want to save both original pic and resized pic into database and folders, but i just couldnt save the small image. can anyone help me?

Link to comment
https://forums.phpfreaks.com/topic/139677-can-anyone-help-me-to-fix-my-codes/
Share on other sites

forget about the code above, i have made some changes, now I can save both thumbnail and original image into folder, but i can only save the original image name into data base. I think the problem is thumbnail code is within a class, and insert into code is outside of the class, see the red line.

 

 

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

}

?>

 

Well, need some details first. What table is the thumbnail being saved into, what fields do you have in the table?

 

table name is house, there is address,thumb,imagedata1,imagedata, address is the primary key, thumb is just a number, imagedata1 is thumbnail and imagedata is original image

I'm sorry, I really don't follow this. Is it your class, have you written it yourself, or are you using someone elses. It's not a difficult task, but the database isn't making sense with what you're doing.

 

I am making an image upload function, when people submit an image, it will be auto resized, and save both resized image(thumbnail) and original image into folder which is on the server, and save the modified image name into database, which is 'imagedata1' and 'imagedata'.

 

these codes are from someone google, it works fine, except it couldnt save the resized image(thumbnail) into database.

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.