<?php
class header
{
    const DIRECTORY = 'web/skin/header/';
    const WIDTH = 1050;
    const HEIGHT = 253;
    
    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.
    
    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.
    
    public static function get_list()
    {
        $list = glob(self::DIRECTORY . '*.jpg');
        
        foreach ($list as &$l)
        {
            $l = pathinfo($l, PATHINFO_FILENAME);
        }
        
        return $list;
    }
    
    public static function save($name, array $file)
    {
        if (!preg_match('~((jpe?g)|(gif)|(png))$~i', $file['name']))
        {
            throw new exception();
        }
        
        $temp = self::DIRECTORY . '__temp_' . pathinfo($file['name'], PATHINFO_BASENAME);
        $target = self::DIRECTORY . $name . '.jpg';
        folder::move($file['tmp_name'], $temp);
        $image = new image($temp);
        $image->resize(self::WIDTH, self::HEIGHT, false);
        $image->save($target, 100);
        folder::mode($target, 0775);
        folder::delete($temp);
    }
    
    public static function delete($name)
    {
        if (is_file($file = self::DIRECTORY . $name . '.jpg'))
        {
            folder::delete($file);
        }
    }
    
    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.
}
