コード例 #1
0
ファイル: Template.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Create a new Template Object
  *
  * @param string $strFilename
  * @throws \PhpOffice\PhpWord\Exception\Exception
  */
 public function __construct($strFilename)
 {
     $this->tempFileName = tempnam(sys_get_temp_dir(), '');
     if ($this->tempFileName === false) {
         throw new Exception('Could not create temporary file with unique name in the default temporary directory.');
     }
     // Copy the source File to the temp File
     if (!copy($strFilename, $this->tempFileName)) {
         throw new Exception("Could not copy the template from {$strFilename} to {$this->tempFileName}.");
     }
     $zipClass = Settings::getZipClass();
     $this->zipClass = new $zipClass();
     $this->zipClass->open($this->tempFileName);
     // Find and load headers and footers
     $index = 1;
     while ($this->zipClass->locateName($this->getHeaderName($index)) !== false) {
         $this->headerXMLs[$index] = $this->zipClass->getFromName($this->getHeaderName($index));
         $index++;
     }
     $index = 1;
     while ($this->zipClass->locateName($this->getFooterName($index)) !== false) {
         $this->footerXMLs[$index] = $this->zipClass->getFromName($this->getFooterName($index));
         $index++;
     }
     $this->documentXML = $this->zipClass->getFromName('word/document.xml');
 }
コード例 #2
0
ファイル: ZipArchive.php プロジェクト: hcvcastro/pxp
 /**
  * Create new instance
  */
 public function __construct()
 {
     $this->usePclzip = Settings::getZipClass() != 'ZipArchive';
     if ($this->usePclzip) {
         if (!defined('PCLZIP_TEMPORARY_DIR')) {
             define('PCLZIP_TEMPORARY_DIR', sys_get_temp_dir() . '/');
         }
         require_once 'PCLZip/pclzip.lib.php';
     }
 }
コード例 #3
0
ファイル: Image.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Get Base64 image data
  *
  * @return string|null
  */
 private function getBase64ImageData(ImageElement $element)
 {
     $source = $element->getSource();
     $imageType = $element->getImageType();
     $imageData = null;
     $imageBinary = null;
     $actualSource = null;
     // Get actual source from archive image or other source
     // Return null if not found
     if ($element->getSourceType() == ImageElement::SOURCE_ARCHIVE) {
         $source = substr($source, 6);
         list($zipFilename, $imageFilename) = explode('#', $source);
         $zipClass = \PhpOffice\PhpWord\Settings::getZipClass();
         $zip = new $zipClass();
         if ($zip->open($zipFilename) !== false) {
             if ($zip->locateName($imageFilename)) {
                 $zip->extractTo($this->parentWriter->getTempDir(), $imageFilename);
                 $actualSource = $this->parentWriter->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
             }
         }
         $zip->close();
     } else {
         $actualSource = $source;
     }
     if (is_null($actualSource)) {
         return null;
     }
     // Read image binary data and convert into Base64
     if ($element->getSourceType() == ImageElement::SOURCE_GD) {
         $imageResource = call_user_func($element->getImageCreateFunction(), $actualSource);
         ob_start();
         call_user_func($element->getImageFunction(), $imageResource);
         $imageBinary = ob_get_contents();
         ob_end_clean();
     } else {
         if ($fileHandle = fopen($actualSource, 'rb', false)) {
             $imageBinary = fread($fileHandle, filesize($actualSource));
             fclose($fileHandle);
         }
     }
     if (!is_null($imageBinary)) {
         $base64 = chunk_split(base64_encode($imageBinary));
         $imageData = 'data:' . $imageType . ';base64,' . $base64;
     }
     return $imageData;
 }
