This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-07-19 12:12:21

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

ITopic: self-updating images and links in posts!

I-Topic: Self Updating Images In Posts And Sigs     

This API allows Scratch users to show images that change according to events and conditions in their forum posts and signatures. This includes showing whether the user is online or not and displaying their newest project(s).
___________________________________________________________________________________
Examples of using the API:
I am currently http://blocks.scratchr.org/API.php?user=sparks&action=onlineStatus&type=square http://blocks.scratchr.org/API.php?user=sparks&action=onlineStatus&type=text and my newest project is called http://www.blocks.scratchr.org/API.php?action=projects&type=newest&return=text&user=sparks&num=1. Click on the thumbnail to view it: http://blocks.scratchr.org/API.php?user=sparks&action=projects&type=newest&return=image&num=1
___________________________________________________________________________________
This API is open source! Look at post #2 for the most recent php code!
___________________________________________________________________________________
Create your custom link/image using the creator linked and documented here by comp500 or make it yourself using the uses list below.

Uses list. Click one to be taken to the documentation page.
Online/offline indicator
     - Online offline circle/square
     - Online offline custom images
     - Online offline custom URLs
Newest project thumbnail(s)/link(s)/info
     - Thumbnail image
     - Link
     - Name
     - Number of love-it's
     - Number of views
     - Number of comments
     - Number of favouriters
     - Number of remixers
     - Number of downloads
newest friend user image(s)
     - Newest friend user icon image(s)
     - Newest friend mystuff page link(s)
Random
     - Random Images
     - Random Links
Customise any text return to match your sig
___________________________________________________________________________________
Thanks to MathWizz for help getting the images to display on the fora!

Last edited by sparks (2012-09-05 14:32:39)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#2 2011-07-19 12:15:32

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

The current php code for this API
V 1.1
~searching the TBG as well as main forum added; not sure if it works.
~up to four users can be checked for "online-ness" at once, if any are online the image returned will be the online one.

1.2
~ Thanks to SeptimusHeap the API can now display your newest friends' Scratch user images and also link to their mystuff page.

1.3
~Newest project action can now report the project name as well as the link and thumbnail!
~ added case-sensitivity to all attributes save for link 1-10, online, offline and string.
~added project love-it, faves, comments, remixes and download return.
~annotated code to make it easier to read and break-down.
1.4
~Added complete image-to-text return control for all current image settings
1.4.1
~Added header type as image for non-link returns.

1.4.2
~Added project views to project info API.

Code:

