GDBook 0.1, a free text to image converter

I hacked together a small text to image converter for converting ebooks. Some devices that can be used as ebook readers will only display images (PSP, Juice Box). Although theoretically it is possible to write real ebook reading software for these devices, for now my GDBook program might help.

GDBook 0.1 is written in PHP, and intended to be run from the command line. It will save image files to your local file system. It does not take arguments: you need to edit the program file to change the settings.

Yes, that’s rather barbaric, but since I do not own a Juice Box or a PSP, I am not likely to develop this program further. GDBook is GPLed, so please take on its development if you like.

[1st page of a Stephen Leacock satire] [Second page of the Leacock story]

Things that I think GDBook does better than the competition:

  • No JPEG
  • Regular line endings (broken at word boundaries)

Things that JPEGBook does better:

  • Slick interface
  • Page numbers
  • Image backgrounds

Requirements:

GDLib comes standard with the MS Windows binaries for PHP, but is not installed. The GDLib web site explains how to install it. Keep in mind that the extension name in the php.ini file should be the same as in your extensions directory: on my system the two differed.

Known bug: when a paragraph ends in a wide line, some of the text may spill over the border and even off the page.

The text that is used in the current configuration is The War Sacrifices of Mr. Spugg (also as readable HTML version) by Stephen Leacock. Worth reading even if you have no need for the program.

