|
|
|
|
|
|
| |
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;
}
function getOriginalFileName()
{
return $this->originalFileName;
}
function getNameFile()
{
return $this->nameFile;
}
function RandomName($nameLength) {
$name = "";
for ($index = 1; $index <= $nameLength; $index++)
{
mt_srand((double) microtime() * 1000000);
$randomNumber = mt_rand(1, 62);
if ($randomNumber < 11)
$name .= Chr($randomNumber + 48 - 1); // [ 1,10] => [0,9]
else if ($randomNumber < 37)
$name .= Chr($randomNumber + 65 - 10); // [11,36] => [A,Z]
else
$name .= Chr($randomNumber + 97 - 36); // [37,62] => [a,z]
}
$name = $name."_".time();
$this->formatNameFile();
$this->formatExtensionFile();
$this->originalFileName = $this->nameFile."_".$name.".".$this->extensionFile;
}
function InvalidCaracter($var)
{
$a="ֱבֹיֽם׃ףתַחֳדְאֲגֺך־מװפױץ& -!@#$%¨&*()_+}=}{[]^~?/:;><,'´`\"\\÷";
$b="AaEeIiOoUuCcAaAaAaEeIiOoOoUue___________________________________";
$var = strtr($var,$a,$b);
$var = strtolower($var);
return $var;
}
function formatNameFile()
{
$originalFileName = $this->originalFileName;
$posDivisionNameExtension = strpos($originalFileName,".");
$nameFile = substr($originalFileName,0,$posDivisionNameExtension);
$nameFile = $this->InvalidCaracter($nameFile);
$this->nameFile = $nameFile;
}
function formatExtensionFile()
{
$originalFileName = $this->originalFileName;
$invertFile = strrev($originalFileName);
$posDivisionNameExtension = strpos($invertFile,".");
$extensionFile = substr($invertFile,0,$posDivisionNameExtension);
$extensionFile = strrev($extensionFile);
$extensionFile = $this->InvalidCaracter($extensionFile);
$this->extensionFile = $extensionFile;
}
function getExtensionFile()
{
return $this->extensionFile;
}
function setNameAndExtension()
{
$this->formatNameFile();
$this->formatExtensionFile();
}
function formatOriginalFileName()
{
$this->formatNameFile();
$this->formatExtensionFile();
$sizeFileName = strlen($this->nameFile);
if ($sizeFileName>$this->sizeMaxFileName)
{
$this->nameFile = substr($this->nameFile,0,$this->sizeMaxFileName);
}
$this->originalFileName = $this->nameFile. "." .$this->extensionFile;
}
function setSO($SO)
{
$this->SO = $SO;
}
function getDivisionDIR()
{
$SO = $this->SO;
$SO = strtolower($SO);
switch ($SO)
{
case "w":
$divisionDir = "\\";
break;
case "l":
$divisionDir = "/";
break;
case "u":
$divisionDir = "/";
break;
}
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];
if ($old_x < $width)
{
$width=$old_x;
}
if ($old_y < $height)
{
$height=$old_y;
}
$canvas = imagecreatetruecolor($width,$height);
$system= substr(strrchr($old_image, "."), 1 );
if (preg_match('/png/',$system))
{
$piece = imagecreatefrompng($old_image);
}
else if (preg_match('/gif/',$system))
{
$piece = imagecreatefromgif($old_image);
}
else $piece = imagecreatefromjpeg($old_image);
$newwidth = $dimensions[0] ;/// 2;
$newheight = $dimensions[1];// / 2;
if ($newwidth>$width)
{
$cropLeft = ($newwidth/2) - ($width/2);
$newwidth=$width;
}
else $cropLeft = 0;
if ($newheight > $height)
{
$cropHeight = ($newheight/2) - ($height/2);
$newheight=$height;
}
else $cropHeight = 0;//($newheight);
if($system=="gif")
{
$transparent = imagecolorallocate($canvas, "255", "255", "255");
imagefill($canvas, 0, 0, $transparent);
}
// Generate the cropped image
@imagecopyresized($canvas, $piece, 0,0, $cropLeft, $cropHeight,$newwidth, $newheight, $width, $height);
if (preg_match('/png/',$system))
{
imagepng($canvas,$new_image);
}
else if (preg_match('/gif/',$system))
{
imagegif($canvas,$new_image,90);
}
else imagejpeg($canvas,$new_image,90);
@imagedestroy($canvas);
@imagedestroy($piece);
}
//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);
$old_x=imagesx($src_img);
$old_y=imagesy($src_img);
if ($old_x > $old_y)
{
$thumb_w=$old_x*($new_h/$old_y);
$thumb_h=$new_h;
if($thumb_w<$new_w)
{
$diff=($new_w-$thumb_w);
$thumb_w=$new_w;
$thumb_h=($new_h+$diff);
}
}
if ($old_x < $old_y)
{
$thumb_w=$new_w;
$thumb_h=$old_y*($new_w/$old_x);
if($thumb_h<$new_h)
{
$diff=($new_h-$thumb_h);
$thumb_h=$new_h;
$thumb_w=($new_w+$diff);
}
}
if ($old_x == $old_y)
{
if($new_w>$new_h)
{
$thumb_w=$new_w;
$thumb_h=$new_w;
}
else if($new_w<$new_h)
{
$thumb_w=$new_h;
$thumb_h=$new_h;
}
else
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
}
$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);
}
//USED BY getResize
function resizenew($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);
$old_x=@imagesx($src_img);//200
$old_y=@imagesy($src_img);//200
if ($old_x > $old_y)
{//lowest value assigned to final
if($new_w>$new_h)
{
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
if($thumb_w>$new_w)
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
}
}
else if($new_w<$new_h)
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
if($thumb_h>$new_h)
{
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
}
}
else
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
if($thumb_h>$new_h)
{
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
}
}
}
if ($old_x < $old_y)
{ //lowest value assigned to final
if($new_w>$new_h)
{
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
if($thumb_w>$new_w)
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
}
}
else if($new_w<$new_h)
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
if($thumb_h>$new_h)
{
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
}
}
else
{
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
if($thumb_w>$new_w)
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
}
}
}
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;
}
}
else if($new_w<$new_h)
{
$thumb_w=$new_w;
$thumb_h=($new_w*$old_y)/$old_x;
if($thumb_h>$new_h)
{
$thumb_h=$new_h;
$thumb_w=$old_x*($new_h/$old_y);
}
}
else
{
$thumb_w=$new_w;
$thumb_h=$new_h;
}
}
$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);
|
| |