Friday, September 12, 2008

Upload a File or image function

## Function _fnUploadFile processing the Uploadedfile StaticPages sub module

function _fnUploadFile($strOldFileName,$strUpFileName,$strFolderName,$strFileTypes="img",$height=0,$width=0,
{


### SOLID FILE UPLOADING CODE ###

if($strFileTypes == "img")
{
$arrAllowImageTypes = array("jpg","jpeg","gif","png");
//$arrAllowImageTypes = array("jpg");
//$strFileTypes = "\"jpg\",\"jpeg\",\"gif\",\"png\"";
}
elseif ($strFileTypes == "doc")
{
$arrAllowImageTypes = array("doc","pdf","rtf","txt","xls","html","html","ppt","csv");
//$strFileTypes = "\"doc\",\"pdf\",\"rtf\",\"txt\"";
}
elseif ($strFileTypes == "video")
{
$arrAllowImageTypes = array("mp4","wmv","mov","mpg","avi","flv");
//$arrAllowImageTypes = array("flv");
}
elseif ($strFileTypes == "podcast")
{
$arrAllowImageTypes = array("mov","mp3","mpe","mp4","mpeg");
}
elseif ($strFileTypes == "csv")
{
$arrAllowImageTypes = array("csv","xls");
}
elseif ($strFileTypes == "resume")
{
$arrAllowImageTypes = array("doc","pdf","rtf","txt");
}


$strImgFileSize = (int)$_FILES[$strOldFileName]["size"];
if($strImgFileSize)
{
if($strFileTypes=="video"&&$strImgFileSize>10485760)
{
$strErrorMessage .= "

Please upload video up to 10 MB.

";
}
elseif($strFileTypes=="img"&&$strImgFileSize>2097152)
{
$strErrorMessage .= "

Please upload image up to 2 MB.

";
}
else
{
$strFileSuccess=1;
$strImgFileName = $_FILES[$strOldFileName]["name"];
$strImgFileError = (int)$_FILES[$strOldFileName]["error"];
if($strImgFileError)
{
$strErrorMessage .= "$strImgFileName Error in uploaded File
"; $strFileSuccess=0;
}
if((int)$strFileSuccess)
{
$strImgFileName = $_FILES[$strOldFileName]["name"];
$strImgFileTmpName = $_FILES[$strOldFileName]["tmp_name"];

#-- get the file extention
$arrImgFileExtention = array_reverse(explode(".",$strImgFileName));
$strImgFileExtention = strtolower($arrImgFileExtention[0]);


#-- create new unique filename to avoide file overwriting situation --#
//$strImgFileNewName = $strUpFileName."_".time().".".$strImgFileExtention;
$strImgFileNewNameTemp = $strUpFileName."_".time();
$strImgFileNewName = $strImgFileNewNameTemp.".".$strImgFileExtention;


# -- check extention in allowed exptentionlist --#
// $arrAllowImageTypes = array($strFileTypes);
// print_r($arrAllowImageTypes);
if(!in_array($strImgFileExtention,$arrAllowImageTypes))
{
$strErrorMessage .= "$strImgFileExtention File Type not allowed for $strImgFileName
"; $strFileSuccess=0;
}
if((int)$strFileSuccess)
{
#-- move file to server --#
if($strFileTypes == "img")
{
$arrHeightWidth = array();
// $arrHeightWidth = clsUtil::fnSetImageHeighWidth($strImgFileTmpName, $height, $width);
if($strSmallImgNewFileName != "")
{
image_resize($strImgFileTmpName,DOCUMENT_ROOT."/usercontent/".$strFolderName."/".$strSmallImgNewFileName,$height,$width);
}
else
{
image_resize($strImgFileTmpName,DOCUMENT_ROOT."/usercontent/".$strFolderName."/".$strImgFileNewName,$height,$width);
}
}
else
{
if(!move_uploaded_file($strImgFileTmpName,DOCUMENT_ROOT."/usercontent/".$strFolderName."/".$strImgFileNewName))
{
$strErrorMessage .= "$strImgFileName File Could not upload
";
}

}
}
}
}
}
else
{
$strErrorMessage .= "

Please check the size.

";
}

if($strErrorMessage == "")
{
return "success=".$strImgFileNewName;
}
else
{
return "error=".$strErrorMessage;
}

}

function image_resize($source, $dest, $targetHeight=75, $targetWidth=100)
{

$image = new Imagick($source);

$intActualheight = $image->getImageHeight(); // GET ACTUAL HEIGHT
$intActualWidth = $image->getImageWidth(); // GET ACTUAL WIDTH

$arrDimentions = fnSetImageHeighWidth($source, $targetHeight, $targetWidth, (int)$intActualheight, (int)$intActualWidth);

// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage($arrDimentions['width'], $arrDimentions['height']);
$image->writeImage($dest);
$image->clear();
$image->destroy();
}

function fnSetImageHeighWidth($strfilename, $intTargetHeight, $intTargetWidth, $intActualHeight=0,$intActualWidth=0)
{
$arrHeightWidth = array();
if($intActualHeight==0 && $intActualWidth==0)
{
list($intWidth, $intHeight) = @getimagesize($strfilename);
}
else
{
$intWidth = $intActualWidth;
$intHeight = $intActualHeight;
}

if($intWidth == 0 || $intHeight == 0)
{
$arrHeightWidth['width'] = $intWidth;
$arrHeightWidth['height'] = $intHeight;
return $arrHeightWidth;
}

if ($intWidth > $intTargetWidth)
{
$intNewWidth = $intTargetWidth;
$intNewHeight = round(($intHeight * $intNewWidth)/$intWidth);
if ($intNewHeight > $intTargetHeight)
{
$intNewHeight = $intTargetHeight;
$intNewWidth = round(($intWidth * $intNewHeight)/$intHeight);
}
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
elseif ($intHeight > $intTargetHeight)
{
$intNewHeight = $intTargetHeight;
$intNewWidth = round(($intWidth * $intNewHeight)/$intHeight);
if ($intNewWidth > $intTargetWidth)
{
$intNewWidth = $intTargetWidth;
$intNewHeight = round(($intHeight * $intNewWidth)/$intWidth);
}
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
elseif ($intWidth == $intHeight && $intWidth > $intTargetWidth)
{
$intNewWidth = $intTargetWidth;
$intNewHeight = round(($intHeight * $intNewWidth)/$intWidth);
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
elseif ($intWidth == $intHeight && $intHeight > $intTargetHeight)
{
$intNewHeight = $intTargetHeight;
$intNewWidth = round(($intWidth * $intNewHeight)/$intHeight);
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
elseif ($intWidth < $intTargetWidth)
{
$intNewWidth = $intTargetWidth;
$intNewHeight = round(($intHeight * $intNewWidth)/$intWidth);
if ($intNewHeight > $intTargetHeight)
{
$intNewHeight = $intTargetHeight;
$intNewWidth = round(($intWidth * $intNewHeight)/$intHeight);
}
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
elseif ($intHeight < $intTargetHeight)
{
$intNewHeight = $intTargetHeight;
$intNewWidth = round(($intWidth * $intNewHeight)/$intHeight);
if ($intNewWidth > $intTargetWidth)
{
$intNewWidth = $intTargetWidth;
$intNewHeight = round(($intHeight * $intNewWidth)/$intWidth);
}
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
elseif ($intWidth == $intHeight && $intWidth < $intTargetWidth)
{
$intNewWidth = $intTargetWidth;
$intNewHeight = round(($intHeight * $intNewWidth)/$intWidth);
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
elseif ($intWidth == $intHeight && $intHeight < $intTargetHeight)
{
$intNewHeight = $intTargetHeight;
$intNewWidth = round(($intWidth * $intNewHeight)/$intHeight);
$arrHeightWidth['width'] = $intNewWidth;
$arrHeightWidth['height'] = $intNewHeight;
}
else
{
$arrHeightWidth['width'] = $intWidth;
$arrHeightWidth['height'] = $intHeight;
}
return $arrHeightWidth;
}

No comments: