<?php
function create_thumbnail($p_photo_file, $p_thumb_file, $p_max_size, $p_quality = 75)
{
    $pic = @imagecreatefromjpeg($p_photo_file);

    if ($pic) {	
        $thumb = @imagecreatetruecolor ($p_max_size, $p_max_size) or die ("Can't create Image!");
		$white = imagecolorallocate($thumb, 255, 255, 255);
		imagefill($thumb,0,0,$white);
        $width = imagesx($pic);
        $height = imagesy($pic);
        if ($width < $height) {
				$aspec_ratio = $height / $p_max_size;
				$theight = $p_max_size;
				$twidth = $width / $aspec_ratio;
				imagecopyresized($thumb, $pic, ($p_max_size-$twidth)/2, 0, 0, 0, $twidth, $theight, $width, $height);
                //$twidth = $p_max_size;
                //$theight = $twidth * $height / $width;
                //imagecopyresized($thumb, $pic, 0, 0, 0, ($height/2)-($width/2), $twidth, $theight, $width, $height);								
        } else {
				$aspec_ratio = $width / $p_max_size;
				$twidth = $p_max_size;
				$theight = $height/ $aspec_ratio;
				imagecopyresized($thumb, $pic, 0, ($p_max_size - $theight) / 2, 0, 0, $twidth, $theight, $width, $height);
                //$theight = $p_max_size;
                //$twidth = $theight * $width / $height;
                //imagecopyresized($thumb, $pic, 0, 0, ($width/2)-($height/2), 0, $twidth, $theight, $width, $height);
        }
        ImageJPEG ($thumb, $p_thumb_file, $p_quality);
		return 1;
    }else{
		return 0;
	}
}
?>