/** * 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; }
/** * Add a cover image to the book. * 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. * * @param string $fileName Filename to use for the image, must be unique for the book. * @param string $imageData Binary image data * @param string $mimetype Image mimetype, such as "image/jpeg" or "image/png". * * @return bool $success */ function setCoverImage($fileName, $imageData = null, $mimetype = null) { if ($this->isFinalized || $this->isCoverImageSet || array_key_exists("CoverPage.xhtml", $this->fileList)) { return false; } if ($imageData == null) { // assume $fileName is the valid file path. if (!file_exists($fileName)) { // Attempt to locate the file using the doc root. $rp = realpath($this->docRoot . "/" . $fileName); if ($rp !== false) { // only assign the docroot path if it actually exists there. $fileName = $rp; } } $image = ImageHelper::getImage($this, $fileName); $imageData = $image['image']; $mimetype = $image['mime']; $fileName = preg_replace('#\\.[^\\.]+$#', "." . $image['ext'], $fileName); } $path = pathinfo($fileName); $imgPath = "images/" . $path["basename"]; if (empty($mimetype) && file_exists($fileName)) { /** @noinspection PhpUnusedLocalVariableInspection */ list($width, $height, $type, $attr) = getimagesize($fileName); $mimetype = image_type_to_mime_type($type); } if (empty($mimetype)) { $ext = strtolower($path['extension']); if ($ext == "jpg") { $ext = "jpeg"; } $mimetype = "image/" . $ext; } if ($this->isEPubVersion2()) { $coverPage = "<?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\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xml:lang=\"en\">\n" . "\t<head>\n" . "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>\n" . $this->getViewportMetaLine() . "\t\t<title>Cover Image</title>\n" . "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"Styles/CoverPage.css\" />\n" . "\t</head>\n" . "\t<body>\n" . "\t\t<div>\n" . "\t\t\t<img src=\"" . $imgPath . "\" alt=\"Cover image\" style=\"height: 100%\"/>\n" . "\t\t</div>\n" . "\t</body>\n" . "</html>\n"; } else { $coverPage = "<?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" . "\t<head>\n" . "\t\t<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n" . $this->getViewportMetaLine() . "\t\t<title>Cover Image</title>\n" . "\t\t<link type=\"text/css\" rel=\"stylesheet\" href=\"Styles/CoverPage.css\" />\n" . "\t</head>\n" . "\t<body>\n" . "\t\t<section epub:type=\"cover\">\n" . "\t\t\t<img src=\"" . $imgPath . "\" alt=\"Cover image\" style=\"height: 100%\"/>\n" . "\t\t</section>\n" . "\t</body>\n" . "</html>\n"; } $coverPageCss = "@page, body, div, img {\n" . "\tpadding: 0pt;\n" . "\tmargin:0pt;\n" . "}\n\nbody {\n" . "\ttext-align: center;\n" . "}\n"; $this->addCSSFile("Styles/CoverPage.css", "CoverPageCss", $coverPageCss); $this->addFile($imgPath, "CoverImage", $imageData, $mimetype); $this->addReferencePage("CoverPage", "CoverPage.xhtml", $coverPage, "cover"); $this->isCoverImageSet = true; return true; }