Bluga Webthumb

February 4th, 2015

Bluga Webthumb API Wrapper for Zend Framework

Based on the original API Wrapper written by Joshua Eichorn & Cal Evans, this is an implementation that uses Zend Framework structure and libraries – Rest Client & Http Response.

Webthumbs for Zend Framework (581 downloads)

Setup/Installation

This wrapper uses the Oxygen namespace, just like ZF uses Zend. To be able to use this wrapper you must include both Zend and Oxygen folders in your include_path.

License/Copyright

You are free to do whatever you want with this piece of code (redistribute, change, adapt, etc), as long as the credits are being kept in the header of the main file.

Basic usage

<?php
//assuming this file is on the same level with the library folder
define( 'APP_PATH', realpath(pathinfo(__FILE__, PATHINFO_DIRNAME)) );

// assuming library folder is named "lib"
define( 'LIB_FOLDER', 'lib' );

set_include_path(APP_PATH . DIRECTORY_SEPARATOR . LIB_FOLDER . PATH_SEPARATOR . get_include_path());

require_once('Oxygen/Service/Bluga.php');

$apikey = 'your api key';
$url = 'http://mandagreen.com/';
try {
    $webthumb = Oxygen_Service_Bluga::webthumb($apikey);
    $status = $webthumb->checkCreditStatus();
    $reqUsed = $status['used-this-month'];

    $job = $webthumb->addUrl($url, 'small', 1024, 768);
    $webthumb->submitRequests();
    while( !$webthumb->readyToDownload() ) {
    sleep(5);
    echo "Checking Job Status\n";
    $webthumb->checkJobStatus();
}

    $webthumb->fetchToFile($job, $job->status->id . '_thumb.jpg', 'small');
    $webthumb->fetchToFile($job, $job->status->id . '_normal.jpg', 'medium');
    $webthumb->fetchToFile($job, $job->status->id . '_large.jpg', 'medium2');

    $status = $webthumb->checkCreditStatus();
    $reqUsed = $status['used-this-month'] - $reqUsed;
    echo 'Req used: ' . $reqUsed;
}
catch( Exception $e ) {
    echo $e->getMessage();
}

A few explanations on how this works. You can either directly instantiate a new Oxygen_Service_Bluga_Webthumb object or use the Oxygen_Service_Bluga::webthumb factory method:

$webthumb = new Oxygen_Service_Bluga_Webthumb( $apikey );

or

$webthumb = new Oxygen_Service_Bluga::webthumb( $apikey );

I prefer the latter one, for convenience.

You can check the account status at anytime by using the “checkCreditStatus” method, which returns an array

$webthumb->checkCreditStatus()

You can add as many jobs as you need (but try keeping a sane number of url’s in the request as the processing time might become a problem). To add a job, call:

$job = $webthumb->addUrl($url, 'small', 1024, 768);

Once you added all the jobs you need, send the request and wait for the server to process it (note the while loop that checks the status of the request).
Finally retrieve the images using

$webthumb->fetchToFile($job, $filename, $size);

where $filename is the destination filename (and path) and $size is the desired size of the snapshot – which can be small (80×60), medium (160×120), medium2 (320×240), large (640×480) or excerpt (400×150, taken from the top left corner by default)

To better understand how to use this library, I suggest having a look at the standard API documentation page

Suggestions and comments are most welcomed.

  1. No comments yet.