<?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, 45, 5, 0, 0, 155, 61, 155, 61);
    
    imagepng($new, 'user_img/'.$username.'.png');
}

function insert_user($id,$name,$screen_name,$location,$description)
{
    $q = "SELECT * FROM users WHERE tw_id = '$id'";
    $q = mysql_query($q);
    if(mysql_num_rows($q) == 0)
    {
        $q = "INSERT INTO users (tw_id, name, screen_name, location, description) 
                VALUES('$id','$name','$screen_name','$location','$description')";
        mysql_query($q);
    }
}

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;
}


?>