Example #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;
 }
Example #2
0
 /**
  * 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());
 }
Example #3
0
 /**
  * Add image element
  *
  * @param string $src
  * @param mixed $style Image style
  * @param boolean $isWatermark
  * @return Image
  */
 public function addImage($src, $style = null, $isWatermark = false)
 {
     $this->checkValidity('image');
     $elementDocPart = $this->checkElementDocPart();
     $image = new Image($src, $style, $isWatermark);
     $image->setDocPart($this->getDocPart(), $this->getDocPartId());
     $rId = Media::addElement($elementDocPart, 'image', $src, $image);
     $image->setRelationId($rId);
     $this->addElement($image);
     return $image;
 }
Example #4
-1
 /**
  * Write watermark element
  */
 private function writeWatermark(XMLWriter $xmlWriter, ImageElement $element)
 {
     $rId = $element->getRelationId();
     $style = $element->getStyle();
     $style->setPositioning('absolute');
     $styleWriter = new ImageStyleWriter($xmlWriter, $style);
     $xmlWriter->startElement('w:p');
     $xmlWriter->startElement('w:r');
     $xmlWriter->startElement('w:pict');
     $xmlWriter->startElement('v:shape');
     $xmlWriter->writeAttribute('type', '#_x0000_t75');
     $styleWriter->write();
     $xmlWriter->startElement('v:imagedata');
     $xmlWriter->writeAttribute('r:id', 'rId' . $rId);
     $xmlWriter->writeAttribute('o:title', '');
     $xmlWriter->endElement();
     // v:imagedata
     $xmlWriter->endElement();
     // v:shape
     $xmlWriter->endElement();
     // w:pict
     $xmlWriter->endElement();
     // w:r
     $xmlWriter->endElement();
     // w:p
 }