#1 2008-07-10 15:41:06

cockydagger
Administrator
From: Simpsonville, SC
Registered: 2008-02-24
Posts: 2402
Website

Source Code to create a graphic of the titles in a news feed for Googl

Here is a free PHP script to convert the titles of a news feed to a PNG graphic for display in Google Lively.  To display a news feed in Google Lively, go to the "Add object" button, search for "Picture Frame".  Click on add to room and then edit properties.  In Gadget Options place the url of the PHP script in the option box.

example url:  http://www.intlstockexchange.com/rssimage.php
in Lively example:  http://www.intlstockexchange.com/punbb/LivelyChat.php


Code:

<?php
/*
    Dynamic Heading Generator
    By Stewart Rosenberger
    http://www.stewartspeak.com/headings/

    Additional documentation on PHP's image handling capabilities can
    be found at http://www.php.net/image/
*/
/* 
 Original Code @ http://www.alistapart.com/d/dynatext/heading.php.txt
 Modified by http://intlstockexchange.com on 7/10/2008 to read a news feed, convert to graphic in order to add to Google Lively */

$font_file  = 'font.ttf' ;
$font_size  = 8 ;
$font_color = '#00CC00' ;
$background_color = '#ffffff' ;
$transparent_background  = false ;
$cache_images = true ;
$cache_folder = 'cache' ;


$mime_type = 'image/png' ;
$extension = '.png' ;
$send_buffer_size = 4096 ;

// check for GD support
if(!function_exists('ImageCreate'))
    fatal_error('Error: Server does not support PHP image generation') ;


include('config.php');


 $url = "http://www.intlstockexchange.com/rss.php";

 $p = xml_parser_create();
 xml_parse_into_struct($p,file_get_contents($url),$results,$index);
 xml_parser_free($p);
 $text = "Latest News from the ISE\n";
 for ($i=1;$i<=7;$i++) {
   $text .= $results[$index[TITLE][$i]][value] . "\n";;
 }



/// look for cached copy, send if it exists
$hash = md5(basename($font_file) . $font_size . $font_color .
            $background_color . $transparent_background . $text) ;

$cache_filename = $cache_folder . '/' . $hash . $extension ;
if($cache_images && ($file = @fopen($cache_filename,'rb')))
{
    header('Content-type: ' . $mime_type) ;
    while(!feof($file))
        print(($buffer = fread($file,$send_buffer_size))) ;
    fclose($file) ;
    exit ;
}

// check font availability
$font_found = is_readable($font_file) ;
if(!$font_found)
{
    fatal_error('Error: The server is missing the specified font.') ;
}

// create image
$background_rgb = hex_to_rgb($background_color) ;
$font_rgb = hex_to_rgb($font_color) ;
$dip = get_dip($font_file,$font_size) ;
$box = @ImageTTFBBox($font_size,0,$font_file,$text) ;
$image = @ImageCreate(350,400);
if(!$image || !$box)
{
    fatal_error('Error: The server could not create this heading image.') ;
}

// allocate colors and draw text
$background_color = @ImageColorAllocate($image,$background_rgb['red'],
    $background_rgb['green'],$background_rgb['blue']) ;
$font_color = ImageColorAllocate($image,$font_rgb['red'],
    $font_rgb['green'],$font_rgb['blue']) ;
ImageTTFText($image,$font_size,0,90,55,$font_color,$font_file,$text);
// set transparency
if($transparent_background)
    ImageColorTransparent($image,$background_color) ;

header('Content-type: ' . $mime_type) ;
ImagePNG($image) ;

// save copy of image for cache
if($cache_images)
{
    @ImagePNG($image,$cache_filename) ;
}

ImageDestroy($image) ;
exit ;


/*
        try to determine the "dip" (pixels dropped below baseline) of this
        font for this size.
*/
function get_dip($font,$size)
{
        $test_chars = 'abcdefghijklmnopqrstuvwxyz' .
                              'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
                                  '1234567890' .
                                  '!@#$%^&*()\'"\\/;.,`~<>[]{}-+_-=' ;
        $box = @ImageTTFBBox($size,0,$font,$test_chars) ;
        return $box[3] ;
}


/*
    attempt to create an image containing the error message given.
*/
function fatal_error($message)
{
    // send an image
    if(function_exists('ImageCreate'))
    {
        $width = ImageFontWidth(5) * strlen($message) + 10 ;
        $height = ImageFontHeight(5) + 10 ;
        if($image = ImageCreate($width,$height))
        {
            $background = ImageColorAllocate($image,255,255,255) ;
            $text_color = ImageColorAllocate($image,0,0,0) ;
            ImageString($image,5,5,5,$message,$text_color) ;
            header('Content-type: image/png') ;
            ImagePNG($image) ;
            ImageDestroy($image) ;
            exit ;
        }
    }

    // send 500 code
    header("HTTP/1.0 500 Internal Server Error") ;
    print($message) ;
    exit ;
}


/*
    decode an HTML hex-code into an array of R,G, and B values.
    accepts these formats: (case insensitive) #ffffff, ffffff, #fff, fff
*/
function hex_to_rgb($hex)
{
    // remove '#'
    if(substr($hex,0,1) == '#')
        $hex = substr($hex,1) ;

    // expand short form ('fff') color
    if(strlen($hex) == 3)
    {
        $hex = substr($hex,0,1) . substr($hex,0,1) .
               substr($hex,1,1) . substr($hex,1,1) .
               substr($hex,2,1) . substr($hex,2,1) ;
    }

    if(strlen($hex) != 6)
 
        fatal_error('Error: Invalid color "'.$hex.'"') ;

    // convert
    $rgb['red'] = hexdec(substr($hex,0,2)) ;
    $rgb['green'] = hexdec(substr($hex,2,2)) ;
    $rgb['blue'] = hexdec(substr($hex,4,2)) ;

    return $rgb ;
}
?>

I don't want to achieve immortality through my work... I want to achieve it through not dying.

Woody Allen

Offline

 

#2 2008-07-11 14:45:17

cockydagger
Administrator
From: Simpsonville, SC
Registered: 2008-02-24
Posts: 2402
Website

Re: Source Code to create a graphic of the titles in a news feed for Googl

This script can also work with Vivaty but Vivaty doesn't support PNG, so two lines need to be changed.

$mime_type = 'image/png' ;
$extension = '.png' ;

change to

$mime_type = 'image/jpeg' ;
$extension = '.jpg' ;



ImagePNG($image) ;

change to

Imagejpg($image) ;


I don't want to achieve immortality through my work... I want to achieve it through not dying.

Woody Allen

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson


Web Hosting and Web Design by SLB Development