getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { return; } $this->writeStyle($style); } /** * Write style attribute */ protected function writeStyle(ImageStyle $style) { $xmlWriter = $this->getXmlWriter(); // Default style array $styleArray = array( 'mso-width-percent' => '0', 'mso-height-percent' => '0', 'mso-width-relative' => 'margin', 'mso-height-relative' => 'margin', ); $styleArray = array_merge($styleArray, $this->getElementStyle($style)); // Absolute/relative positioning $positioning = $style->getPositioning(); $styleArray['position'] = $positioning; if ($positioning == ImageStyle::POSITION_ABSOLUTE) { $styleArray['mso-position-horizontal-relative'] = 'page'; $styleArray['mso-position-vertical-relative'] = 'page'; } elseif ($positioning == ImageStyle::POSITION_RELATIVE) { $styleArray['mso-position-horizontal'] = $style->getPosHorizontal(); $styleArray['mso-position-vertical'] = $style->getPosVertical(); $styleArray['mso-position-horizontal-relative'] = $style->getPosHorizontalRel(); $styleArray['mso-position-vertical-relative'] = $style->getPosVerticalRel(); $styleArray['margin-left'] = 0; $styleArray['margin-top'] = 0; } // Wrapping style $wrapping = $style->getWrappingStyle(); if ($wrapping == ImageStyle::WRAPPING_STYLE_INLINE) { // Nothing to do when inline } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_BEHIND) { $styleArray['z-index'] = -251658752; } else { $styleArray['z-index'] = 251659264; $styleArray['mso-position-horizontal'] = 'absolute'; $styleArray['mso-position-vertical'] = 'absolute'; } // w10 wrapping if ($wrapping == ImageStyle::WRAPPING_STYLE_SQUARE) { $this->w10wrap = 'square'; } elseif ($wrapping == ImageStyle::WRAPPING_STYLE_TIGHT) { $this->w10wrap = 'tight'; } $imageStyle = $this->assembleStyle($styleArray); $xmlWriter->writeAttribute('style', $imageStyle); } /** * Write alignment */ public function writeAlignment() { $style = $this->getStyle(); if (!$style instanceof \PhpOffice\PhpWord\Style\Image) { return; } $xmlWriter = $this->getXmlWriter(); $xmlWriter->startElement('w:pPr'); $styleWriter = new Alignment($xmlWriter, new AlignmentStyle(array('value' => $style->getAlign()))); $styleWriter->write(); $xmlWriter->endElement(); // w:pPr } /** * Write w10 wrapping */ public function writeW10Wrap() { if (is_null($this->w10wrap)) { return; } $xmlWriter = $this->getXmlWriter(); $xmlWriter->startElement('w10:wrap'); $xmlWriter->writeAttribute('type', $this->w10wrap); $xmlWriter->endElement(); // w10:wrap } /** * Get element style * * @param \PhpOffice\PhpWord\Style\Image $style * @return array */ private function getElementStyle(ImageStyle $style) { $styles = array(); $styleValues = array( 'width' => $style->getWidth(), 'height' => $style->getHeight(), 'margin-top' => $style->getMarginTop(), 'margin-left' => $style->getMarginLeft() ); foreach ($styleValues as $key => $value) { if (!is_null($value) && $value != '') { $styles[$key] = $value . 'px'; } } return $styles; } /** * Assemble style array into style string * * @param array $styles * @return string */ protected function assembleStyle($styles = array()) { $style = ''; foreach ($styles as $key => $value) { if (!is_null($value) && $value != '') { $style .= "{$key}:{$value}; "; } } return trim($style); } }