<?php 
    function createImage($uploadedfile,$newWidth,$name,$mark)
    {
        // Capture the original size of the uploaded image
        if(!$info=getimagesize($uploadedfile))
          return false;
       
        switch ($info['mime'])
        {
            case 'image/jpeg':
                $src = imagecreatefromjpeg($uploadedfile);
                break;
            case 'image/gif':
                $src = imagecreatefromgif($uploadedfile);
                break;
            case 'image/png':
                $src = imagecreatefrompng($uploadedfile);
				imagealphablending($src, true);				
                break;
            default:
                return false;
        }


    $filename=$name;	
	$size = getimagesize($uploadedfile);
	if($newWidth != "original")
	{
		if($size[0]>$size[1])
		{
			$newHeight=round(($size[1]/$size[0])*$newWidth);
		}
		else
		{	
			$newHeight = $newWidth;
			$newWidth=round(($size[0]/$size[1])*$newHeight);
		}
	}
	else
	{
		$newWidth  = $size[0];
		$newHeight = $size[1];		
	}

        $tmp=imagecreatetruecolor($newWidth,$newHeight);		
		if($info['mime']=='image/png')
		{	
			imagealphablending($tmp, false);
			imagesavealpha($tmp, true);   			
		}   
        imagecopyresampled($tmp,$src,0,0,0,0,$newWidth,$newHeight,$size[0], $size[1]);
  		
		//for watermark
		if($mark)
		{
			switch(exif_imagetype($mark))
			{				
				case '2':
				$wtrsrc = imagecreatefromjpeg($mark);
				break;
				
				case '3':
				$wtrsrc = imagecreatefrompng($mark);				
				break;	
				
				default:
				$wtrsrc = imagecreatefromgif($mark);
				break;															
			}
			
			$woh = imagesy($wtrsrc) ; $wow = imagesx($wtrsrc);	
			imagecopymerge($tmp, $wtrsrc, round(($newWidth-$wow)/2), round(($newHeight-$woh)/2), 0, 0, $wow, $woh, 10);					
		}				
		
        switch ($info['mime'])
        {
            case 'image/jpeg':
                imagejpeg($tmp,$filename,66); //100 is the quality settings, values range from 0-100.
                break;
            case 'image/gif':
                imagegif($tmp,$filename,66); //100 is the quality settings, values range from 0-100.
                break;
            case 'image/png':
                imagepng($tmp,$filename); //100 is the quality settings, values range from 0-100.
                break;
        }
           
        imagedestroy($src);
        imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
        // has completed.
        return $filename;
    }
?>