<?php
#--------------------------------------------------------------------------FUNCTIONS---------------------------------------------------------------------------------
#the function nth_position() helps locate project information.
function nth_position($str, $letter, $n, $offset = 0){
    $str_arr = str_split($str);
    $letter_size = array_count_values(str_split(substr($str, $offset)));
    if( !isset($letter_size[$letter])){
        trigger_error('letter "' . $letter . '" does not exist in ' . $str . ' after ' . $offset . '. position', E_USER_WARNING);
        return false;
    } 
    else if($letter_size[$letter] < $n) {
        trigger_error('letter "' . $letter . '" does not exist ' . $n .' times in ' . $str . ' after ' . $offset . '. position', E_USER_WARNING);
        return false;
    }
    for($i = $offset, $x = 0, $count = (count($str_arr) - $offset); $i < $count, $x != $n; $i++){
        if($str_arr[$i] == $letter){
        $x++;
        }
    }
    return $i - 1;
}
#-------------------------------------------------------------------------------CONVERSION---------------------------------------------------------------------------
#This part converts most of the inputs to the API into lowercase in order to decrease human error
if(isset($_GET['action'])){
    $action = strtolower($_GET['action']);
}
if(isset($_GET['target'])){
    $target = strtolower($_GET['target']);
}
if(isset($_GET['type'])){
    $type = strtolower($_GET['type']);
}
if(isset($_GET['online'])){
    $inOnline = $_GET['online'];
}
if(isset($_GET['offline'])){
    $inOffline = $_GET['offline'];
}
if(isset($_GET['user'])){
    $inUser = $_GET['user'];
}
if(isset($_GET['user2'])){
    $inUser2 = strtolower($_GET['user2']);
}
if(isset($_GET['user3'])){
    $inUser3 = strtolower($_GET['user3']);
}
if(isset($_GET['user4'])){
    $inUser4 = strtolower($_GET['user4']);
}
if(isset($_GET['return'])){
    $inReturn = strtolower($_GET['return']);
}
#---------------------------------------------------------------IMAGE TO TEXT CUSTOM SETTINGS ----------------------------------------------
$extraText = ''; # these are custom commands that can be set to change the appearance of the custom image, such as text size and colour or background colour.
if(isset($_GET['font_size'])){
    $extraText = $extraText . '&font_size=' . $_GET['font_size'];
}
if(isset($_GET['width'])){
    $extraText = $extraText . '&width=' . $_GET['width'];
}
if(isset($_GET['height'])){
    $extraText = $extraText . '&height=' . $_GET['height'];
}
if(isset($_GET['bgr'])){
    $extraText = $extraText . '&bgr=' . $_GET['bgr']; #bgr, bgg, bgb, textr, textg and textb are all RGB values, only set them to numbers from 0 to 255.
}
if(isset($_GET['bgg'])){
    $extraText = $extraText . '&bgg=' . $_GET['bgg'];#If you don't set one of the rgb values it will assume it is 0 though if r, b and g aren't set the default colour for the fora is used.
}
if(isset($_GET['bgb'])){
    $extraText = $extraText . '&bgb=' . $_GET['bgb'];
}
if(isset($_GET['textr'])){
    $extraText = $extraText . '&textr=' . $_GET['textr'];
}
if(isset($_GET['textg'])){
    $extraText = $extraText . '&textg=' . $_GET['textg'];
}
if(isset($_GET['textr'])){
    $extraText = $extraText . '&textb=' . $_GET['textb'];
}
if(isset($_GET['xpos'])){
    $extraText = $extraText . '&xpos=' . $_GET['xpos'];
}
if(isset($_GET['ypos'])){
    $extraText = $extraText . '&ypos=' . $_GET['ypos'];
}
if(isset($_GET['limitstring'])){
    $extraText = $extraText . '&limitstring=' . $_GET['limitstring'];
}
if(isset($_GET['bgimage'])){
    $extraText = $extraText . '&bgimage=' . $_GET['bgimage'];
}
#-------------------------------------------------------------THE API--------------------------------------------------------------------------------------------
if(isset($action)){ #the action attribute tells the API what sort of thing it's going to do, e.g. look at projects or check your online status
    if($action == 'onlinestatus'){
     #target tells this part of the API whether to search in the tbg forum as well as the main one or not. If left blank, only the main fora are searched.
        if($target == 'tbg'){ #search only the text-based-games forum
            $page = ltrim(file_get_contents('http://scratch.mit.edu/tbgforums/'));
            $startpos = strrpos ($page, "Online:&nbsp;</strong></dt>");
            $endpos = strlen(ltrim(file_get_contents('http://scratch.mit.edu/tbgforums/')));
            $refined = substr($page, $startpos, $endpos);
        }
        if($target == 'all'){ #search the main fora as well as the tbg forum
            $page = ltrim(file_get_contents('http://scratch.mit.edu/tbgforums/'));
            $startpos = strrpos ($page, "Online:&nbsp;</strong></dt>");
            $endpos = strlen(ltrim(file_get_contents('http://scratch.mit.edu/tbgforums/')));
            $refined = substr($page, $startpos, $endpos);
            $page = ltrim(file_get_contents('http://scratch.mit.edu/forums/index.php'));
            $startpos = strrpos ($page, "Online:&nbsp;</strong></dt>");
            $endpos = strlen(ltrim(file_get_contents('http://scratch.mit.edu/forums/index.php')));
            $refined = $refined . substr($page, $startpos, $endpos);
        }
        if(!isset($target)){ #search only the main fora.
            $page = ltrim(file_get_contents('http://scratch.mit.edu/forums/index.php'));
            $startpos = strrpos ($page, "Online:&nbsp;</strong></dt>");
            $endpos = strlen(ltrim(file_get_contents('http://scratch.mit.edu/forums/index.php')));
            $refined = substr($page, $startpos, $endpos);
        }
        #set the default online and offline circle images. If no type value is set, they will appear.
        $online = 'http://blocks.scratchr.org/online.gif'; 
        $offline = 'http://blocks.scratchr.org/offline.gif';
        if(isset($type)){
            if($type == 'square'){ #set the images to square indicators
                $online = 'http://blocks.scratchr.org/onlineSquare.gif';
                $offline = 'http://blocks.scratchr.org/offlineSquare.gif';
            }
            if($type == 'text'){ #set the return to the text 'online' or 'offline'
                $online = 'http://blocks.scratchr.org/onlineText.png';
                $offline = 'http://blocks.scratchr.org/offlineText.png';
            }
            if($type == 'customtext'){ #Allow you to choose your own custom online and offline text.
                $online = 'http://www.blocks.scratchr.org/textrender.php?string=' . $inOnline;
                $offline = 'http://www.blocks.scratchr.org/textrender.php?string=' . $inOffline;
            }
        }
        if(!isset($type)){ #if type isn't set then you're either going for the default images or your own custom online/offline images.
            if(isset($inOnline)){ #if the input 'online' is set, display the image linked there when you are online
                $online = $inOnline;
            }
                if(isset($inOffline)){#if the input 'offline' is set, display the image linked there when you are offline
                $offline = $inOffline;
            }
        }
        $isonline = False; #innocent until proven guilty. The user is assumed to be offline until proof of his presence is found.
        if(isset($inUser)){
            $pos = strrpos($refined, $inUser);
            if($pos >= '1'){
                $isonline = True; #the user's name was found so he is online.
            }
        }
        if(isset($inUser2)){
            $pos = strrpos($refined, $inUser2);
            if($pos >= '1'){
                $isonline = True; #users 2 - 4 can also be checked. If they are all offline then isonline will return false, if any one of them are online it will return true.
            }
        }
        if(isset($inUser3)){
            $pos = strrpos($refined, $inUser3);
            if($pos >= '1'){
                $isonline = True;
            }
        }
        if(isset($inUser4)){
            $pos = strrpos($refined, $inUser4);
            if($pos >= '1'){
                $isonline = True;
            }
        }
        
        
        if(!$isonline){
        #the return attribute indicates what sort of thing is being sent back to the requester, e.g. an image or an URL.
            if($inReturn == 'link'){ #the user asked for this link to be given if they are online
                echo "<meta http-equiv='REFRESH' content='0;url=" . $inOnline .  "'>"; #auto-redirect to requested page.
            }
            if($inReturn != 'link'){ #the user didn't want a link, rather an image.
                echo file_get_contents($offline);  #the image they specified above is returned to the requestee.
                header("Content-Type: image/gif");
            }
        }
        if($isonline){
            if($inReturn == 'link'){
                echo "<meta http-equiv='REFRESH' content='0;url=" . $inOffline .  "'>";
            }
            if($inReturn != 'link'){
                echo file_get_contents($online);
                header("Content-Type: image/gif");
            }
        }
    }
    if($action == 'projects'){ #the following API section looks at a user's projects and return information about them.
        if($type == 'newest'){ #looking at the n'th newest project
            $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getprojectsbyusername/' . $inUser)); #this pulls all the project id's belonging to a user into the API.
            $count = '0';
            $placeholder = '0';
            while ($count < $_GET['num'] - '1'){ #this finds the project id for the n'th newest project. n'th being set by the num parameter.
                $placeholder = strpos($page, ':', $placeholder + '1');
                $count ++;
            }
            if($count > 0){
                $placeholder++;
            }
            $endplaceholder = strpos($page, ':', $placeholder + 1);
            if($inReturn == 'image'){ #the user wants a thumbnail of their project returned.
            #echo "<img src = 'http://scratch.mit.edu/static/projects/" . $inUser . "/" . substr($page, $placeholder, $endplaceholder - $placeholder) . "_sm.png' />"; < this echoes the image rather than the file. For glitch testing.
            echo file_get_contents('http://scratch.mit.edu/static/projects/' . $inUser . '/' . substr($page, $placeholder, $endplaceholder - $placeholder) . '_sm.png');
            header("Content-Type: image/gif");
            }
            if($inReturn == 'link'){ # the user wants a link to their n'th project.
                $user = $inUser;
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                echo "<meta http-equiv='REFRESH' content='0;url=http://scratch.mit.edu/projects/" . $user . "/" . $project .  "'>"; #auto-redirect to requested project page.
            }
            if($inReturn == 'text'){ #the user wants the name of their n'th project.
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getprojectinfobyid/' . $project)); #getinfobyprojectid is a page on the Scratch site that lists everything about a chosen project.
                $text = strpos($page, ':', 1);
                $text ++;
                $text = substr($page, $text, strpos($page, ':', $text + 1) - 7); #each piece of information is seperated by a : (colon) so all this is just filtering through the colons until it reaches the right one.
                $text = str_replace("%20","_",$text);
                echo file_get_contents('http://www.blocks.scratchr.org/textrender.php?string=' . $text . $extraText);
                header("Content-Type: image/gif");
            }
            if($inReturn == 'loves'){ # the user wants the number of loves of their n'th project
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getprojectinfobyid/' . $project));
                $startlove = nth_position($page, ':', 6) + 1;
                $page = substr($page, $startlove, strlen($page));
                $startlove = nth_position($page, ':', 1) + 1;
                $page =  substr($page, 0, $startlove - 1);
                echo file_get_contents('http://www.blocks.scratchr.org/textrender.php?string=' . $page . $extraText);
                header("Content-Type: image/gif");
            }
            if($inReturn == 'faves'){ #the user wants the number of favouriters of their n'th project
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getprojectinfobyid/' . $project));
                $startlove = nth_position($page, ':', 7) + 1;
                $page = substr($page, $startlove, strlen($page));
                $startlove = nth_position($page, ':', 1) + 1;
                $page =  substr($page, 0, $startlove - 1);
                echo file_get_contents('http://www.blocks.scratchr.org/textrender.php?string=' . $page . $extraText);
                header("Content-Type: image/gif");
            }
            if($inReturn == 'comments'){ #the user wants the number of comments of their n'th project
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getprojectinfobyid/' . $project));
                $startlove = nth_position($page, ':', 12) + 1;
                $page = substr($page, $startlove, strlen($page));
                $startlove = nth_position($page, ':', 1) + 1;
                $page =  substr($page, 0, $startlove - 1);
                echo file_get_contents('http://www.blocks.scratchr.org/textrender.php?string=' . $page . $extraText);
                header("Content-Type: image/gif");
            }
            if($inReturn == 'remixes'){ #the user wants the number of remixes of their n'th project
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getprojectinfobyid/' . $project));
                $startlove = nth_position($page, ':', 8) + 1;
                $page = substr($page, $startlove, strlen($page));
                $startlove = nth_position($page, ':', 1) + 1;
                $page =  substr($page, 0, $startlove - 1);
                echo file_get_contents('http://www.blocks.scratchr.org/textrender.php?string=' . $page . $extraText);
                header("Content-Type: image/gif");
            }
            if($inReturn == 'downloads'){ # the user wants the number of downloads of their n'th project
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getprojectinfobyid/' . $project));
                $startlove = nth_position($page, ':', 13) + 1;
                $page = substr($page, $startlove, strlen($page));
                echo file_get_contents('http://www.blocks.scratchr.org/textrender.php?string=' . $page . $extraText);
                header("Content-Type: image/gif");
            }
            if($inReturn == 'views'){ # The user wants the number of views for their n'th project
                $project = substr($page, $placeholder, $endplaceholder - $placeholder);
                $page = ltrim(file_get_contents('http://scratch.mit.edu/projects/' . $_GET['user'] . '/' . $project));
                $viewStart = strrpos($page, '<!-- ::: Begin viewers, lovers, taggers and favoriters ::: -->') + 71;
                $viewEnd = strrpos($page, 'views, <a href') - 1;
                $page = substr($page, $viewStart, $viewEnd - $viewStart);
                echo file_get_contents('http://www.blocks.scratchr.org/textrender.php?string=' . $page . $extraText);
                header("Content-Type: image/gif");
            }
        }
        if($type == 'least'){ #this type will return the project witth the least number of.... loves/faves/views etc.
            $page = 'http://scratch.mit.edu/api/getprojectsbyusername/' . $_GET['user'];
            $numberOfProjects = substr_count(file_get_contents($page), ':', 0) + 1;
            $projectcount = 1;
            if($inReturn == 'views'){
                $start = nth_position($page, ':', $count) + 1;
                $end = nth_position($page, ':', $count + 1) + 1;
                
            }
        }
    }
    if($action == 'friends'){ #Thanks to SeptimusHeap for this section
        if($type == 'newest'){
            $page = ltrim(file_get_contents('http://scratch.mit.edu/api/getfriendsbyusername/' . $inUser));
            $count = '0';
            $placeholder = '0';
            while ($count < $_GET['num'] - '1'){
                $placeholder = strpos($page, ':', $placeholder + '1');
                $count ++;
            }
            if($count > 0){
                $placeholder++;
            }
            $endplaceholder = strpos($page, ':', $placeholder + 1);
            if($inReturn == 'image'){
            #echo "<img src = 'http://scratch.mit.edu/static/icons/buddy/" . substr($page, $placeholder, $endplaceholder - $placeholder) . "_sm.png' />"; < this echoes the image rather than the file. For glitch testing.
            echo file_get_contents('http://scratch.mit.edu/static/icons/buddy/' . substr($page, $placeholder, $endplaceholder - $placeholder) . '_sm.png');
            header("Content-Type: image/gif");
        }
            if($inReturn == 'link'){
                $user = $inUser;
                $userguy = substr($page, $placeholder, $endplaceholder - $placeholder);
                echo "<meta http-equiv='REFRESH' content='0;url=http://scratch.mit.edu/api/getuser/" . $userguy .  "'>";
            }
        }
    }
    if($action == 'text'){
        echo file_get_contents('http://blocks.scratchr.org/textrender.php?string=' . $_GET['string'] . $extraText);
        header("Content-Type: image/gif");
    }
    if($action == 'random'){
        $numberOfLinks = 0;
        if(isset($_GET['link1'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link2'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link3'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link4'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link5'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link6'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link7'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link8'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link9'])){
            $numberOfLinks ++;
        }
        if(isset($_GET['link10'])){
            $numberOfLinks ++;
        }
        if($numberOfLinks > 0){
            $url = rand(1, $numberOfLinks);
            if($inReturn == 'image'){
                echo file_get_contents($_GET['link' . $url]);
                header("Content-Type: image/gif");
            }
            if($inReturn == 'link'){
                echo "<meta http-equiv='REFRESH' content='0;url=" . $_GET['link' . $url] .  "'>";
            }
        }
    }
}
if(!isset($action)){
echo "<p>Sorry, you've not set the action parameter, I don't know what to do! For documentation on this API look <a href = 'http://scratch.mit.edu/forums/viewtopic.php?id=69113&p=1'>here</a>";
}
?>