19 responses to “GDBook 0.1, a free text to image converter”

  1. Zach says:

    Damn, you beat me; I have a C++ program that I’ve written that saves straight to raw JBP files, but I don’t quite feel like finishing the fifty-odd pixel font characters I have left to do. However, I’m currently working on something a bit more useful to me – a set of pocket technical documents, right now the 7400 series of logic chips.
    Perhaps you should go out and get yourself a JuiceBox. It makes a wonderful slurping noise when you turn it on…

  2. Branko Collin says:

    Zach, do like I did and use GDLib/Freetype or Pango/Freetype (or perhaps even just Freetype if that offers the right primitives). That way you can leave the measuring and placing of text to the specialized libraries.

    I haven’t had time to try and see if they sell Juice Boxes over here. I guess I could buy one via eBay, but haven’t bought there before, and don’t know which sellers I can trust.

  3. Zach says:

    I present to you, GDBook: JBRemix (well, it’s linked on that page). I used your code, but modified it to take command line args (jbremix input.txt outprefix) and to save straight to JBP. We dah man. Oh, and, if you want (and if Target still has them), I’d buy one and sell it to you on ebay for no markup ($25 + shipping for JuiceBox and the MP3/JPEG kit, which includes the 32MB SD card, the reader, the cable, the cd).

  4. rd says:

    Great idea, unfortunately I’m not technically savvy enough to get this up and running. You got a short guide you could bang out on how a windows user could get this done? I mean besides RTFM… ;)

  5. Zach says:

    What I did: Google for AppServ and install it (it’s apache with php + mysql, good for any sort of web design on your computer). Make sure c:\appserv\php is in your PATH variable (on win 2k/xp it’s under advanced settings of right click my computer -> properties). Find php.ini, which should be in c:\winnt or c:\windows depending on what you’re using, and find the line that talks about enabling argc and argv. It should be set to off; replace it with on. From there, use a command prompt (start -> run -> cmd) and cd to the directory of your php file and the text files. Use syntax ‘php jbremix.php input.txt outfile’. Good luck. I’ll see about porting this to run on the web…

  6. Branko Collin says:

    Zach, thanks for the offer. Let me think about it.

    RD, the steps I took:
    – Download and install PHP for Windows

    – Edit the PHP.ini file to “comment out” the gdlib extension. The GDLib FAQ has got extensive instructions. (See the links in the entry.)

    – Start a DOS console (command line), either by clicking its icon or by choosing Start/Execute … “CMD”

    – Run the cli version of PHP by entering “c:\php\cli\php.exe -f gdbook.php”. (This assumes PHP is installed in C:\php.

    These instructions aren’t very detailed I am afraid. It is likely that your system differs slightly from mine, and you will have to experiment to get things right.

  7. Luke says:

    Seeing as this is the first time I have ever tried to use php like this, I am failing miserably. I am getting an error at the cmd prompt saying: Fatal Error: call to undefined function: imagecreatetruecolor in C:\PHP\jbremix.php in line 77. What am I doing wrong? Any help is much appreciated.

  8. Luke says:

    Also I get a window pop up saying “Unknown(): Unable to load dynamic library !./php_gd2.dll! – the specified module could not be found”. I assume have installed gdlib incorrectly. I’m using Windows 2k and was very unsure on how to install gdlib and couldn’t find any mention in the faq. Can you help at all please?

  9. Luke says:

    Ok, I have found the necessary in the GDLib faq and done that, but I am still having problems. Instructions for us Windows users would be great.

  10. Luke says:

    Who would have thought it? I got it working in the end! I used Appserv and the jbremix.php. Yippee, many thanks. Also a web port of this would be great.

  11. Branko Collin says:

    Luke, the imagecreatetruecolor() is a GD function; if you haven’t got GDLib installed correctly, the two errors you got are likely to occur.

    As I mentioned in the article (but perhaps did not expand upon enough), there is a difference between the way the GDLib .dll file is called and the way it is referenced from the php.ini file. The GDLib FAQ does not mention this, so that is something extra to watch out for.

    However, if the AppServ solution works out of the box, and you do not feel like tinkering too much, AppServ is definitely the way to go.

    BTW, sorry for the late reply, but the weather over here is such as to keep me away from the computer. :-)

  12. Condor says:

    Is there a way of converting jpg to JBP and back to jpeg? kind of like storage on the move? I’ve got some images on my juice box i want to convert back to there original format but cant.

    condor_joe_g2@yahoo.co.uk

  13. Branko Collin says:

    Presumably the Juicebox comes with software for conversion from JPEG to JBP, or you wouldn’t be able to save your own photos to it.

    From what I understand, JBP is a raw format with four bits per channel of Red, Green and Blue (RGB). ImageMagick can convert from raw RGB to JPEG; I haven’t tested it to see if it will take 4bpc images though.

  14. Branko Collin says:

    Condor, you can find some more info at the Linux-Hacker forum. Good luck!

  15. ShoeUnited says:

    I love this proggie. I’m attempting to use to to make quotes as images. Anyway, long story short: I found that having a name like -Oscar Wilde or -George Carlin would cut off the last names. I solved this problem by adding a space and 2 dashes ( –) after the last name for an individual file. I was wondering if there was something to indicate a page break? Or am I stuck at guessing how long a line will run? I love it every way though :D I mean, I was just curious if there was a shortcut…

    -Shoe

  16. brankl says:

    I am not sure I understand your problem, but the parser is very simple. It knows about words, lines, and files, and that’s it. It rewraps lines at word boundaries, and outputs them to the image until the image is filled up with lines. Anyway, it’s been awhile since I looked at the code, and I don’t use the program myself. I suggest that if you understand PHP, you hack it yourself. Good luck!

  17. Ian says:

    I really like this program, thanks for sharing it. I am however having a slight problem with it in that on some pages certain lines have the text running off the end of the line, almost as if the algorithm that calculates how many words can be fitted on each line is failing occasionally. This happens even if I set a large right margin, I’ve even tried 40.

    Can anyone suggest what might be causing the problem or whether there might be a bug in the code somewhere?

  18. Ian says:

    Following on from my last comment I’ve done some further investigating to see if I could see under what conditions the problem was occurring and it seems to be whenever the last word of a sentence (ie a word that precedes /r /n) makes the line too long to display and it needs to be put back onto the “stack”. If I then add a space between the word and the carriage return/linefeed the problem doesn’t arise.

    Not sure whether I know enough PHP to fix it though but maybe someone more familiar can suggest what needs to be changed to fix the problem ?

  19. brankl says:

    You could pre-process your input files so that a line always ends in a space, or you could change the program so that it adds spaces to lines. That’s an unelegant work-around, but it would still work.

Leave a Reply

Your email address will not be published.