This class gives crop and resize image with given width and height parameters and also gives uploading facilities. this class uses gd library functions
Please also see attached file above.
<?php
/********
MODIFY ON 6-12-06 By Raj
CLASS FOR IMAGE UPLOAD,IMAGE CROP,IMAGE RESIZE,SIMPLE UPLOAD
//FOR RESIZE AND CROP THERE SHOULD BE FILE EXISTS IN SOMEWHERE THEN WE USE THAT FILE FOR OPERATION
WE USED DIRECTLY getResizeImage AND getCropImage
*********/
Class ImageUpload
{
var $pathFile;
var $originalFileName;
var $SO;
var $sizeMaxFileName = 20;
var $extensionFile;
var $nameFile;
var $CurrentBit=0;
function ClassUpload()
{
}
function setPath($pathFile)
{
$this->pathFile = $pathFile;
}
function setSizeMaxFileName($sizeMaxFileName)
{
$this->sizeMaxFileName = $sizeMaxFileName;
}
function setOriginalFileName($originalFileName)
{
$this->originalFileName = $originalFileName;
}
return $divisionDir;
}
//FILE IS A FILENAME WITHOUT FULLPATH
function isFileExist($file)
{
$x="";
$file = $this->originalFileName;
$openDir = @opendir($this->pathFile);
while (($filesServer=@readdir($openDir))!=false)
{
if (is_file($this->pathFile.$this->getDivisionDIR().$filesServer)):
if ($filesServer==$file)
{
$x = true;
break;
}
else
{
$x = false;
}
else:
$x = false;
endif;
}
@closedir($openDir);
return $x;
}
//FILE IS A FILENAME WITHOUT FULLPATH i.e. $_FILES['name']
function uploadCopyFile($file,$final_name)
{
$x = false;
if($final_name!="")
$path_upload_main = $this->pathFile.$this->getDivisionDIR().$final_name;
else
$path_upload_main = $this->pathFile.$this->getDivisionDIR().$this->originalFileName;
$openDir = @opendir($this->pathFile);
if (@copy($file,$path_upload_main)):
if (@is_uploaded_file($file))
{
$x = true;
}
endif;
@closedir($openDir);
return $x;
}
//FILE IS A FILENAME WITHOUT FULLPATH i.e. $_FILES['tmp_name']
function uploadMoveFile($file,$final_name="")
{
$x = false;
if($final_name!="")
$pathUpload = $this->pathFile.$this->getDivisionDIR().$final_name;
else
$pathUpload = $this->pathFile.$this->getDivisionDIR().$this->originalFileName;
if (@move_uploaded_file($file,$pathUpload)):
if (@is_uploaded_file($file))
{
$x = true;
}
endif;
return $x;
}
/******FOR COYING IMAGE ONE TO OTHER PLACE //RETURN TRUE/FASLE
image_name_field= IS A FILENAME WITHOUT FULLPATH i.e.$_FILES['name']
folder_path =IS A FOLDER PATH WITHOUT FILENAME
final_name= FULLPATH WITH FILENAME IN THAT UPLOADED FILE IS STORED
****/
function upload_copyimage($image_name_field, $folder_path,$final_name="")
{
$this->setSizeMaxFileName(25);
$this->setSO("l"); //FOR WINDOWS USER w ELSE l
$this->setPath($folder_path);
$this->setOriginalFileName($image_name_field);
$this->formatOriginalFileName();
$this->setNameAndExtension();
$nameFileFull = $this->getOriginalFileName();
if (($this->isFileExist($image_name_field))):
$upload_result=false;
else:
if (!($this->uploadCopyFile($image_name_field,$final_name))):
$upload_result=true;
else:
$upload_result=true;
endif;
endif;
return $upload_result;
}
/******FOR MOVING IMAGE ONE TO OTHER PLACE //RETURN TRUE/FASLE
image_name_field= IS A FILENAME WITHOUT FULLPATH i.e.$_FILES['tmp_name']
folder_path =IS A FOLDER PATH WITHOUT FILENAME
final_name= FULLPATH WITH FILENAME IN WHICH UPLOADED FILE IS STORED
****/
function upload_moveimage($image_temp_name, $folder_path,$final_name="")
{
$this->setSizeMaxFileName(25);
$this->setSO("l");//FOR WINDOWS USER w ELSE l
$this->setPath($folder_path);
$this->setOriginalFileName($image_temp_name);
$this->formatOriginalFileName();
$this->setNameAndExtension();
$nameFileFull = $this->getOriginalFileName();
if (($this->isFileExist($image_temp_name))):
$upload_result=false;
else:
if (!($this->uploadMoveFile($image_temp_name,$final_name))):
$upload_result=true;
else:
$upload_result=true;
endif;
endif;
return $upload_result;
}
/****USED BY getCrop
RETURN CROPING IMAGE FOR GIVVEN WIDTH AND HEIGHT
TYPE =IS SIDE OF CROP HEIGHT OR WIDTH
****/
function crop_image($old_image,$new_image,$width,$height,$type="")
{
$dimensions = getimagesize($old_image);
$old_x=$dimensions[0];
$old_y=$dimensions[1];
}
//USED BY getCrop
//RETURN RESIZED IMAGE FOR GIVEN WIDTH AND HEIGHT
function resize_crop($name,$filename,$new_w,$new_h)
{
$system= substr(strrchr($name, "."), 1 );
if (preg_match('/jpg|jpeg/',$system))
{
$src_img=imagecreatefromjpeg($name);
}
else if (preg_match('/png/',$system))
{
$src_img=imagecreatefrompng($name);
}
else if (preg_match('/gif/',$system))
{
$src_img=imagecreatefromgif($name);
}
else $src_img=imagecreatefromjpeg($name);
if ($old_x == $old_y) //both same
{ //lowest value assigned to final
if($new_w>$new_h)
{
$thumb_h=$new_h;
$thumb_w=($new_h*$old_x)/$old_y;
if($thumb_w>$new_w)
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
}
$dst_img= @imagecreatetruecolor($thumb_w,$thumb_h);
if($system=="gif")
{
$transparent = @imagecolorallocate($dst_img, "255", "255", "255");
@imagefill($dst_img, 0, 0, $transparent);
}
@imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
if (preg_match("/png/",$system))
{
imagepng($dst_img,$filename);
}
else if (preg_match('/gif/',$system))
{
imagegif($dst_img,$filename);
}
else
{
imagejpeg($dst_img,$filename);
}
imagedestroy($dst_img);
imagedestroy($src_img);
}
/*****
//FOR RETURN FINAL IMAGE AFTER RESIZE AND CROP
$final_path=FULLPATH WITH FILENAME IN WHICH CROPPED IMAGE WILL STORED
$path= PATH WITHOUT FILENAME IN WHICH $file_name WAS STORED
//SAMPLE=getCrop($ORIGINAL_FOLDER,"2.JPG",PHOTO_CROP_WIDTH,PHOTO_CROP_HEIGHT,$final_path);
*****/
function getCrop($path,$file_name,$width,$height,$final_path)
{
$fold_large=$path."/".$file_name;
@chmod($fold_large,0777);
$size = @getimagesize($fold_large);//300/293
if($size[0]>$width && $size[1]>$height)//both large
{//resize + crop
$new_name=$path."/rajbl_".$file_name;
$this->resize_crop($fold_large,$new_name,$width,$height);
@chmod($new_name,0777);
$this->crop_image($new_name,$final_path,$width,$height);
$final=$final_path;
@unlink($new_name);
}
else if($size[0]>$width && $size[1]<=$height)
{//crop on width
$new_w=$width;
$new_h=$size[1];
$type="w";
$this->crop_image($fold_large,$final_path,$new_w,$new_h,$type);
$final =$final_path;
}
else if($size[0]<=$width && $size[1]>$height)
{//crop on height
$new_w=$size[0];
$new_h=$height;
$type="h";
$this->crop_image($fold_large,$final_path,$new_w,$new_h,$type);
$final =$final_path;
}
else //both small , size[0]<=width && siize[1]<=height
{//no operation//only upload
@copy($fold_large, $final_path);
$final =$final_path;
}
@chmod($final,0777);
return $final;
}
/****
//FOR RETURN FINAL IMAGE AFTER RESIZE
$final_path=FULLPATH WITH FILENAME IN WHICH CROPPED IMAGE WILL STORED
$path= PATH WITHOUT FILENAME IN WHICH $file_name WAS STORED
$file_name=ONLY FILENAME WHICH WAS STORED IN $path
//SAMPLE=getResize($ORIGINAL_FOLDER,"2.JPG",PHOTO_CROP_WIDTH,PHOTO_CROP_HEIGHT,$final_path);
***/
function getResize($path,$file_name,$width,$height,$final_path)
{
$fold_large=$path."/".$file_name;
@chmod($fold_large,0777);
$size = @getimagesize($fold_large);//300/293
if($size[0]<=$width && $size[1]<=$height)
{
//no operation//only upload
@copy($fold_large, $final_path);
$final =$final_path;
}
else
{
$this->resizenew($fold_large,$final_path,$width,$height);
$final=$final_path;
}
@chmod($final,0777);
return $final;
}
/*****
//FOR RETURN FINAL IMAGE AFTER RESIZE AND CROP
$final_path=FULLPATH WITH FILENAME IN WHICH CROPPED IMAGE WILL STORED
$path= PATH WITHOUT FILENAME IN WHICH $file_name WAS STORED
//SAMPLE=getCrop($ORIGINAL_FOLDER,"2.JPG",PHOTO_CROP_WIDTH,PHOTO_CROP_HEIGHT,$final_path);
*****/
function getCropImage($path,$file_name,$width,$height,$final_path)
{
$system= substr(strrchr($file_name, "."), 1 );
if (preg_match('/bmp/',$system))
{
$temp_path1=$path."/".$file_name;
$temp_val="raj.png";
$temp_path2=$path."/".$temp_val;
@chmod($temp_path1,0777);
$ret=$this->imagecreatefrombmp($temp_path1,$temp_path2);
@chmod($temp_path2,0777);
$final_path= substr($final_path, 0, -3)."png";
}
else
{
$temp_val=$file_name;
}
$image_new =$this->getCrop($path,$temp_val,$width,$height,$final_path);
if (preg_match('/bmp/',$system))
{
@unlink($temp_path2);
}
return $image_new;
}
/****
//FOR RETURN FINAL IMAGE AFTER RESIZE
$final_path=FULLPATH WITH FILENAME IN WHICH CROPPED IMAGE WILL STORED
$path= PATH WITHOUT FILENAME IN WHICH $file_name WAS STORED
$file_name=ONLY FILENAME WHICH WAS STORED IN $path
//SAMPLE=getResizeImage($ORIGINAL_FOLDER,"2.JPG",PHOTO_CROP_WIDTH,PHOTO_CROP_HEIGHT,$final_path);
***/
function getResizeImage($path,$file_name,$width,$height,$final_path)
{
$system= substr(strrchr($file_name, "."), 1 );
if (preg_match('/bmp/',$system))
{
$temp_path1=$path."/".$file_name;
$temp_val="raj.png";
$temp_path2=$path."/".$temp_val;
@chmod($temp_path1,0777);
$ret=$this->imagecreatefrombmp($temp_path1,$temp_path2);
@chmod($temp_path2,0777);
$final_path= substr($final_path, 0, -3)."png";
}
else
{
$temp_val=$file_name;
}
$image_new =$this->getResize($path,$temp_val,$width,$height,$final_path);
if (preg_match('/bmp/',$system))
{
@unlink($temp_path2);
}
return $image_new;
}
//FOR DYNAMIC CREATE DIR UPLOAD FILES ON IT OBNE BY ONE//24/8
//RETURN TOTAL FILES IN GIVEN DIR
function total_file($dir)
{
if ($dh = opendir($dir))
{
$i=0;
while (($file = readdir($dh)) !== false)
{
if ($file != "." && $file != "..")
{
if($file!="Thumbs.db")
$i++;
}
}
closedir($dh);
}
return $i;
}
//RETURN TOTAL DIR FOR GIVEN FOLDER/DIR
//FOLD IS EXCEPTIONAL FOLDER IN GIVEN DIR
function total_dir($dir,$fold)
{
if ($dh = opendir($dir))
{
$i=0;
while (($file = readdir($dh)) !== false)
{
if ($file != "." && $file != "..")
{
if($fold!="")
{
if (is_dir($dir."/".$file) && $file!=$fold)
{
$i++;
}
}
else
{
if (is_dir($dir."/".$file))
{
$i++;
}
}
}
}
closedir($dh);
}
return $i;
}
//CREATE FOLDER NAMED DIR
function create_folder($dir)
{
mkdir($dir);
@chmod($dir,0777);
}
/*****
//FOR DYNAMICALLY GENERATE FOLDER AND COPY IMAGE FILE IN IT
$fold_path=FOLDER PATH WITHOUT FILENAME IN WHICH FOLDER STORED
$max_file=HOW MUCH FILE WILL STORED IN THAT FOLDER
$file_name=ONLY FILENAME THAT WILL STORED IN SOME FOLDER(FINAL FILENAME) NEW NAME OF FILE
$temp_name=TEMP NAME OR FILENAME(ORIGINAL FILE)$_FILES['NAME']/$_FILES['TMP_NAME']
OR FULLPATH WITH FILENAME IN WHICH UPLOADED FILE WAS STORED
$fold=EXTRA FOLDER WHICH IS LOCATED IN $fold_path
//SAMPLE=increment_folder("var/html/www/wamba_ccc/picture",MAX_FILE,"1.JPG",temp_name/finalpath,images);
****/
function increment_folder($fold_path,$max_file,$file_name,$temp_name,$fold="")
{
$total_dir = $this->total_dir($fold_path,$fold);
if($total_dir>0)
{
for($i=1;$i<=$total_dir;$i++)
{
$old_dir = $fold_path."/".$i;
$new_dir= $fold_path."/".($i+1);
if($this->total_file($old_dir)<$max_file)
{
@copy($temp_name,$old_dir."/".$file_name);
$ret_val= $old_dir;
}
else
{
if(!is_dir($new_dir))
{
$this->create_folder($new_dir);
$ret_val= $this->increment_folder($fold_path,$max_file,$file_name,$temp_name,$fold);
}
}
}//end for
}//end if
else
{
$new_dir=$fold_path."/"."1";
$this->create_folder($new_dir);
$ret_val= $this->increment_folder($fold_path,$max_file,$file_name,$temp_name,$fold);
}//END ELSE