setCoverImage() public method

If the $imageData is not set, the function assumes the $fileName is the path to the image file. The styling and structure of the generated XHTML is heavily inspired by the XHTML generated by Calibre.
public setCoverImage ( string $fileName, string $imageData = null, string $mimetype = null ) : boolean
$fileName string Filename to use for the image, must be unique for the book.
$imageData string Binary image data
$mimetype string Image mimetype, such as "image/jpeg" or "image/png".
return boolean $success
Exemplo n.º 1
0
 /**
  * Use PHPePub to dump a .epub file.
  *
  * @return Response
  */
 private function produceEpub()
 {
     /*
      * Start and End of the book
      */
     $content_start = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" . "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n" . '<head>' . "<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" . "<title>wallabag articles book</title>\n" . "</head>\n" . "<body>\n";
     $bookEnd = "</body>\n</html>\n";
     $book = new EPub(EPub::BOOK_VERSION_EPUB3);
     /*
      * Book metadata
      */
     $book->setTitle($this->title);
     // Could also be the ISBN number, prefered for published books, or a UUID.
     $book->setIdentifier($this->title, EPub::IDENTIFIER_URI);
     // 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->setLanguage($this->language);
     $book->setDescription('Some articles saved on my wallabag');
     foreach ($this->authors as $author) {
         $book->setAuthor($author, $author);
     }
     // I hope this is a non existant address :)
     $book->setPublisher('wallabag', 'wallabag');
     // Strictly not needed as the book date defaults to time().
     $book->setDate(time());
     $book->setSourceURL($this->wallabagUrl);
     $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'PHP');
     $book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, 'wallabag');
     /*
      * Front page
      */
     if (file_exists($this->logoPath)) {
         $book->setCoverImage('Cover.png', file_get_contents($this->logoPath), 'image/png');
     }
     $book->addChapter('Notices', 'Cover2.html', $content_start . $this->getExportInformation('PHPePub') . $bookEnd);
     $book->buildTOC();
     /*
      * Adding actual entries
      */
     // set tags as subjects
     foreach ($this->entries as $entry) {
         foreach ($entry->getTags() as $tag) {
             $book->setSubject($tag->getLabel());
         }
         // the reader in Kobo Devices doesn't likes special caracters
         // in filenames, we limit to A-z/0-9
         $filename = preg_replace('/[^A-Za-z0-9\\-]/', '', $entry->getTitle());
         $chapter = $content_start . $entry->getContent() . $bookEnd;
         $book->addChapter($entry->getTitle(), htmlspecialchars($filename) . '.html', $chapter, true, EPub::EXTERNAL_REF_ADD);
     }
     return Response::create($book->getBook(), 200, ['Content-Description' => 'File Transfer', 'Content-type' => 'application/epub+zip', 'Content-Disposition' => 'attachment; filename="' . $this->title . '.epub"', 'Content-Transfer-Encoding' => 'binary']);
 }
Exemplo n.º 2
0
// 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.
$book->setSourceURL("http://JohnJaneDoePublications.com/books/TestBook.html");
$book->addDublinCoreMetadata(DublinCore::CONTRIBUTOR, "PHP");
$book->setSubject("Test book");
$book->setSubject("keywords");
$book->setSubject("Chapter levels");
// Insert custom meta data to the book, in this cvase, Calibre series index information.
$book->addCustomMetadata("calibre:series", "PHPePub Test books");
$book->addCustomMetadata("calibre:series_index", "1");
$log->logLine("Set up parameters");
$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";
$log->logLine("Add css");
$book->addCSSFile("styles.css", "css1", $cssData);
// This test requires you have an image, change "demo/cover-image.jpg" to match your location.
$log->logLine("Add Cover Image");
$book->setCoverImage("Cover.jpg", file_get_contents("demo/cover-image.jpg"), "image/jpeg");
// A better way is to let EPub handle the image itself, as it may need resizing. Most e-books are only about 600x800
//  pixels, adding mega-pixel images is a waste of place and spends bandwidth. setCoverImage can resize the image.
//  When using this method, the given image path must be the absolute path from the servers Document root.
/* $book->setCoverImage("/absolute/path/to/demo/cover-image.jpg"); */
// setCoverImage can only be called once per book, but can be called at any point in the book creation.
$log->logLine("Set Cover Image");
$cover = $content_start . "<h1>Test Book</h1>\n<h2>By: John Doe Johnson</h2>\n" . $bookEnd;
$book->addChapter("Notices", "Cover.html", $cover);
$book->buildTOC(NULL, "toc", "Table of Contents", TRUE, TRUE);
//    function buildTOC($cssFileName = NULL, $tocCSSClass = "toc", $title = "Table of Contents", $addReferences = TRUE, $addToIndex = FALSE, $tocFileName = "TOC.xhtml") {
$book->addFileToMETAINF("com.apple.ibooks.display-options.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<display_options>\n    <platform name=\"*\">\n        <option name=\"fixed-layout\">true</option>\n        <option name=\"interactive\">true</option>\n        <option name=\"specified-fonts\">true</option>\n    </platform>\n</display_options>");
// add a custom metadata to use on the MetaValue entries.
// Using DublinCore as an example, even if it is automatically included.
$book->addCustomNamespace("dc", "http://purl.org/dc/elements/1.1/");
// StaticData::$namespaces["dc"]);
Exemplo n.º 3
0
$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", "http://JohnJaneDoePublications.com/");
// I hope this is a non existent 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.
$book->setSourceURL("http://JohnJaneDoePublications.com/books/TestBook.html");
$book->isGifImagesEnabled = TRUE;
$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" . "    \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n" . "<html xmlns=\"http://www.w3.org/1999/xhtml\">\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/tests/';
$fileDir = '.';
// setting timezone for time functions used for logging to work properly
date_default_timezone_set('Europe/Berlin');
$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" . "<p><img src='demo/test.svg' alt='Demo SVG 1' /></p>\n" . $bookEnd;
$book->setCoverImage('demo/512x700_1.jpg');
//$book->maxImageWidth = 150;
$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);
$book->finalize();
// 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.