/** * Create OPF File. * * @param array $book_contents * @param array $metadata * * @throws \Exception */ protected function createOPF($book_contents, $metadata) { if (empty($this->manifest)) { throw new \Exception('$this->manifest cannot be empty. Did you forget to call $this->createOEPBS() ?'); } // Vars $vars = array('meta' => $metadata, 'manifest' => $this->manifest, 'stylesheet' => $this->stylesheet); // Find all the image files, insert them into the OPF file $html = ''; $path_to_assets = $this->tmpDir . '/OEBPS/assets'; $assets = scandir($path_to_assets); $used_ids = array(); foreach ($assets as $asset) { if ('.' == $asset || '..' == $asset) { continue; } $mimetype = $this->mediaType("{$path_to_assets}/{$asset}"); if ($this->coverImage == $asset) { $file_id = 'cover-image'; } else { $file_id = 'media-' . pathinfo("{$path_to_assets}/{$asset}", PATHINFO_FILENAME); $file_id = Sanitize\sanitize_xml_id($file_id); } // Check if a media id has already been used, if so give it a new one $check_if_used = $file_id; for ($i = 2; $i <= 999; $i++) { if (empty($used_ids[$check_if_used])) { break; } else { $check_if_used = $file_id . "-{$i}"; } } $file_id = $check_if_used; $html .= sprintf('<item id="%s" href="OEBPS/assets/%s" media-type="%s" />', $file_id, $asset, $mimetype) . "\n"; $used_ids[$file_id] = true; } $vars['manifest_assets'] = $html; $vars['do_copyright_license'] = strip_tags($this->doCopyrightLicense($metadata)); // Put contents file_put_contents($this->tmpDir . "/book.opf", $this->loadTemplate($this->dir . '/templates/opf.php', $vars)); }
/** * Find all the image files, insert them into the OPF file * * @return string */ protected function buildManifestAssetsHtml() { $html = ''; $path_to_assets = $this->tmpDir . '/OEBPS/assets'; $assets = scandir($path_to_assets); $used_ids = array(); foreach ($assets as $asset) { if ('.' == $asset || '..' == $asset) { continue; } $mimetype = $this->mediaType("{$path_to_assets}/{$asset}"); if ($this->coverImage == $asset) { $file_id = 'cover-image'; } else { $file_id = 'media-' . pathinfo("{$path_to_assets}/{$asset}", PATHINFO_FILENAME); $file_id = Sanitize\sanitize_xml_id($file_id); } // Check if a media id has already been used, if so give it a new one $check_if_used = $file_id; for ($i = 2; $i <= 999; $i++) { if (empty($used_ids[$check_if_used])) { break; } else { $check_if_used = $file_id . "-{$i}"; } } $file_id = $check_if_used; $html .= sprintf('<item id="%s" href="OEBPS/assets/%s" media-type="%s" />', $file_id, $asset, $mimetype) . "\n"; $used_ids[$file_id] = true; } return $html; }
/** * @param array $book_contents * @param array $metadata */ protected function echoMetaData($book_contents, $metadata) { foreach ($metadata as $name => $content) { $name = Sanitize\sanitize_xml_id(str_replace('_', '-', $name)); $content = trim(strip_tags(html_entity_decode($content))); // Plain text $content = preg_replace('/\\s+/', ' ', preg_replace('/\\n+/', ' ', $content)); // Normalize whitespaces $content = Sanitize\sanitize_xml_attribute($content); printf('<meta name="%s" content="%s" />', $name, $content); echo "\n"; } }
/** * Create OPF File. * * @param array $book_contents * @param array $metadata * * @throws \Exception */ protected function createOPF($book_contents, $metadata) { if (empty($this->manifest)) { throw new \Exception('$this->manifest cannot be empty. Did you forget to call $this->createOEPBS() ?'); } $vars = array('meta' => $metadata, 'manifest' => $this->manifest, 'stylesheet' => $this->stylesheet); // Find all the image files, insert them into the OPF file $html = ''; $path_to_assets = $this->tmpDir . '/OEBPS/assets'; $assets = scandir($path_to_assets); $used_ids = array(); foreach ($assets as $asset) { if ('.' == $asset || '..' == $asset) { continue; } $mimetype = $this->mediaType("{$path_to_assets}/{$asset}"); if ($this->coverImage == $asset) { $file_id = 'cover-image'; } else { $file_id = 'media-' . pathinfo("{$path_to_assets}/{$asset}", PATHINFO_FILENAME); $file_id = Sanitize\sanitize_xml_id($file_id); } // Check if a media id has already been used, if so give it a new one $check_if_used = $file_id; for ($i = 2; $i <= 999; $i++) { if (empty($used_ids[$check_if_used])) { break; } else { $check_if_used = $file_id . "-{$i}"; } } $file_id = $check_if_used; $html .= sprintf('<item id="%s" href="OEBPS/assets/%s" media-type="%s" />', $file_id, $asset, $mimetype) . "\n\t\t"; $used_ids[$file_id] = true; } $vars['manifest_assets'] = $html; //Clear the html buffer for reuse $html = ''; //Loop through the html files for the manifest and assemble them. Assign properties based on their content. foreach ($this->manifest as $k => $v) { $properties = $this->getProperties($this->tmpDir . "/OEBPS/" . $v['filename']); array_key_exists('mathml', $properties) ? $mathml = 'properties="mathml" ' : ($mathml = ''); array_key_exists('scripted', $properties) ? $scripted = 'properties="scripted" ' : ($scripted = ''); $html .= sprintf('<item id="%s" href="OEBPS/%s" %s%smedia-type="application/xhtml+xml" />', $k, $v['filename'], $mathml, $scripted) . "\n\t\t"; } $vars['manifest_filelist'] = $html; $vars['do_copyright_license'] = strip_tags($this->doCopyrightLicense($metadata)); // Put contents file_put_contents($this->tmpDir . "/book.opf", $this->loadTemplate($this->dir . '/templates/opf.php', $vars)); }