예제 #1
0
파일: EPub.php 프로젝트: grandt/phpepub
 /**
  * Resolve a media src and determine it's target location and add it to the book.
  *
  * @param string $source           Source link.
  * @param string $internalPath     (referenced) Return value, will be set to the target path and name in the book.
  * @param string $internalSrc      (referenced) Return value, will be set to the target name in the book.
  * @param string $isSourceExternal (referenced) Return value, will be set to TRUE if the image originated from a full URL.
  * @param string $baseDir          Default is "", meaning it is pointing to the document root.
  * @param string $htmlDir          The path to the parent HTML file's directory from the root of the archive.
  *
  * @return bool
  */
 protected function resolveMedia($source, &$internalPath, &$internalSrc, &$isSourceExternal, $baseDir = "", $htmlDir = "")
 {
     if ($this->isFinalized) {
         return false;
     }
     $mediaPath = null;
     $tmpFile = null;
     if (preg_match('#^(http|ftp)s?://#i', $source) == 1) {
         $urlInfo = parse_url($source);
         if (strpos($urlInfo['path'], $baseDir . "/") !== false) {
             $internalSrc = substr($urlInfo['path'], strpos($urlInfo['path'], $baseDir . "/") + strlen($baseDir) + 1);
         }
         $internalPath = $urlInfo["scheme"] . "/" . $urlInfo["host"] . "/" . pathinfo($urlInfo["path"], PATHINFO_DIRNAME);
         $isSourceExternal = true;
         $mediaPath = FileHelper::getFileContents($source, true);
         $tmpFile = $mediaPath;
     } else {
         if (strpos($source, "/") === 0) {
             $internalPath = pathinfo($source, PATHINFO_DIRNAME);
             $mediaPath = $source;
             if (!file_exists($mediaPath)) {
                 $mediaPath = $this->docRoot . $mediaPath;
             }
         } else {
             $internalPath = $htmlDir . "/" . preg_replace('#^[/\\.]+#', '', pathinfo($source, PATHINFO_DIRNAME));
             $mediaPath = $baseDir . "/" . $source;
             if (!file_exists($mediaPath)) {
                 $mediaPath = $this->docRoot . $mediaPath;
             }
         }
     }
     if ($mediaPath !== false) {
         $mime = MimeHelper::getMimeTypeFromExtension(pathinfo($source, PATHINFO_EXTENSION));
         $internalPath = RelativePath::getRelativePath("media/" . $internalPath . "/" . $internalSrc);
         if (!array_key_exists($internalPath, $this->fileList) && $this->addLargeFile($internalPath, "m_" . $internalSrc, $mediaPath, $mime)) {
             $this->fileList[$internalPath] = $source;
         }
         if (isset($tmpFile)) {
             unlink($tmpFile);
         }
         return true;
     }
     return false;
 }
예제 #2
0
 /**
  * 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;
 }
예제 #3
0
 /**
  * Try to determine the mimetype of the file path.
  *
  * @param string $source Path
  *
  * @return string mimetype, or FALSE.
  * @deprecated Use getMimeTypeFromExtension(string $extension) instead.
  */
 public static function getMime($source)
 {
     return MimeHelper::getMimeTypeFromExtension(pathinfo($source, PATHINFO_EXTENSION));
 }