___________________________________________________________________________________
Generator
The current source code for comp500's generator can be downloaded here.
___________________________________________________________________________________
Text renderer
V 1.1
~Added "_" to " "(space) converter.
V 1.2
~ Added complete text image control for current settings.

V2.0
~ Added background-image options (thanks to LS97 for the help there)

Code:

<?PHP
//parser in spaces
$count = 1;
$string = str_replace("_"," ",$_GET['string']);
if(isset($_GET['bgimage']) && strstr($_GET['bgimage'], ".png")){ #thanks to LS97 for making most of the bgimage attribute!
    // create the background
    $img = imagecreatefrompng($_GET['bgimage']);
    
    //chooses the text color
        if(isset($_GET['textr']) | isset($_GET['textg']) | isset($_GET['textb'])){
        $red = (int)$_GET['textr'];
        $green = (int)$_GET['textg'];
        $blue = (int)$_GET['textb'];
        $text_colour = imagecolorallocate($img, $red, $blue, $green);
    }
    else{
        $text_colour = imagecolorallocate($img, 0, 0, 0);
    }
    
    // get the font
    $font = realpath('Trebuchet_MS.ttf');
    
    // print the string
    $xpos = 5;
    if(isset($_GET['xpos'])){
        $xpos = $_GET['xpos'];
    }
    $ypos = 20;
    if(isset($_GET['ypos'])){
        $ypos = $_GET['ypos'];
    }
    imagettftext($img, 13, 0, $xpos, $ypos, $text_colour, $font, $string);
    
    // tell browser about content type
    header( 'Content-type: image/png' );
    // create image
    imagepng( $img );
}
if(!isset($_GET['bgimage'])){
    // Set font size
    $font_size = 2;
    if(isset($_GET['font_size'])){
        $font_size = $_GET['font_size'];
    }
    $font = '/fonts/Trebuchet_MS.ttf';
    if(isset($_GET['font'])){
        $font = $_GET['font'];
    }
    // Create image width dependant on width of the string
    $width  = imagefontwidth($font_size)*strlen($string);
    if(isset($_GET['width'])){
        $width = $_GET['width'];
    }
    // Set height to that of the font
    $height = imagefontheight($font_size);
    if(isset($_GET['height'])){
        $height = $_GET['height'];
    }
    // Create the image pallette
        $img = imagecreate($width,$height);
        if(isset($_GET['bgr']) | isset($_GET['bgg']) | isset($_GET['bgb'])){
            $red = (int)$_GET['bgr'];
            $blue = (int)$_GET['bgg'];
            $green = (int)$_GET['bgb'];
            $bg    = imagecolorallocate($img, $red, $blue, $green);
        }
        else{
            $bg    = imagecolorallocate($img, 222, 223, 223);
        }
    // Font color
    if(isset($_GET['textr']) | isset($_GET['textg']) | isset($_GET['textb'])){
        $red = (int)$_GET['textr'];
        $green = (int)$_GET['textg'];
        $blue = (int)$_GET['textb'];
        $color = imagecolorallocate($img, $red, $blue, $green);
    }
    else{
        $color = imagecolorallocate($img, 0, 0, 0);
    }

    // Length of the string
    if(isset($_GET['limitstring'])){
        if(strlen($string) > $_GET['limitstring']){
            $string= substr($string, 0, $_GET['limitstring']);
        }
    }
    $len = strlen($string);
    // Y-coordinate of character, X changes, Y is static
    $ypos = 0;
    if(isset($_GET['ypos'])){
        $ypos = $_GET['ypos'];
    }
    // Loop through the string
    for($i=0;$i<$len;$i++){
        // Position of the character horizontally
        if(isset($_GET['xpos'])){
            $xpos = $i * imagefontwidth($font_size) + $_GET['xpos'];
        }
        else{
            $xpos = $i * imagefontwidth($font_size);
        }
        // Draw character
        imagechar($img, $font_size, $xpos, $ypos, $string, $color);
        // Remove character from string
        $string = substr($string, 1);   
    
    }
    // Return the image
    header("Content-Type: image/gif");
    imagegif($img);
}
// Remove image
imagedestroy($img);

