Location: PHPKode > scripts > Secure Image Uploading and Resizing > class_secure_resize_storing_images.php
<?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;
}

}
?>
Return current item: Secure Image Uploading and Resizing