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

#1 2011-11-20 14:47:52

kayybee
Scratcher
Registered: 2009-12-07
Posts: 1000+

Auto updating images/text

Since sparks or people stuff thingy is too busy to make auto updating images for post counts, if someone could teach me how an API works I bet I could figure it out. (don't doubt me, I do know PHP and stuff)

So can someone teach me how it works?

Offline

 

#2 2011-11-20 15:27:04

cocolover76
Scratcher
Registered: 2011-10-09
Posts: 500+

Re: Auto updating images/text

For what you want, you would need for you to have a topic for everyone to post in if they wanted to use the API, and then make PHP code to search for the username in every page in that topic, and when it finds it, gets the post count, and renders it with this:

Code:

<?php
/*
text to image converter
daftlogic 
www.daftlogic.com
*/
Header("Content-type: image/png");
class textPNG 
{
    var $font = 'fonts/TIMES.TTF'; //default font. directory relative to script directory.
    var $msg = "no text"; // default text to display.
    var $size = 24; // default font size.
    var $rot = 0; // rotation in degrees.
    var $pad = 0; // padding.
    var $transparent = 1; // transparency set to on.
    var $red = 0; // black text...
    var $grn = 0;
    var $blu = 0;
    var $bg_red = 255; // on white background.
    var $bg_grn = 255;
    var $bg_blu = 255;
    
    function draw() 
    {
        $width = 0;
        $height = 0;
        $offset_x = 0;
        $offset_y = 0;
        $bounds = array();
        $image = "";
    
        // get the font height.
        $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, "W");
        if ($this->rot < 0) 
        {
            $font_height = abs($bounds[7]-$bounds[1]);        
        } 
        else if ($this->rot > 0) 
        {
        $font_height = abs($bounds[1]-$bounds[7]);
        } 
        else 
        {
            $font_height = abs($bounds[7]-$bounds[1]);
        }
        // determine bounding box.
        $bounds = ImageTTFBBox($this->size, $this->rot, $this->font, $this->msg);
        if ($this->rot < 0) 
        {
            $width = abs($bounds[4]-$bounds[0]);
            $height = abs($bounds[3]-$bounds[7]);
            $offset_y = $font_height;
            $offset_x = 0;
        } 
        else if ($this->rot > 0) 
        {
            $width = abs($bounds[2]-$bounds[6]);
            $height = abs($bounds[1]-$bounds[5]);
            $offset_y = abs($bounds[7]-$bounds[5])+$font_height;
            $offset_x = abs($bounds[0]-$bounds[6]);
        } 
        else
        {
            $width = abs($bounds[4]-$bounds[6]);
            $height = abs($bounds[7]-$bounds[1]);
            $offset_y = $font_height;;
            $offset_x = 0;
        }
        
        $image = imagecreate($width+($this->pad*2)+1,$height+($this->pad*2)+1);
        $background = ImageColorAllocate($image, $this->bg_red, $this->bg_grn, $this->bg_blu);
        $foreground = ImageColorAllocate($image, $this->red, $this->grn, $this->blu);
    
        if ($this->transparent) ImageColorTransparent($image, $background);
        ImageInterlace($image, false);
    
        // render the image
        ImageTTFText($image, $this->size, $this->rot, $offset_x+$this->pad, $offset_y+$this->pad, $foreground, $this->font, $this->msg);
    
        // output PNG object.
        imagePNG($image);
        }
    }

    $text = new textPNG;

    if (isset($msg)) $text->msg = $msg; // text to display
    if (isset($font)) $text->font = $font; // font to use (include directory if needed).
    if (isset($size)) $text->size = $size; // size in points
    if (isset($rot)) $text->rot = $rot; // rotation
    if (isset($pad)) $text->pad = $pad; // padding in pixels around text.
    if (isset($red)) $text->red = $red; // text color
    if (isset($grn)) $text->grn = $grn; // ..
    if (isset($blu)) $text->blu = $blu; // ..
    if (isset($bg_red)) $text->bg_red = $bg_red; // background color.
    if (isset($bg_grn)) $text->bg_grn = $bg_grn; // ..
    if (isset($bg_blu)) $text->bg_blu = $bg_blu; // ..
    if (isset($tr)) $text->transparent = $tr; // transparency flag (boolean).

    $text->draw(); // GO!!!!!
?>

There is a demo of the renderer here.

Last edited by cocolover76 (2011-11-20 15:27:29)


http://i.imgur.com/HfEPZ.gifhttp://i.imgur.com/pvKb6.png

Offline

 

#3 2011-11-21 08:50:33

kayybee
Scratcher
Registered: 2009-12-07
Posts: 1000+

Re: Auto updating images/text

Okay thanks.

i could also just find an old post, right?

Offline

 

#4 2011-11-21 09:33:11

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

Re: Auto updating images/text

kayybee wrote:

Okay thanks.

i could also just find an old post, right?

Yep.

Offline

 

Board footer