Skip to content

Commit

Permalink
fixes and added Viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
Grandt committed Feb 21, 2016
1 parent b7801ee commit ba251e0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
39 changes: 32 additions & 7 deletions src/PHPePub/Core/EPub.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPePub\Core\Structure\OPF\Reference;
use PHPePub\Helpers\FileHelper;
use PHPePub\Helpers\ImageHelper;
use PHPePub\Helpers\MimeHelper;
use PHPePub\Helpers\StringHelper;
use PHPePub\Helpers\URLHelper;
use PHPZip\Zip\File\Zip;
Expand Down Expand Up @@ -55,13 +56,26 @@ class EPub {
const BOOK_VERSION_EPUB2 = '2.0';
const BOOK_VERSION_EPUB3 = '3.0';

public $viewportMap = array(
"small" => array('width' => 600, 'height' => 800),
"medium" => array('width' => 720, 'height' => 1280),
"720p" => array('width' => 720, 'height' => 1280),
"ipad" => array('width' => 768, 'height' => 1024),
"large" => array('width' => 1080, 'height' => 1920),
"2k" => array('width' => 1080, 'height' => 1920),
"1080p" => array('width' => 1080, 'height' => 1920),
"ipad3" => array('width' => 1536, 'height' => 2048),
"4k" => array('width' => 2160, 'height' => 3840)
);

public $splitDefaultSize = 250000;
/** Gifs can crash some early ADE based readers, and are disabled by default.
* getImage will convert these if it can, unless this is set to TRUE.
*/

public $maxImageWidth = 768;
public $maxImageHeight = 1024;
/**
* Gifs can crash some early ADE based readers, and are disabled by default.
* getImage will convert these if it can, unless this is set to TRUE.
*/
public $isGifImagesEnabled = false;

public $isReferencesAddedToToc = true;
Expand Down Expand Up @@ -229,6 +243,7 @@ function addChapter($chapterName, $fileName, $chapterData = null, $autoSplit = f
$chapter = $chapterData;
if ($autoSplit && is_string($chapterData) && mb_strlen($chapterData) > $this->splitDefaultSize) {
$splitter = new EPubChapterSplitter();
$splitter->setSplitSize($this->splitDefaultSize);

$chapterArray = $splitter->splitChapter($chapterData);
if (count($chapterArray) > 1) {
Expand All @@ -255,13 +270,16 @@ function addChapter($chapterName, $fileName, $chapterData = null, $autoSplit = f
$this->ncx->addNavPoint($navPoint);
$this->ncx->chapterList[$chapterName] = $navPoint;
} elseif (is_array($chapter)) {
$this->log->logLine("addChapter: \$chapterName: $chapterName ; \$fileName: $fileName ; ");
$fileNameParts = pathinfo($fileName);
$extension = $fileNameParts['extension'];
$name = $fileNameParts['filename'];

$partCount = 0;
$this->chapterCount++;

$this->log->logLine("addChapter: \$chapterCount: " . $this->chapterCount);

$oneChapter = each($chapter);
while ($oneChapter) {
/** @noinspection PhpUnusedLocalVariableInspection */
Expand Down Expand Up @@ -865,7 +883,7 @@ protected function resolveMedia($source, &$internalPath, &$internalSrc, &$isSour
}

if ($mediaPath !== false) {
$mime = $this->getMimeTypeFromExtension(pathinfo($source, PATHINFO_EXTENSION));
$mime = MimeHelper::getMimeTypeFromExtension(pathinfo($source, PATHINFO_EXTENSION));
$internalPath = RelativePath::getRelativePath("media/" . $internalPath . "/" . $internalSrc);

if (!array_key_exists($internalPath, $this->fileList) &&
Expand Down Expand Up @@ -1261,7 +1279,7 @@ function addReferencePage($pageName, $fileName, $pageData, $reference, $external
$this->addFile($fileName, "ref_" . $reference, $pageData, "application/xhtml+xml");

if ($reference !== Reference::TABLE_OF_CONTENTS || !isset($this->ncx->referencesList[$reference])) {
$this->opf->addItemRef("ref_" . $reference, false);
$this->opf->addItemRef("ref_" . $reference); //, false);
$this->opf->addReference($reference, $pageName, $fileName);

$this->ncx->referencesList[$reference] = $fileName;
Expand Down Expand Up @@ -1842,7 +1860,7 @@ function buildTOC($cssFileName = null, $tocCSSClass = "toc", $title = "Table of
$this->tocAddReferences = $addReferences;

$this->opf->addReference(Reference::TABLE_OF_CONTENTS, $title, $this->tocFileName);
if (!$this->tocNavAdded = true) {
if (!$this->tocNavAdded) {
$this->opf->addItemRef("ref_" . Reference::TABLE_OF_CONTENTS, false);

if ($addToIndex) {
Expand Down Expand Up @@ -2229,13 +2247,20 @@ function getLog() {
* Viewport is used for fixed-layout books, specifically ePub 3 books using the Rendition metadata.
* Calling this function without arguments clears the viewport.
*
* @param int $width
* The predefined viewports can be accessed with $this->viewportMap
*
* @param int|string $width integer for the width, or a string referencing an entry in the $viewportMap.
* @param int $height
*/
public function setViewport($width = null, $height = null) {
if ($width == null) {
unset($this->viewport);
}
if (is_string($width) && in_array($width, $this->viewportMap)) {
$vp = $this->viewportMap[$width];
$width = $vp['width'];
$height = $vp['height'];
}
$this->viewport = array('width' => $width, 'height' => $height);
}

Expand Down
19 changes: 14 additions & 5 deletions src/PHPePub/Core/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,24 @@ function dumpInstalledModules() {
$isFileGetContentsInstalled = function_exists('file_get_contents');
$isFileGetContentsExtInstalled = $isFileGetContentsInstalled && ini_get('allow_url_fopen');

$this->logLine("isCurlInstalled...............: " . ($isCurlInstalled ? "Yes" : "No"));
$this->logLine("isGdInstalled.................: " . ($isGdInstalled ? "Yes" : "No"));
$this->logLine("isExifInstalled...............: " . ($isExifInstalled ? "Yes" : "No"));
$this->logLine("isFileGetContentsInstalled....: " . ($isFileGetContentsInstalled ? "Yes" : "No"));
$this->logLine("isFileGetContentsExtInstalled.: " . ($isFileGetContentsExtInstalled ? "Yes" : "No"));
$this->logLine("isCurlInstalled...............: " . $this->boolYN($isCurlInstalled));
$this->logLine("isGdInstalled.................: " . $this->boolYN($isGdInstalled));
$this->logLine("isExifInstalled...............: " . $this->boolYN($isExifInstalled));
$this->logLine("isFileGetContentsInstalled....: " . $this->boolYN($isFileGetContentsInstalled));
$this->logLine("isFileGetContentsExtInstalled.: " . $this->boolYN($isFileGetContentsExtInstalled));
}
}

function getLog() {
return $this->log;
}

/**
* @param $isCurlInstalled
*
* @return string
*/
public function boolYN($isCurlInstalled) {
return ($isCurlInstalled ? "Yes" : "No");
}
}
8 changes: 0 additions & 8 deletions src/PHPePub/Core/Structure/OPF/DublinCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ class DublinCore extends MetaValue {
const SUBJECT = "subject";
const TITLE = "title";
const TYPE = "type";
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $value;

/**
* Class constructor.
Expand Down
6 changes: 2 additions & 4 deletions tests/EPub.Example3.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@

use PHPePub\Core\EPub;
use PHPePub\Core\Logger;
use PHPePub\Core\StaticData;
use PHPePub\Core\Structure\OPF\DublinCore;
use PHPePub\Helpers\CalibreHelper;
use PHPePub\Helpers\iBooks\IBooksHelper;
use PHPePub\Helpers\iBooks\Orientation;
use PHPePub\Helpers\IBooksHelper;
use PHPePub\Helpers\Rendition\RenditionHelper;
use PHPePub\Helpers\URLHelper;
use PHPZip\Zip\File\Zip;
Expand Down Expand Up @@ -61,7 +59,7 @@
// Setting rendition parameters for fixed layout requires the user to add a viewport to each html file.
// It is up to the user to do this, however the cover image and toc files are generated by the EPub class, and need the information.
// It can be set multiple times if different viewports are needed for the cover image page and toc.
$book->setViewport(720, 1280);
$book->setViewport("720p");

IBooksHelper::addPrefix($book);
IBooksHelper::setIPadOrientationLock($book, IBooksHelper::ORIENTATION_PORTRAIT_ONLY);
Expand Down

0 comments on commit ba251e0

Please sign in to comment.