コード例 #4
0
ファイル: XMLReader.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Get DOMDocument from ZipArchive
  *
  * @param string $zipFile
  * @param string $xmlFile
  * @return \DOMDocument|false
  */
 public function getDomFromZip($zipFile, $xmlFile)
 {
     if (file_exists($zipFile) === false) {
         throw new Exception('Cannot find archive file.');
     }
     $zipClass = Settings::getZipClass();
     $zip = new $zipClass();
     $canOpen = $zip->open($zipFile);
     if ($canOpen === false) {
         throw new Exception('Cannot open archive file.');
     }
     $contents = $zip->getFromName($xmlFile);
     $zip->close();
     if ($contents === false) {
         return false;
     } else {
         $this->dom = new \DOMDocument();
         $this->dom->loadXML($contents);
         return $this->dom;
     }
 }
コード例 #5
0
ファイル: Word2007.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Read all relationship files
  *
  * @param string $docFile
  * @return array
  */
 private function readRelationships($docFile)
 {
     $relationships = array();
     // _rels/.rels
     $relationships['main'] = $this->getRels($docFile, '_rels/.rels');
     // word/_rels/*.xml.rels
     $wordRelsPath = 'word/_rels/';
     $zipClass = Settings::getZipClass();
     $zip = new $zipClass();
     if ($zip->open($docFile) === true) {
         for ($i = 0; $i < $zip->numFiles; $i++) {
             $xmlFile = $zip->getNameIndex($i);
             if (substr($xmlFile, 0, strlen($wordRelsPath)) == $wordRelsPath && substr($xmlFile, -1) != '/') {
                 $docPart = str_replace('.xml.rels', '', str_replace($wordRelsPath, '', $xmlFile));
                 $relationships[$docPart] = $this->getRels($docFile, $xmlFile, 'word/');
             }
         }
         $zip->close();
     }
     return $relationships;
 }
コード例 #6
0
ファイル: SettingsTest.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Test set/get zip class
  */
 public function testSetGetZipClass()
 {
     $this->assertEquals(Settings::ZIPARCHIVE, Settings::getZipClass());
     $this->assertTrue(Settings::setZipClass(Settings::PCLZIP));
     $this->assertFalse(Settings::setZipClass('foo'));
 }
コード例 #7
0
ファイル: Image.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Get image size from archive
  *
  * @param string $source
  * @return array|null
  */
 private function getArchiveImageSize($source)
 {
     $imageData = null;
     $source = substr($source, 6);
     list($zipFilename, $imageFilename) = explode('#', $source);
     $tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
     $zipClass = Settings::getZipClass();
     $zip = new $zipClass();
     if ($zip->open($zipFilename) !== false) {
         if ($zip->locateName($imageFilename)) {
             $imageContent = $zip->getFromName($imageFilename);
             if ($imageContent !== false) {
                 file_put_contents($tempFilename, $imageContent);
                 $imageData = @getimagesize($tempFilename);
                 unlink($tempFilename);
             }
         }
         $zip->close();
     }
     return $imageData;
 }
コード例 #8
0
ファイル: AbstractWriter.php プロジェクト: kaantunc/MYK-BOR
 /**
  * Add file to package
  *
  * Get the actual source from an archive image
  *
  * @param mixed $objZip
  * @param string $source
  * @param string $target
  */
 protected function addFileToPackage($objZip, $source, $target)
 {
     $isArchive = strpos($source, 'zip://') !== false;
     $actualSource = null;
     if ($isArchive) {
         $source = substr($source, 6);
         list($zipFilename, $imageFilename) = explode('#', $source);
         $zipClass = \PhpOffice\PhpWord\Settings::getZipClass();
         $zip = new $zipClass();
         if ($zip->open($zipFilename) !== false) {
             if ($zip->locateName($imageFilename)) {
                 $zip->extractTo($this->getTempDir(), $imageFilename);
                 $actualSource = $this->getTempDir() . DIRECTORY_SEPARATOR . $imageFilename;
             }
         }
         $zip->close();
     } else {
         $actualSource = $source;
     }
     if (!is_null($actualSource)) {
         $objZip->addFile($actualSource, $target);
     }
 }