Exemple #1
0
 /**
  * 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;
 }
 /**
  * Test archived image
  */
 public function testArchivedImage()
 {
     $archiveFile = __DIR__ . '/../_files/documents/reader.docx';
     $imageFile = 'word/media/image1.jpeg';
     $image = new Image("zip://{$archiveFile}#{$imageFile}");
     $this->assertEquals('image/jpeg', $image->getImageType());
 }