?>

Last edited by sparks (2011-11-24 06:53:43)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#3 2011-07-19 12:16:40

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

^ home
Online/offline indicator

action value:

onlineStatus

parameters:

user, [type, online, offline]

___________________________________________________________________________________
Standard image
For this type you need only give the Scratcher name. This will either show a http://blocks.scratchr.org/online.gif or a http://blocks.scratchr.org/offline.gif. For example: I am currently http://blocks.scratchr.org/API.php?user=sparks&amp;action=onlineStatus.
type value:

none

Code:

[img]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=onlineStatus[/img]

___________________________________________________________________________________
Square image
This requires the Scratcher name and the type value "square". This will either show a http://blocks.scratchr.org/onlineSquare.gif or a http://blocks.scratchr.org/offlineSquare.gif. For example: I am currently http://blocks.scratchr.org/libstatus.php?user=sparks&amp;type=square.

type value:

square

Code:

[img]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=onlineStatus&type=square[/img]

___________________________________________________________________________________
text return
This requires the Scratcher name and the type value "text". It returns an image of the words "online" or "offline". For example, I am currently http://blocks.scratchr.org/API.php?action=onlineStatus&amp;user=sparks&amp;type=text.

type value:

text

Code:

[img]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=onlineStatus&type=text[/img]

