PHP Classes

File: EPub.ExampleImg.php

Recommend this page to a friend!
  Classes of Asbjorn Grandt  >  EPub  >  EPub.ExampleImg.php  >  Download  
File: EPub.ExampleImg.php
Role: Example script
Content type: text/plain
Description: Simple Example showing only image import features
Class: EPub
Create ebook in EPUB format for ex. Apple iPad
Author: By
Last change: Merge pull request #13 from raqy/master

The changes look ok.
Date: 2 years ago
Size: 3,586 bytes


Class file image Download
ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 1);

// Example.
// Create a test book for download.
// ePub uses XHTML 1.1, preferably strict.
$book = new EPub();

$book->setTitle("Test book");
$book->setIdentifier("", EPub::IDENTIFIER_URI); // Could also be the ISBN number, prefered for published books, or a UUID.
$book->setLanguage("en"); // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc.
$book->setDescription("This is a brief description\nA test ePub book as an example of building a book in PHP");
$book->setAuthor("John Doe Johnson", "Johnson, John Doe");
$book->setPublisher("John and Jane Doe Publications", ""); // I hope this is a non existant address :)
$book->setDate(time()); // Strictly not needed as the book date defaults to time().
$book->setRights("Copyright and licence information specific for the book."); // As this is generated, this _could_ contain the name or licence information of the user who purchased the book, if needed. If this is used that way, the identifier must also be made unique for the book.

$cssData = "body {\n margin-left: .5em;\n margin-right: .5em;\n text-align: justify;\n}\n\np {\n font-family: serif;\n font-size: 10pt;\n text-align: justify;\n text-indent: 1em;\n margin-top: 0px;\n margin-bottom: 1ex;\n}\n\nh1, h2 {\n font-family: sans-serif;\n font-style: italic;\n text-align: center;\n background-color: #6b879c;\n color: white;\n width: 100%;\n}\n\nh1 {\n margin-bottom: 2px;\n}\n\nh2 {\n margin-top: -2px;\n margin-bottom: 2px;\n}\n";
$book->addCSSFile("Styles/styles.css", "css1", $cssData);

$content_start =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
. " \"\">\n"
. "<html xmlns=\"\">\n"
. "<head>"
. "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
. "<link rel=\"stylesheet\" type=\"text/css\" href=\"../Styles/styles.css\" />\n"
. "<title>Test Book</title>\n"
. "</head>\n"
. "<body>\n";

$bookEnd = "</body>\n</html>\n";
$fileDir = './PHPePub';
// setting timezone for time functions used for logging to work properly

$chapter = $content_start . "<p><img src='demo/512x700_g1.gif' alt='Image' /></p>\n"
. "<p><img src='demo/rxhVVUP.gif' alt='Animated Gif' /></p>\n"
. "<p><img src='demo/512x700_2.jpg' alt='none' /></p>\n"
. "<p><img src='demo/512x700_3.jpg' alt='Demo 2' /></p>\n"
. $bookEnd;

$book->isGifImagesEnabled = TRUE;

$book->addChapter("Prologue", "Texts/Prologue.html", $content_start . "<h2>Prologue</h2>\n" . $bookEnd);
$book->addChapter("Chapter 1", "Texts/Chapter1.html", $chapter, false, EPub::EXTERNAL_REF_ADD, $fileDir);
$book->addChapter("Chapter 2", "Texts/Chapter2.html", $chapter, false, EPub::EXTERNAL_REF_REPLACE_IMAGES, $fileDir);

// Send the book to the client. ".epub" will be appended if missing.
$zipData = $book->sendBook("ExampleBookImg");

// After this point your script should call exit. If anything is written to the output,
// it'll be appended to the end of the book, causing the epub file to become corrupt.