I used Bookleteer’s Storycube API to make physical souvenirs of ebooks, as described in my post on Bookcubes. In this post I’ll run through the code used to manipulate Bkkeepr data and send it to the API.
Bkkeepr pulls a bunch of data out, which includes your username, the book cover image, the book’s title and author, the dates you started and finished reading it, and the number of bookmarks you made in it.
The Storycube API only accepts images, not text, so if you want text you’re going to have to build it into images. Here’s a useful piece of code for creating images with text on the fly, using the GD Graphics library which is built into most versions of PHP. It’s not pretty, but it’s useful for quick prototyping of data into images:
$im = @imagecreatetruecolor(156, 156)
or die(‘Cannot Initialize new GD image stream’);
$grey = imagecolorallocate($im, 231, 231, 231);
imagefilledrectangle($im, 0, 0, 156, 156, $grey);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 3, 10, 35, $title, $text_color);
imagestring($im, 3, 75, 50, ‘by’, $text_color);
imagestring($im, 3, 10, 65, $author, $text_color);
imagejpeg($im,’cubes/title-image.jpg’);
imagedestroy($im);
This code creates a new image of 156 x 156 pixels, colours it grey, then writes some text (“Title” by “Author”) in black across three lines, before saving the images to the ‘cubes’ directory, and wiping the temporary image. All these functions are well documented at http://php.net/manual/en/book.image.php – mostly, you just supply the text or colour for insertion, and a couple of reference points.
Exactly the same process is followed to write the start date, finish date (if applicable) and bookmarks panel:
$im = @imagecreatetruecolor(156, 156)
or die(‘Cannot Initialize new GD image stream’);
$grey = imagecolorallocate($im, 231, 231, 231);
imagefilledrectangle($im, 0, 0, 156, 156, $grey);
$text_color = imagecolorallocate($im, 0, 0, 0);
imagestring($im, 3, 10, 30, ‘Started:’, $text_color);
imagestring($im, 3, 10, 45, $started, $text_color);
if ($finished !== ‘0000-00-00 00:00:00’) {
imagestring($im, 3, 10, 70, ‘Finished:’, $text_color);
imagestring($im, 3, 10, 85, $finished, $text_color);
}
imagestring($im, 3, 10, 105, ‘Bookmarks: ‘.$bookmarks_num, $text_color);
$im = imagerotate($im, 180, 0);
imagejpeg($im,’cubes/time-image.jpg’);
imagedestroy($im);
Note that here we also use imagerotate() to spin this panel 180° so that it’s the same orientation as the book cover panel.
The next step is to send the completed panels to the Storycube API. First we need to get an authentication token:
$username = ‘USERNAME’;
$password = ‘PASSWORD’;
$token = file_get_contents(‘http://generator.bookleteer.com/authenticate.html?username=’.$username.’&password=’.$password);
Then complete a request array, including the authentication token, and send this via curl to Bookleteer:
$fields=array(
‘token’=>$token,
‘author’=>$user,
‘title’=>$title.’ by ‘.$author,
‘creation_date’=>”,
‘num_images’=>’6’,
‘image_1’=>’@cubes/cover-image.jpg’,
‘image_2’=>’@cubes/title-image.jpg’,
‘image_3’=>’@cubes/time-image.jpg’,
‘image_4’=>’@cubes/bkkeepr-image.jpg’,
‘image_5’=>’@cubes/blank-image.jpg’,
‘image_6’=>’@cubes/blank-image.jpg’,
‘paper_size’=>’A4’,
‘design’=>’Diffusion’);$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,’http://generator.bookleteer.com/createStoryCube’);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
$result=curl_exec ($curl);
$content_type=curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
curl_close ($curl);
Note that the path to the images in the request array are prefaced by ‘@’ – this is because curl needs to upload the images to the remote server, and also means those need to be local paths: you can’t send a URL. All images to be sent need to be stored locally – which is done here by calling Amazon for the cover image and saving it locally (cubes/cover-image.jpg).
The Storycube API returns either a pdf, or an error code. So we check which it is via the content-type, and if all is well we save the file with a unique timestamp and offer it for download, or we print the error message:
if ($content_type == ‘application/pdf’) {
$filename = ‘bookcube-‘.time().’.pdf’;
$fp = fopen(‘cubes/’.$filename, ‘w’);
fwrite($fp, $result);
fclose($fp);
echo ‘Done: Download‘;
}
else {
echo $result;
}
And that’s it.
James Bridle, April 2010
5 replies on “James Bridle : residency part 1”
[…] I’ll be further exploring the Bookleteer API in a future post. The code for the experiments can be found on the Bookleteer blog. […]
[…] This post was mentioned on Twitter by tony white, bookleteer. bookleteer said: fruits of 1st @bookleteer api virtual residency emerging : @stml on his bookcubes http://bit.ly/aVg5S2 & http://bit.ly/aaCobI […]
[…] data initiatives. We hope to see lots of exciting ideas building on the first experiments – bookcubes – done by James Bridle last spring. Look out too for some forthcoming experiments by Simon Pope […]
[…] record its life with you. While Bookleteer are developing their tangible souvenir concept through Bookcubes and sensory […]
[…] a small experiment from James Bridle last year for his Bkkeepr project – read about his Bookcubes here – as a first step in exploring what could be done. Get your own free bookleteer account and […]