Update to latest release of epub.php

This commit is contained in:
Sébastien Lucas 2013-01-31 21:25:23 +01:00
parent d1ce53087f
commit d84ea868a8

View file

@ -15,6 +15,7 @@ class EPub {
protected $file; protected $file;
protected $meta; protected $meta;
protected $zip; protected $zip;
protected $coverpath='';
protected $namespaces; protected $namespaces;
protected $imagetoadd=''; protected $imagetoadd='';
@ -85,37 +86,21 @@ class EPub {
* TODO update * TODO update
*/ */
public function save(){ public function save(){
$zip = new ZipArchive(); $this->download ();
$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='';
}
$zip->close(); $zip->close();
} }
/** /**
* Get the updated epub * Get the updated epub
*/ */
public function download($file){ public function download($file=false){
$this->zip->FileReplace($this->meta,$this->xml->saveXML()); $this->zip->FileReplace($this->meta,$this->xml->saveXML());
// add the cover image // add the cover image
if($this->imagetoadd){ if($this->imagetoadd){
$path = dirname('/'.$this->meta).'/php-epub-meta-cover.img'; // image path is relative to meta file $this->zip->FileReplace($this->coverpath,file_get_contents($this->imagetoadd));
$path = ltrim($path,'/');
$this->zip->FileReplace($path,file_get_contents($this->imagetoadd));
$this->imagetoadd=''; $this->imagetoadd='';
} }
$this->zip->Flush(TBSZIP_DOWNLOAD, $file); if ($file) $this->zip->Flush(TBSZIP_DOWNLOAD, $file);
} }
@ -408,6 +393,61 @@ class EPub {
); );
} }
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 * A simple getter/setter for simple meta attributes
* *