From d84ea868a84d791a184798269bd75cf23bf7eaee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Thu, 31 Jan 2013 21:25:23 +0100 Subject: [PATCH] Update to latest release of epub.php --- php-epub-meta/epub.php | 80 +++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/php-epub-meta/epub.php b/php-epub-meta/epub.php index 5d03a59..857d29a 100644 --- a/php-epub-meta/epub.php +++ b/php-epub-meta/epub.php @@ -15,6 +15,7 @@ class EPub { protected $file; protected $meta; protected $zip; + protected $coverpath=''; protected $namespaces; protected $imagetoadd=''; @@ -85,37 +86,21 @@ class EPub { * TODO update */ public function save(){ - $zip = new ZipArchive(); - $res = @$zip->open($this->file, ZipArchive::CREATE); - if($res === false){ - throw new Exception('Failed to write back metadata'); - } - $zip->addFromString($this->meta,$this->xml->saveXML()); - // add the cover image - if($this->imagetoadd){ - $path = dirname('/'.$this->meta).'/php-epub-meta-cover.img'; // image path is relative to meta file - $path = ltrim($path,'/'); - - $zip->addFromString($path,file_get_contents($this->imagetoadd)); - $this->imagetoadd=''; - } + $this->download (); $zip->close(); } /** * Get the updated epub */ - public function download($file){ + public function download($file=false){ $this->zip->FileReplace($this->meta,$this->xml->saveXML()); // add the cover image if($this->imagetoadd){ - $path = dirname('/'.$this->meta).'/php-epub-meta-cover.img'; // image path is relative to meta file - $path = ltrim($path,'/'); - - $this->zip->FileReplace($path,file_get_contents($this->imagetoadd)); + $this->zip->FileReplace($this->coverpath,file_get_contents($this->imagetoadd)); $this->imagetoadd=''; } - $this->zip->Flush(TBSZIP_DOWNLOAD, $file); + if ($file) $this->zip->Flush(TBSZIP_DOWNLOAD, $file); } @@ -407,6 +392,61 @@ class EPub { 'found' => $path ); } + + + public function Cover2($path=false, $mime=false){ + $hascover = true; + $item; + // load cover + $nodes = $this->xpath->query('//opf:metadata/opf:meta[@name="cover"]'); + if(!$nodes->length){ + $hascover = false; + } else{ + $coverid = (String) $nodes->item(0)->attr('opf:content'); + if(!$coverid){ + $hascover = false; + } else{ + $nodes = $this->xpath->query('//opf:manifest/opf:item[@id="'.$coverid.'"]'); + if(!$nodes->length){ + $hascover = false; + } else{ + $item = $nodes->item(0); + $mime = $item->attr('opf:media-type'); + $this->coverpath = $item->attr('opf:href'); + $this->coverpath = dirname('/'.$this->meta).'/'.$this->coverpath; // image path is relative to meta file + $this->coverpath = ltrim($this->coverpath,'/'); + } + } + } + + // set cover + if($path !== false){ + if (!$hascover) return; // TODO For now only update + + if($path){ + $item->attr('opf:media-type',$mime); + + // remember path for save action + $this->imagetoadd = $path; + } + + $this->reparse(); + } + + if (!$hascover) return $this->no_cover(); + + $zip = new ZipArchive(); + if(!@$zip->open($this->file)){ + throw new Exception('Failed to read epub file'); + } + $data = $zip->getFromName($this->coverpath); + + return array( + 'mime' => $mime, + 'data' => $data, + 'found' => $this->coverpath + ); + } /** * A simple getter/setter for simple meta attributes