image resizing.....without adding width property

images php coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 25
Joined: Fri Mar 18, 2011 3:18 am

Wed Apr 13, 2011 1:08 am

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

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

$path = "profile_image/" . $_FILES["file"]["name"];

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;


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


echo $email_error="already exits";
$error = 1;



$result1=mysql_query("UPDATE users
SET full_name='$_POST[fullname]', email='$_POST[email]',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");

$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 my code fileuploading is workng..pls explain resizing of A IMAGE.....

New php-forum User
New php-forum User
Posts: 76
Joined: Sun Nov 28, 2010 6:33 pm

Wed Apr 20, 2011 6:09 pm

Maybe you can use the follow scripts:

Code: Select all

/*  Class Created by Anirban Nath (India) email (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 *
 /*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;}
							                 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 */
							{ 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));


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

User avatar
New php-forum User
New php-forum User
Posts: 14
Joined: Wed Apr 20, 2011 4:53 am
Location: Ahmedabad, Gujarat, India.

Thu Apr 21, 2011 2:43 am

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.

User avatar
New php-forum User
New php-forum User
Posts: 84
Joined: Sat May 21, 2011 10:26 pm

Thu May 26, 2011 5:08 am

Use the dynamic thumbnail creation system. No need to re size the image. It will re size automatically and also will reduce the file size to load quickly.
Find the Dynamic thumbnail creation method below ... g-PHP.html

Hope it will help you
Thank You

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest