/** * Build the Table of Contents. This is not strictly necessary, as most eReaders will build it from the navigation structure in the .ncx file. * * @param string $cssFileName Include a link to this css file in the TOC html. * @param string $tocCSSClass The TOC is a <div>, if you need special formatting, you can add a css class for that div. Default is "toc". * @param string $title Title of the Table of contents. Default is "Table of Contents". Use this for ie. languages other than English. * @param bool $addReferences include reference pages in the TOC, using the $referencesOrder array to determine the order of the pages in the TOC. Default is TRUE. * @param bool $addToIndex Add the TOC to the NCX index at the current leve/position. Default is FALSE * @param string $tocFileName Change the default name of the TOC file. The default is "TOC.xhtml" * * @return bool */ function buildTOC($cssFileName = null, $tocCSSClass = "toc", $title = "Table of Contents", $addReferences = true, $addToIndex = false, $tocFileName = "TOC.xhtml") { if ($this->isFinalized) { return false; } $this->buildTOC = true; $this->tocTitle = $title; $this->tocFileName = FileHelper::normalizeFileName($tocFileName); if (!empty($cssFileName)) { $this->tocCssFileName = FileHelper::normalizeFileName($cssFileName); } $this->tocCSSClass = $tocCSSClass; $this->tocAddReferences = $addReferences; $this->opf->addReference(Reference::TABLE_OF_CONTENTS, $title, $this->tocFileName); if (!($this->tocNavAdded = true)) { $this->opf->addItemRef("ref_" . Reference::TABLE_OF_CONTENTS, false); if ($addToIndex) { $navPoint = new NavPoint(StringHelper::decodeHtmlEntities($title), $this->tocFileName, "ref_" . Reference::TABLE_OF_CONTENTS); $this->ncx->addNavPoint($navPoint); } else { $this->ncx->referencesList[Reference::TABLE_OF_CONTENTS] = $this->tocFileName; $this->ncx->referencesName[Reference::TABLE_OF_CONTENTS] = $title; } } return true; }
/** * Get an image from a file or url, return it resized if the image exceeds the $maxImageWidth or $maxImageHeight directives. * * The return value is an array. * ['width'] is the width of the image. * ['height'] is the height of the image. * ['mime'] is the mime type of the image. Resized images are always in jpeg format. * ['image'] is the image data. * ['ext'] is the extension of the image file. * * @param EPub $book * @param string $imageSource path or url to file. * * @return array|bool * @throws \Exception */ public static function getImage($book, $imageSource) { $width = -1; $height = -1; $mime = "application/octet-stream"; $ext = ""; $image = FileHelper::getFileContents($imageSource); $ratio = 1; if ($image !== false && strlen($image) > 0) { if (BinStringStatic::startsWith(trim($image), '<svg') || (BinStringStatic::startsWith(trim($image), '<?xml') || strpos($image, '<svg') > 0)) { // SVG image. $xml = simplexml_load_string($image); $attr = $xml->attributes(); $meta = ImageHelper::handleSVGAttribs($xml); $mime = "image/svg+xml"; $ext = "svg"; $width = $meta['width']; $height = $meta['height']; $ratio = ImageHelper::getImageScale($width, $height, $book->maxImageWidth, $book->maxImageHeight); if ($ratio < 1) { $attr->width = $width * $ratio; $attr->height = $height * $ratio; } $image = $xml->asXML(); } else { $imageFile = imagecreatefromstring($image); if ($imageFile !== false) { $width = ImageSX($imageFile); $height = ImageSY($imageFile); } if (self::isGdInstalled()) { @($type = exif_imagetype($imageSource)); $mime = image_type_to_mime_type($type); } if ($mime === "application/octet-stream") { $mime = ImageHelper::getImageFileTypeFromBinary($image); } if ($mime === "application/octet-stream") { $mime = MimeHelper::getMimeTypeFromUrl($imageSource); } } } else { return false; } if ($width <= 0 || $height <= 0) { return false; } if ($mime !== "image/svg+xml" && self::isGdInstalled()) { $ratio = ImageHelper::getImageScale($width, $height, $book->maxImageWidth, $book->maxImageHeight); if ($ratio < 1 || empty($mime)) { if ($mime == "image/png" || $book->isGifImagesEnabled === false && $mime == "image/gif") { $image_o = imagecreatefromstring($image); $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio); imagealphablending($image_p, false); imagesavealpha($image_p, true); imagealphablending($image_o, true); imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height); ob_start(); imagepng($image_p, null, 9); $image = ob_get_contents(); ob_end_clean(); imagedestroy($image_o); imagedestroy($image_p); $ext = "png"; } else { if ($book->isGifImagesEnabled !== false && $mime == "image/gif") { $tFileD = tempnam("BewareOfGeeksBearingGifs", "grD"); ResizeGif::ResizeByRatio($imageSource, $tFileD, $ratio); $image = file_get_contents($tFileD); unlink($tFileD); } else { $image_o = imagecreatefromstring($image); $image_p = imagecreatetruecolor($width * $ratio, $height * $ratio); imagecopyresampled($image_p, $image_o, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height); ob_start(); imagejpeg($image_p, null, 80); $image = ob_get_contents(); ob_end_clean(); imagedestroy($image_o); imagedestroy($image_p); $mime = "image/jpeg"; $ext = "jpg"; } } } } if ($ext === "") { static $mimeToExt = array('image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/png' => 'png', 'image/svg+xml' => "svg"); if (isset($mimeToExt[$mime])) { $ext = $mimeToExt[$mime]; } } $rv = array(); $rv['width'] = $width * $ratio; $rv['height'] = $height * $ratio; $rv['mime'] = $mime; $rv['image'] = $image; $rv['ext'] = $ext; return $rv; }
/** * Get file contents, using curl if available, else file_get_contents * * @param string $source * @param bool $toTempFile * * @return bool|mixed|null|string */ public static function getFileContents($source, $toTempFile = false) { $isExternal = preg_match('#^(http|ftp)s?://#i', $source) == 1; if ($isExternal && FileHelper::getIsCurlInstalled()) { $ch = curl_init(); $outFile = null; $fp = null; curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL, str_replace(" ", "%20", $source)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects curl_setopt($ch, CURLOPT_ENCODING, ""); // handle all encodings curl_setopt($ch, CURLOPT_USERAGENT, "EPub (Version " . EPub::VERSION . ") by A. Grandt"); // who am i curl_setopt($ch, CURLOPT_AUTOREFERER, true); // set referer on redirect curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); // timeout on connect curl_setopt($ch, CURLOPT_TIMEOUT, 120); // timeout on response curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // stop after 10 redirects curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Disabled SSL Cert checks if ($toTempFile) { $outFile = tempnam(sys_get_temp_dir(), "EPub_v" . EPub::VERSION . "_"); $fp = fopen($outFile, "w+b"); curl_setopt($ch, CURLOPT_FILE, $fp); $res = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); fclose($fp); } else { $res = curl_exec($ch); $info = curl_getinfo($ch); curl_close($ch); } if ($info['http_code'] == 200 && $res != false) { if ($toTempFile) { return $outFile; } return $res; } return false; } if (FileHelper::getIsFileGetContentsInstalled() && (!$isExternal || FileHelper::getIsFileGetContentsExtInstalled())) { @($data = file_get_contents($source)); return $data; } return false; }