___________________________________________________________________________________
Any image return
This API attribute is excellent for changing signatures! It requires the Scratcher name and the parameters online and offline. So any two images can be grabbed from anywhere online such as http://www.burtonmutualangling.com/images/online.png and http://www.glam-ou-rama.co.uk/forum/images9/offline.png. In application it shows that I am currently http://blocks.scratchr.org/API.php?user=sparks&amp;action=onlineStatus&amp;online=http://www.burtonmutualangling.com/images/online.png&amp;offline=http://www.glam-ou-rama.co.uk/forum/images9/offline.png.

type value:

none

other parameters:

online, offline

Code:

[img]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=onlineStatus&online=URLTOIMAGEBEINGSHOWNFORONLINE&offline=URLTOIMAGEBEINGSHOWNFOROFFLINE[/img]

Note~ if you leave either online or offline out of the URL the normal image will appear in its place.

Any link return

if you want to link to one URL when you are online and another when you are offline you simply have to add "return=link" to the above api and place them within a link tag.

Code:

[url]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=onlineStatus&return=link&online=ONLINELINKURL&offline=OFFLINELINKURL[/url]

___________________________________________________________________________________
Show if one of up to four users are online
To do this, simply add one parameter per scratch username. if any of them are online, the image returned will be the online image. If you want to search for, for example, only two users, simply don't add user3 and user4 to the URL.
parameters:

user, [user2, user3, user4]

Code:

[img]http://blocks.scratchr.org/API.php?action=onlineStatus&user=USERNAME1HERE&user2=USERNAME2HERE&user3=USERNAME3HERE&user4=USERNAME4HERE[/img]

___________________________________________________________________________________
Searching the text-based games forum
This uses the parameter "target". It can be added to any online/offline search you are using. If you don't set "target" and leave it out, the API will search the Scratch forums only. If you set it to "tbg" it will search for online users on the text-based games forum only. If you set it to "all" it will search through both the main fora and the text-based games forum.

parameters:

target

example:

Code:

