image resizing.....without adding width property

A

Anonymous

Guest
hi,all i have a problem...i am uploading an image...it successfully uploading...but my requirement is whenever i want to upload a large image it will resize and then only it has to be upload...my code is...


<?php
ob_start(); session_start();
include ("includes/common.php");
include("includes/authentication.php");
include("includes/dbconfig.php");
include("SimpleImage.php");
$module = "profile"; //IMP don't change
$id=$_SESSION['userid'];
//echo "SELECT * FROM users where id='$id'";
if($_POST[submit] == "save")
{

$fullname=$_POST['fullname'];
$email=$_POST['email'];
$location=$_POST['location'];
$path = "profile_image/" . $_FILES["file"]["name"];


$error=0;
if($_POST['fullname'] == "")
{
$fullname_error="name cannot be left blank";
$error = 1;
}
if($_POST['email'] == "")
{
$email_error="email cannot be left blank";
$error = 1;
}
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";

if (eregi($pattern, $email)==false)
{
$email_error="enter a valid email adress";
$error = 1;
}
if($_POST['location'] == "")
{
$location_error="location cannot be left blank";
$error = 1;
}




if($error==0)
{

$result=mysql_query("SELECT id FROM users WHERE email='$email' AND id!=".$_SESSION['userid']);
$num_rows=mysql_num_rows($result);
if($num_rows > 0)
{
$error = 2;
/* $row = mysql_fetch_row($result);
$id_2=$row['id'];
if($id == $id_2)
{

}

else
{
echo $email_error="already exits";
$error = 1;
}*/

}
else

{

$result1=mysql_query("UPDATE users
SET full_name='$_POST[fullname]', email='$_POST',location='$_POST[location]' WHERE id=$id");


/*echo "<pre>";
print_r($_FILES["file"]["size"]); */


if ( (is_uploaded_file($_FILES['file']['tmp_name'])) &&(($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 20000))
{
if(move_uploaded_file($_FILES["file"]["tmp_name"], "profile_image/" . $_FILES["file"]["name"]))
{
//echo GetImageSize("profile_image/" . $_FILES["file"]["name"]);
/*$tmpimg = tempnam("/tmp", "MKUP");
system("djpeg $imgfile >$path");
system("pnmscale -xy 250 200 $tmpimg | cjpeg -smoo 10 -qual 50 >$path");
unlink($tmpimg);*/

$result1=mysql_query("UPDATE users SET picture='$path' WHERE id=$id");


}


}
else if($_FILES["file"]["tmp_name"]!='')
{
echo $error=3;
}

//echo "saved successfully";//header("location:contactimage1.php");


}
//echo "saved successfully";//header("location:contactimage1.php");


}

}//end of if



$result = mysql_query("SELECT * FROM users where id='$id'");
while($row = mysql_fetch_array($result))
{
$fullname= $row['full_name'];
$email = $row['email'];
$location = $row['location'];
$imgpath = $row['picture'];

}

?>


this is my code the problem is at only uploading image...pls check my code once...and how i will resize my uploaded image and how it will dispaly......in my code fileuploading is workng..pls explain resizing of A IMAGE.....
 
Hi,
Maybe you can use the follow scripts:
Code:
<?PHP 
/*  Class Created by Anirban Nath (India) email :hide@address.com (software programmer , Kaizen tech, Chennai(India) ,expert in ZEND,JOOMLA(CMS+LMS) *
       
	   This class basically has two function ie; (1) checkfile and (2) fileoperation..*
	   while the former is for secure image uploading and the latter for resizing the image and storing it in Database*
	   whether you can use this code, yes obiously ...but you have to retain the copyright code and credit for author!! *
	   last but not least , iwould like to thank all the authors of this sites , for thier valuable works and code...that inspired me *
	   ENJOY FOLKS!!! HOPE YOU LIKE IT... HAPPY CODING */
 /*for DB connection here iam not using any classes to make it simple clean, you can use DB connection*/

class secureimage
{
		var $open ;
		var $content;
		var $im;
		var $width;
		var $height;
		var $instr ;
		var $thumb;
		var $imgw;
		var $imgwh;
		var $xscale;
		var $yscale;
		var $new_width;
		var $new_height;
		var $msg="sorry,  image size exceeds!";
		var $msg2="sorry,  format not allowed!";
         var $redsize;
		 
	function checkfile( $size,$error, $name) {  /* this function is for secure image uploading, ie invalid and malicious files uploading is restricted */
							
								if ( $error>0){
								echo "ERROR:". $error;}
								else{
										
							                 if($size>200005000){ /* max size , you can check this according to your needs */
							                                                      echo $this->msg;
							                                                       return false;}
							
							elseif (exif_imagetype($name) != IMAGETYPE_GIF && exif_imagetype($name) != IMAGETYPE_JPEG && 
							exif_imagetype($name) != IMAGETYPE_PNG )                                                                                                            /* CHECK exif_imagetype() manual in php.net */
							{ echo $this->msg2;
								return false;}
							
							else {return true;} /* returns true , if image is secured ,,,then the execution of function fileoperation() is executed*/       

                                                            }   }



function fileoperation ($tmpname){     /* note this function is execueted if and only function checkfile() return true..this function is for uploading image(reaized) into mysql db.... */


$this->open = fopen($tmpname, 'r+');
$this->content = fread($this->open, filesize($tmpname));


fclose($this->open);

 $this->im = imagecreatefromstring($this->content);
										 
                                     $this->width = imagesx($this->im); /* get the width of the image*/
                                         $this->height = imagesy($this->im);  /*get the height of the image*/          
                                                                 
                                     $this->xscale=$this->width/150;  /* this is the new size 150 px width, you can change this to your own requirements*/
                                      $this->yscale=$this->height/180;        /* this is the new size 180 px height, you can change this to your own requirements*/
									    
										if ($this->yscale>$this->xscale){
        $this->new_width = round($this->width * (1/$this->yscale)); /* new width*/
        $this->new_height = round($this->height * (1/$this->yscale)); /* new height */
    }
    else {
        $this->new_width = round($this->width * (1/$this->xscale));
        $this->new_height = round($this->height * (1/$this->xscale));
    }
									                                
                                         # create new image using thumbnail-size
                                         $this->thumb=imagecreatetruecolor( $this->new_width, $this->new_height);                  
                                         # copy original image to thumbnail
                                        imagecopyresampled($this->thumb,$this->im,0,0,0,0,$this->new_width,$this->new_height, $this->width , $this->height); #makes thumb
                                         
										 imagejpeg($this->thumb, "my.jpg", 85);            
                                         $this->redsize=filesize("my.jpg");   /* getting the reduced size */
										 $this->instr = fopen("my.jpg","r+"); #need to move this to a safe directory
                                     $this->image = addslashes(fread($this->instr,filesize("my.jpg")));
									  
									 return  $this->image;
}

}
?>
Need some free PHP Image handling scripts which includes PHP image resizing and upload image.
 
hello macek

For resize image u must set single parameter to the function is width/height based on that we calculate the actual image size of the given image. and we just re size original image.
 
Back
Top