Sunday, June 24, 2012

Resize image and create thumbnail

<?php
function fnCreatThumbnail($strSourcePath,$strFilename,$strDestinationPath,$intGetWidth)
{
    $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}");}
    $width = imagesx( $img );
    $height = imagesy( $img );
   
    $new_width = $intGetWidth;
    $new_height = floor( $height * ( $intGetWidth / $width ) );

    $tmp_img = imagecreatetruecolor($new_width, $new_height);
   
    imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
   
    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($_REQUEST["btnSubmit"])
{
    $type = $_FILES['file']['type'];
    if(($type=="image/gif")||($type=="image/jpeg"))
    {
        move_uploaded_file($_FILES['file']['tmp_name'],'upload/'.$_FILES['file']['name']);
        $origional_image_path = "upload/".$_FILES['file']['name'];
        $strFilename = $_FILES['file']['name'];
        $strDestinationPath = "thumbnail/".$strFilename;
        fnCreatThumbnail($origional_image_path,$strFilename,$strDestinationPath,100);   
    }
    else
    {
        echo "Invalid value";
    }
}

?>
<html>
<body>
<form action="index.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="btnSubmit" value="Submit">
</form>
</body>
</html>

No comments:

Post a Comment

Please mention your comments.......