[img]http://www.blocks.scratchr.org/API.php?action=onlineStatus&user=USERNAMEHERE&target=all[/img]

Last edited by sparks (2011-07-28 05:11:41)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#4 2011-07-19 12:17:43

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

^ home
Project links, thumbs and info

Project thumnails

This API lets you show your newest projects in your sig! When you add a new project it will appear as num=1 automatically and all the other project images will slide back! For example, my newest project has this thumbnail:http://blocks.scratchr.org/API.php?user=sparks&amp;action=projects&amp;type=newest&amp;return=image&amp;num=1

action value:

projects

type value:

newest

return value:

image

parameters:

user, num

Code:

[img]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=projects&type=newest&return=image&num=NUMBER[/img]

setting num to 1 will return your newest project, setting it to 2 will return your second newest and so on.
___________________________________________________________________________________
Project links

This API lets you link to your n'th newest projects in your signature! For example, my newest project has this link: My newest project

action value:

projects

type value:

newest

return value:

link

parameters:

user, num

Code:

[url]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=projects&type=newest&return=link&num=NUMBER[/url]

setting num to 1 will return your newest project, setting it to 2 will return your second newest and so on.
________________________________________________________________________________
if you want to create a thumbnail to go with the link to your newest project, the parameters and method are exactly the same for the link version but you put it into [ url] tags and return=link is replaced with return=image.

For example:
http://blocks.scratchr.org/API.php?user=sparks&amp;action=projects&amp;type=newest&amp;return=image&amp;num=1

Code:

[url=http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=projects&type=newest&return=link&num=1][img]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=projects&type=newest&return=image&num=1[/img][/url]

___________________________________________________________________________________
Project info
This API returns info about your num'th project as an image of the name. For example, my newest project, http://www.blocks.scratchr.org/API.php?action=projects&amp;type=newest&amp;return=text&amp;user=sparks&amp;num=1 has http://www.blocks.scratchr.org/API.php?action=projects&amp;type=newest&amp;return=loves&amp;user=sparks&amp;num=1 love-it's and  http://www.blocks.scratchr.org/API.php?action=projects&amp;type=newest&amp;return=faves&amp;user=sparks&amp;num=1 favouriters, http://www.blocks.scratchr.org/API.php?action=projects&amp;type=newest&amp;return=comments&amp;user=sparks&amp;num=1 comments, http://www.blocks.scratchr.org/API.php?action=projects&amp;type=newest&amp;return=downloads&amp;user=sparks&amp;num=1 downloads, http://www.blocks.scratchr.org/API.php?action=projects&amp;type=newest&amp;return=views&amp;user=sparks&amp;num=1 views and http://www.blocks.scratchr.org/API.php?action=projects&amp;type=newest&amp;return=remixes&amp;user=sparks&amp;num=1 remixes!

action value:

projects

type value:

newest

return value:

text, loves, faves, comments, remixes, downloads, views

parameters:

user, num, return

Code:

[img]http://blocks.scratchr.org/API.php?user=USERNAMEHERE&action=projects&type=newest&return=RETURNVALUE&num=NUMBER[/img]

There are several return values for this action:
text -return the project name
loves - return the number of love-it's
faves - return the number of favouriters
comments - return the number of comments
remixes - return the number of remixes
downloads - return the number of downloads
views - return the number of views

Last edited by sparks (2011-11-24 06:51:54)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#5 2011-07-19 12:18:45

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

^ home

Random

Random images
This API randomly selects one of up to ten images to display with every page refresh. This works very well for sigs where a user wants a random message or picture shown. For example, this image will randomly change whenever you refresh the page: http://www.blocks.scratchr.org/API.php?action=random&amp;return=image&amp;link1=http://img.tutorialking.eu/icon/1662-create-awesome-abstract-nebula-circle-shape-in-photoshop.jpg&amp;link2=http://www.tutorialhero.com/uploads/65716.jpg&amp;link3=http://www.tutorialhero.com/uploads/65716.jpg&amp;link4=http://s3.amazonaws.com/gt-production-icons/icons/icon/48745.gif?1277449254&amp;link5=http://www.tutorialhero.com/uploads/36939.jpg&amp;link6=http://www.cgtutorials.com/oneadmin/_files/linksdir/1219_Abstract_Brushing_Techniques__Particle_Zoom_Blast_Effect.gif&amp;link7=http://cdn3.ilovedesign.com/shared/assets/QRK013/234038/abstract.jpg&amp;link8=http://img.tutorialking.eu/icon/1486-creating-an-abstract-ecology-scene-with-3d-render.jpg&amp;link9=http://www.tutorialhero.com/uploads/68280.jpg&amp;link10=http://www.guidesandtutorials.net/images/avatar/98.gif

Action value:

random

Return value:

image

Parameters:

link1 [link2 link3 link4 link5 link6 link7 link8 link9 link10]

Code:

[img]http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=IMAGELINK1&link2=IMAGELINK2[/img]

___________________________________________________________________________________
Random links
This API attribute will link randomly to one of up to ten pages. You can follow the link more than once without refreshing the page and you will still be taken randomly to a different page each time. for example, this link will either take you to the google homepage or the Scratch homepage.

Action value:

random

Return value:

link

Parameters:

link1 [link2 link3 link4 link5 link6 link7 link8 link9 link10]

Code:

