<?php

function resize_image($file, $w, $h) {
    list($width, $height, $src_image_type) = getimagesize($file);
    switch ($src_image_type) 
    {
        case IMAGETYPE_GIF:
            $src = imagecreatefromgif($file);
            break;
        case IMAGETYPE_JPEG:
            $src = imagecreatefromjpeg($file);
            break;
        case IMAGETYPE_PNG:
            $src = imagecreatefrompng($file);
            break; 
    }

    $dst = imagecreatetruecolor($w, $h);
    imagecopyresampled($dst, $src, 0, 0, 0, 0, $w, $h, $width, $height);

    return $dst;
}

function make_img($username)
{
    $top_file = 'user_img/'.$username.'.jpg';
    $bottom_file = 'img/overlay.png';
    
    list($bottom_width, $top_bottom) = getimagesize($bottom_file);    
    
    $top = resize_image($top_file, 200, 200);
    $bottom = imagecreatefrompng($bottom_file);
    
    $new = imagecreatetruecolor(200,200);
    imagecopyresampled($new, $top, 0, 0, 0, 0, 200, 200, 200, 200);

    imagecopyresampled($new, $bottom, 0, 0, 0, 0, 200, 200, 255, 255);
    
    imagepng($new, 'user_img/'.$username.'.png');
}

function insert_user($screen_name)
{
    $q = 'UPDATE users SET tw_username = "' . $screen_name . '" WHERE id = "' . $_SESSION['userId'].'"';
    if(mysql_query($q)){
        return $_SESSION['userId'];
    }

    return mysql_error();
}

function add_to_playlist($id)
{
    $q = "UPDATE users SET status = 1 WHERE tw_id = '$id'";
    mysql_query($q);
}

function get_user_avatars()
{
    $q = "SELECT * FROM users WHERE screen_name != '' AND status = 1 ORDER BY RAND()";
    $res = mysql_query($q);
    $data = "";

        while($row = mysql_fetch_assoc($res))
        {
            $data .= "<a clsss = 'bottom_thumb' target='_blank' href = 'https://twitter.com/".$row['screen_name']."'>
                        <img src = 'http://homepage.rs/pratinemacku/user_img/".$row['screen_name'].".png'>
                     </a>";
                     //$data .= $row;
        }

       return $data;
}


?>