<?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, 145, 5, 0, 0, 53, 86, 53, 86);
    
    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 get_user_avatars()
{
    $q = "SELECT * FROM users WHERE screen_name != '' 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/MojedojkesuOk/user_img/".$row['screen_name'].".png'>
                     </a>";
                     //$data .= $row;
        }
        return $data;
}


?>