[url=http://www.blocks.scratchr.org/API.php?action=random&return=link&link1=LINK1URL&link2=LINK2URL]LINKTEXT/IMAGE[/url]

Last edited by sparks (2011-07-22 07:09:16)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#6 2011-07-19 12:19:51

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

^ home
Friend thumbnails and links

Friend thumbs
This API attribute allows you to display the user images for your n'th friend(s). For example, my newest friend has this image as their user image: http://www.blocks.scratchr.org/API.php?action=friends&amp;type=newest&amp;user=sparks&amp;return=image&amp;num=1

Action value:

friends

Type value:

newest

return value:

image

parameters:

user, num

Code:

[img]http://www.blocks.scratchr.org/API.php?action=friends&type=newest&user=USERNAMEHERE&return=image&num=NUMBER[/img]

___________________________________________________________________________________
Friend links
This API attribute allows you to link to the mystuff page of your n'th friend(s). For example, my newest friend's mystuff page can be seen here.

Action value:

friends

Type value:

newest

return value:

link

parameters:

user, num

Code:

[url=http://www.blocks.scratchr.org/API.php?action=friends&type=newest&user=USERNAMEHERE&return=link&num=NUMBER]LINK TEXT OR IMAGE HERE[/url]

___________________________________________________________________________________
to create a dynamic image of their user picture to go with the link, use the code below which applies the two API's.
For example:http://blocks.scratchr.org/API.php?action=friends&amp;user=sparks&amp;type=newest&amp;return=image&amp;num=1

Thanks to SeptimusHeap for helping with this section of the API!

Last edited by sparks (2011-07-21 18:55:56)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#7 2011-07-19 12:21:08

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Customising text returns

The API text return allows changing information about projects and people to be displayed as an image http://www.blocks.scratchr.org/API.php?action=text&amp;string=like_this!

The standard return above is designed to fit in with the rest of the fora as unobtrusive. However, should you wish to make a text return fit your signature colour scheme at all the grey background and black text simply won't do. This post lists all the additional attributes you can apply to both the text and the background of a text return.

Each attribute is simply added to the URL with an & between each one. For example &width=30&height=30. NEVER use spaces or the image won't show!.

Background attributes

Background size
parameters:

height, width

Notes: Both height and width are set in pixels and may only be whole numbers (integers). If only width is set the height will still snap to that of the font size and visa versa if width is set, not height.
http://www.blocks.scratchr.org/API.php?action=text&amp;string=example&amp;height=60&amp;width=100&amp;bgr=200&amp;bgg=200&amp;bgb=200 (colour slightly changed for clarification).

Code:

[img]http://www.blocks.scratchr.org/API.php?action=text&string=example&height=60&width=100[/img]

Background colour
parameters:

bgr, bgg, bgb

Notes: The background colour can be set using red, green and blue component values. Each can be set to a number from 0 to 255. bgr is red, bgg is green and bgb is blue. A good colour picker that shows you the RGB values of a colour can be found here.

If bgr, bgg and bgb are all unset, the background will revert to the standard forum grey (R=222 G=223 B=223). If you are setting one or two of the three values to 0 you can just leave them unset. So rather than sending &bgr=0&bgg=0&bgb=0 to get black you can just send &bgr=0 as the other two are assumed to be 0 too. In the same way, red can be sent as just &bgr=255.
http://www.blocks.scratchr.org/API.php?action=text&amp;string=example&amp;bgr=255&amp;bgg=255 (R:255 G:255 B:0)

Code:

[img]http://www.blocks.scratchr.org/API.php?action=text&string=example&bgr=255&bgg=255[/img

Background image
parameters:

bgimage

Notes: The background image can be set to an online image. Please note, it can take PNG, GIF, JPEG and BMP but does not always work with PNG and BMP. If a background image is set, the background size and colour attributes will do nothing.
http://www.blocks.scratchr.org/API.php?action=text&amp;string=example&amp;bgimage=http://www.clker.com/cliparts/Z/W/N/F/6/p/light-blue-square-th.png (http://www.clker.com/cliparts/Z/W/N/F/6/p/light-blue-square-th.png)

Code:

[img]http://www.blocks.scratchr.org/API.php?action=text&string=example&bgimage=LINKTOIMAGE[/img]

Text control and position
These attributes allow you to control the positioning of text.
parameters:

xpos, ypos, font_size, limitstring

Notes: xpos and ypos indicate how far from the top-left corner the text should be drawn (in pixels). The value must be a whole number (integer) for both attributes. They do not both have to be set together, one can be set by itself. When not set they revert to the default value of 0.
http://www.blocks.scratchr.org/API.php?action=text&amp;string=example&amp;height=60&amp;width=100&amp;bgr=200&amp;bgg=200&amp;bgb=200&amp;xpos=20&amp;ypos=30 (bg colour and size slightly changed for clarification). (x:20 y:30)

Code:

[img]http://www.blocks.scratchr.org/API.php?action=text&string=example&xpos=20&ypos=30[/img]

font_size controls the font size. The default size is 2.
http://www.blocks.scratchr.org/API.php?action=text&amp;string=example&amp;font_size=5 (font_size=5)

Code:

[img]http://www.blocks.scratchr.org/API.php?action=text&string=example&font_size=5[/img]

limitstring is an attribute that cuts return text short if it's longer than the specified number of characters.
http://www.blocks.scratchr.org/API.php?action=text&amp;string=example&amp;limitstring=5 (limited to 5 characters or less)

Code:

[img]http://www.blocks.scratchr.org/API.php?action=text&string=example&limitstring=5[/img]

Text colour
The text colour can be controlled using the same rgb system described above to control background colour.
parameters:

textr, textg, textb

http://www.blocks.scratchr.org/API.php?action=text&amp;string=example&amp;textr=10&amp;textg=100&amp;textb=200 (R:10 G:100 B:200)

Code:

[img]http://www.blocks.scratchr.org/API.php?action=text&string=example&textr=10&textg=100&textb=200[/img]

Last edited by sparks (2011-07-29 11:17:50)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#8 2011-07-19 12:25:26

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Yay!


http://i46.tinypic.com/dw7zft.png

Offline

 

#9 2011-07-19 12:26:13

RUMCHEERYPOOPOO
Scratcher
Registered: 2008-12-23
Posts: 100+

Re: ITopic: self-updating images and links in posts!

Looks like sparks is being awesome again  smile
My newest project:
(using sparks' code)
http://blocks.scratchr.org/API.php?user=RUMCHEERYPOOPOO&amp;action=projects&amp;type=newest&amp;return=image&amp;num=1

Last edited by RUMCHEERYPOOPOO (2011-07-19 12:31:28)


I AM ROOKWOOD101 NOW! (just so you know)

Offline

 

#10 2011-07-19 12:41:01

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

My 3 newest projects:
http://blocks.scratchr.org/API.php?user=SeptimusHeap&amp;action=projects&amp;type=newest&amp;return=image&amp;num=1
http://blocks.scratchr.org/API.php?user=SeptimusHeap&amp;action=projects&amp;type=newest&amp;return=image&amp;num=2
http://blocks.scratchr.org/API.php?user=SeptimusHeap&amp;action=projects&amp;type=newest&amp;return=image&amp;num=3

Last edited by SeptimusHeap (2011-07-19 12:43:42)


http://i46.tinypic.com/dw7zft.png

Offline

 

#11 2011-07-19 12:52:26

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Is there anything smaller than _sm? Icon sized would be handy for sigs...

EDIT: finished re-documenting online/offline part of the API. projects images and links next!

Last edited by sparks (2011-07-19 12:57:22)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#12 2011-07-19 15:33:31

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

You should capitalize the title.

News on the other APIs?


http://i46.tinypic.com/dw7zft.png

Offline

 

#13 2011-07-19 15:38:19

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

RUMCHEERYPOOPOO wrote:

Looks like sparks is being awesome again  smile

Yep.  big_smile

Post these on the wiki: dazman wanted dynamically updating projects on his page.  smile

Offline

 

#14 2011-07-19 15:49:01

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

I posted on the community portal, I hope that's the right place :S

EDIT: all currently supported API actions are documented.

Last edited by sparks (2011-07-19 15:58:01)


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#15 2011-07-19 16:06:07

Lucario621
Community Moderator
Registered: 2007-10-03
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Wow! That's really impressive! I can imagine this would be very useful, whether you want to use it here on the forum, or even on your own website perhaps. Kudos to you!  smile


http://i.imgur.com/WBkM2QQ.png

Offline

 

#16 2011-07-19 16:11:51

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Thanks, Lucario, it started out a just being a way to show which Scratchers on a collab thread were online, but once I added the "link your own online and offline image" people started using it for their sigs  tongue  I'm just developing it some more now so we'll see where it goes!


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#17 2011-07-19 16:14:57

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

sparks wrote:

Thanks, Lucario, it started out a just being a way to show which Scratchers on a collab thread were online, but once I added the "link your own online and offline image" people started using it for their sigs  tongue  I'm just developing it some more now so we'll see where it goes!

Ooh! What now? Newest favorite? Newest friend?


http://i46.tinypic.com/dw7zft.png

Offline

 

#18 2011-07-19 16:16:23

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Possibly! It's all possible, do you think newest friend will be helpful? What I really need is to work out how to get the page to make a text image, then text such as project names etc will work too!


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#19 2011-07-19 16:33:54

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Yeah, project names are a must...


http://i46.tinypic.com/dw7zft.png

Offline

 

#20 2011-07-19 17:48:23

johnnydean1
Scratcher
Registered: 2010-02-12
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Cool.


You can now reach me on Twitter @johnnydean1_

Offline

 

#21 2011-07-19 17:52:36

SeptimusHeap
Scratcher
Registered: 2010-02-01
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

New name = way better.


http://i46.tinypic.com/dw7zft.png

Offline

 

#22 2011-07-19 18:01:26

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Now, I need 3 random projects xD of any user xD


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#23 2011-07-19 19:08:47

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Great stuff, sparks!  Thanks for sharing this wonderfullness  smile


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#24 2011-07-19 19:11:58

sparks
Community Moderator
Registered: 2008-11-05
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Paddle2See wrote:

Great stuff, sparks!  Thanks for sharing this wonderfullness  smile

Thanks, Paddle2See, I'm glad you like it! (especially because I was just a little bit worried that after the Scratch team spent so much time disallowing php or changing.... stuff in posts they wouldn't like this  tongue  (technically the API is no more dangerous than any other image tag though))  smile


http://img541.imageshack.us/img541/7563/scratchbetabanner.png

Offline

 

#25 2011-07-19 20:43:43

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: ITopic: self-updating images and links in posts!

Well done Sparks! You don't know how much I'll want to see the code of this.  lol

Offline

 

Board footer