<?
include_once 'sys/inc/start.php';
include_once 'sys/inc/compress.php';
include_once 'sys/inc/sess.php';
include_once 'sys/inc/home.php';
include_once 'sys/inc/settings.php';
include_once 'sys/inc/db_connect.php';
include_once 'sys/inc/ipua.php';
include_once 'sys/inc/fnc.php';
include_once 'sys/inc/user.php';

only_reg();
$set['title']='Set Afatar';
include_once 'sys/inc/thead.php';
title();

if (isset($_FILES['file']))
{


if (eregi('\.jpe?g$',$_FILES['file']['name']) && $imgc=@imagecreatefromjpeg($_FILES['file']['tmp_name']))
{
if (imagesx($imgc)>48 || imagesy($imgc)>48)
{
$img_x=imagesx($imgc);
$img_y=imagesy($imgc);
if ($img_x==$img_y)
{
$dstW=48; // &#1096;&#1080;&#1088;&#1080;&#1085;&#1072;
$dstH=48; // &#1074;&#1099;&#1089;&#1086;&#1090;&#1072; 
}
elseif ($img_x>$img_y)
{
$prop=$img_x/$img_y;
$dstW=48;
$dstH=ceil($dstW/$prop);
}
else
{
$prop=$img_y/$img_x;
$dstH=48;
$dstW=ceil($dstH/$prop);
}

$screen=imagecreatetruecolor($dstW, $dstH);
imagecopyresampled($screen, $imgc, 0, 0, 0, 0, $dstW, $dstH, $img_x, $img_y);
imagedestroy($imgc);
@chmod(H."sys/avatar/$user[id].jpg",0777);
@chmod(H."sys/avatar/$user[id].gif",0777);
@chmod(H."sys/avatar/$user[id].png",0777);
@unlink(H."sys/avatar/$user[id].jpg");
@unlink(H."sys/avatar/$user[id].gif");
@unlink(H."sys/avatar/$user[id].png");
imagejpeg($screen,H."sys/avatar/$user[id].jpg",100);
@chmod(H."sys/avatar/$user[id].jpg",0777);
imagedestroy($screen);
}
else
{
copy($_FILES['file']['tmp_name'], H."sys/avatar/$user[id].jpg");
}

msg("&#1040;fatar berhasil di tambah kan");
}
elseif (eregi('\.gif$',$_FILES['file']['name']) && $imgc=@imagecreatefromgif($_FILES['file']['tmp_name']))
{
$screen=gif_resize(fread ( fopen ($_FILES['file']['tmp_name'], "rb" ), filesize ($_FILES['file']['tmp_name']) ),48,48);
@chmod(H."sys/avatar/$user[id].jpg",0777);
@chmod(H."sys/avatar/$user[id].gif",0777);
@chmod(H."sys/avatar/$user[id].png",0777);
@unlink(H."sys/avatar/$user[id].jpg");
@unlink(H."sys/avatar/$user[id].gif");
@unlink(H."sys/avatar/$user[id].png");

file_put_contents(H."sys/avatar/$user[id].gif", $screen);
@chmod(H."sys/avatar/$user[id].gif",0777);

msg("&#1040;fatar berhasil di tambah kan");
}
elseif (eregi('\.png$',$_FILES['file']['name']) && $imgc=@imagecreatefrompng($_FILES['file']['tmp_name']))
{
if (imagesx($imgc)>48 || imagesy($imgc)>48)
{
$img_x=imagesx($imgc);
$img_y=imagesy($imgc);
if ($img_x==$img_y)
{
$dstW=48; // &#1096;&#1080;&#1088;&#1080;&#1085;&#1072;
$dstH=48; // &#1074;&#1099;&#1089;&#1086;&#1090;&#1072; 
}
elseif ($img_x>$img_y)
{
$prop=$img_x/$img_y;
$dstW=48;
$dstH=ceil($dstW/$prop);
}
else
{
$prop=$img_y/$img_x;
$dstH=48;
$dstW=ceil($dstH/$prop);
}

$screen=ImageCreate($dstW, $dstH);
imagecopyresampled($screen, $imgc, 0, 0, 0, 0, $dstW, $dstH, $img_x, $img_y);
imagedestroy($imgc);


@chmod(H."sys/avatar/$user[id].jpg",0777);
@chmod(H."sys/avatar/$user[id].gif",0777);
@chmod(H."sys/avatar/$user[id].png",0777);
@unlink(H."sys/avatar/$user[id].jpg");
@unlink(H."sys/avatar/$user[id].gif");
@unlink(H."sys/avatar/$user[id].png");
imagepng($screen,H."sys/avatar/$user[id].png");
@chmod(H."sys/avatar/$user[id].png",0777);
imagedestroy($screen);
}
else
{

copy($_FILES['file']['tmp_name'], H."sys/avatar/$user[id].png");
}

msg("&#1040;fatar berhasil di tambah kan");
}
else
{
$err='Format file tidak di izinkan';
}
}

err();
aut();


echo "<table class='post'>\n";
echo "   <tr>\n";
echo "  <td class='icon' rowspan='2'>\n";
avatar($user['id']);
echo "  </td>\n";
echo "  <td class='p_t'>\n";
echo " 	Your current avatar\n";
echo "  </td>\n";
echo "   </tr>\n";
echo "   <tr>\n";
echo "  <td class='p_m'>\n";
echo "You can upload a picture formats: GIF, JPG, PNG<br />\n";
echo "Quality Conversions GIF-animation is not guaranteed<br />\n";
echo "  </td>\n";
echo "   </tr>\n";
echo "   <tr>\n";
echo "  <td colspan='2'>\n";
echo "<form method='post' enctype=\"multipart/form-data\" action='?$passgen'>\n";
echo "<input type=\"file\" name=\"file\" accept='image/*,image/gif,image/png,image/jpeg' />\n";
echo "<input value=\"Upload\" type=\"submit\" />\n";
echo "</form>\n";
echo "  </td>\n";
echo "   </tr>\n";
echo "</table>\n";



echo "<div class='foot'>\n";
if(isset($_SESSION['refer']) && $_SESSION['refer']!=NULL && otkuda($_SESSION['refer']))
echo "&laquo;<a href='$_SESSION[refer]'>".otkuda($_SESSION['refer'])."</a><br />\n";
echo "&laquo;<a href='umenu.php'>&#1052;y Menu</a><br />\n";
echo "</div>\n";
include_once 'sys/inc/tfoot.php';
?>