<?php
    $title = $l = system :: check($group['name']);
    require (CORE."head.php");
    if (isset($_POST['cfms']))
    {
        if (isset($_FILES['filik_ob']) && isset($_FILES['filik_ob']['tmp_name']) && ((preg_match('#\.jpe?g$#i',$_FILES['filik_ob']['name']) && $imgc=@imagecreatefromjpeg($_FILES['filik_ob']['tmp_name'])) || (preg_match('#\.gif$#i',$_FILES['filik_ob']['name']) && $imgc=@imagecreatefromgif($_FILES['filik_ob']['tmp_name'])) || (preg_match('#\.png$#i',$_FILES['filik_ob']['name']) && $imgc=@imagecreatefrompng($_FILES['filik_ob']['tmp_name']))))
        {
            $key = rand(1000000, 10000000);
            $image = new image;
            $info = getimagesize($_FILES['filik_ob']['tmp_name']); 
            $width = $info[0];
            $height = $info[1]; 
            $timeu = time();
            $keyk = md5("photos_group://".$user['id']."/".$group['id']."/".$timeu."/".$key).".jpg";
            $lu = FILES_PATH.'groups/avatar/';


            unlink($lu.$group['avatar']);
            unlink($lu."150/".$group['avatar']);
            unlink($lu."64/".$group['avatar']);

            // Квадрат.
            $image -> load($_FILES['filik_ob']['tmp_name']);    
            if($width != $height) {
                $image->preview2($width,$height,150);
                $image->crop(array(0,0,150,150));
            } else {
                $image->resize(150,150);
            }
            $image->save($lu.'150/'.$keyk); 

            // x64
            $image -> load($_FILES['filik_ob']['tmp_name']);    
            if($width != $height) {
                $image->preview2($width,$height,64);
                $image->crop(array(0,0,64,64));
            } else {
                $image->resize(64,64);
            }
            $image->save($lu.'64/'.$keyk); 

            // original
            $image->load($_FILES['filik_ob']['tmp_name']);    
            $image->save($lu.$keyk);

            DB :: $pdo -> query("UPDATE `groups` SET `avatar` = ? WHERE `id` = ? LIMIT 1;", [$keyk,$group['id']]);
        	system :: redirect("Аватар успешно изменен.", "/groups/?group=".$group['id']."");
        } else {
            $errMsg = "Отсутствет изображение!";
        }
    }
    echo "<div class=\"app-block\">";
    system :: show((isset($errMsg) ? $errMsg : NULL));
    ?>
    <div class="list list-flex">
        <div class="post-l">
            <?=group :: Avatar($group,0)?>
        </div>
        <div class="post-c">
            <a class="_gray _bold size--big" href="/groups/?group=<?=$group['id']?>">
                <?=group :: getName($group)?>
            </a>
        </div>
    </div>
        <form method="post" class="post-add" data-type="arbour" enctype="multipart/form-data" action="/groups/?group=<?=$group['id']?>&ava=1">
            <label for="myFile"><div class="list _center _gray">
                    Выберите фотографию размером не более 5 Мб и формата JPG/JPEG.
                </div></label>
            <div class="list _bg-blue">
                <input id="myFile" name="filik_ob" accept="image/*,image/png,image/gif,image/jpg" type="file" />
            </div>
            <div class="list">
                <input class="main-btn _dbl _w-100" type="submit" name="cfms" value="Сохранить" />
            </div>
        </form>
    <?
    echo system :: back("/groups/?group=".$group['id']."", "Назад");
    echo "</div>";
    require (CORE."foot.php");
    ?>