Saturday, June 23, 2012

PHP Resize image on both side

function fnCreatBigThumbnail($strSourcePath,$strFilename,$strDestinationPath,$intGetWidth,$intGetHeight)
{
$strSourcePath = $strSourcePath.$strFilename;
$info = pathinfo($strSourcePath);
if (strtolower($info['extension']) == 'jpg') {$img = imagecreatefromjpeg( "{$strSourcePath}");}
elseif(strtolower($info['extension']) == 'gif'){$img = imagecreatefromgif( "{$strSourcePath}");}
elseif(strtolower($info['extension']) == 'png'){$img = imagecreatefrompng( "{$strSourcePath}");}
$intNewWidth = $intOrigionalWidth = imagesx( $img );
$intNewHeight = $intOrigionalHeight = imagesy( $img );
if($intOrigionalWidth > $intGetWidth)
{
$intNewWidth = $intGetWidth;
$intNewHeight = (($intOrigionalHeight * $intGetWidth)/$intOrigionalWidth);
}
if($intNewHeight > $intGetHeight)
{
$intNewHeight = $intGetHeight;
$intNewWidth = (($intOrigionalWidth * $intGetHeight)/$intOrigionalHeight);
}
$tmp_img = imagecreatetruecolor($intGetWidth,$intGetHeight);
$strWhiteBackground = imagecolorallocate($tmp_img, 255, 255, 255);
imagefill($tmp_img, 0, 0, $strWhiteBackground);
$floatWhiteWidth = (($intGetWidth - $intNewWidth)/2);
$floatWhiteHeight = (($intGetHeight - $intNewHeight)/2);
imagecopyresized( $tmp_img, $img, $floatWhiteWidth, $floatWhiteHeight, 0, 0, $intNewWidth, $intNewHeight, $intOrigionalWidth, $intOrigionalHeight );
if(strtolower($info['extension']) == 'jpg'){imagejpeg($tmp_img,"{$strDestinationPath}{$strFilename}");}
elseif(strtolower($info['extension']) == 'gif'){imagegif( $tmp_img, "{$strDestinationPath}{$strFilename}");}
elseif(strtolower($info['extension']) == 'png'){imagepng( $tmp_img, "{$strDestinationPath}{$strFilename}");}
}
if(isset($_REQUEST["submit"]))
{
$type = strtolower($_FILES['file']['type']);
if(($type=="image/gif")||($type=="image/jpeg") ||($type=="image/png") ||($type=="image/jpeg"))
{
move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$_FILES['file']['name']);
$strSourcePath = "upload/";
$strFilename = $_FILES['file']['name'];
$strDestinationPath = "resize/";
fnCreatBigThumbnail($strSourcePath,$strFilename,$strDestinationPath,935,440);
}
else
{
echo "Invalid value";
}
}
fnCreatThumbnail(TEMPLATEPATH.'/images/banners/','abc.jpg',TEMPLATEPATH.'/images/banners/large/',300,300);

No comments:

Post a Comment

Please mention your comments.......