Inheritance: extends PhpOffice\PhpPresentation\Slide\AbstractBackground
Example #1
0
 public function testColor()
 {
     $object = new Image();
     $imagePath = PHPPRESENTATION_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'PhpPresentationLogo.png';
     $numSlide = rand(1, 100);
     $this->assertNull($object->getPath());
     $this->assertEmpty($object->getFilename());
     $this->assertEmpty($object->getExtension());
     $this->assertEquals('background_' . $numSlide . '.', $object->getIndexedFilename($numSlide));
     $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Slide\\Background\\Image', $object->setPath($imagePath));
     $this->assertEquals($imagePath, $object->getPath());
     $this->assertEquals('PhpPresentationLogo.png', $object->getFilename());
     $this->assertEquals('png', $object->getExtension());
     $this->assertEquals('background_' . $numSlide . '.png', $object->getIndexedFilename($numSlide));
     $this->assertInstanceOf('PhpOffice\\PhpPresentation\\Slide\\Background\\Image', $object->setPath(null, false));
     $this->assertNull($object->getPath());
     $this->assertEmpty($object->getFilename());
     $this->assertEmpty($object->getExtension());
     $this->assertEquals('background_' . $numSlide . '.', $object->getIndexedFilename($numSlide));
 }
Example #2
0
 /**
  * Write the background image style
  * @param XMLWriter $objWriter
  * @param Image $oBkgImage
  */
 protected function writeBackgroundStyle(XMLWriter $objWriter, Image $oBkgImage, $numSlide)
 {
     $objWriter->startElement('draw:fill-image');
     $objWriter->writeAttribute('draw:name', 'background_' . $numSlide);
     $objWriter->writeAttribute('xlink:href', 'Pictures/' . str_replace(' ', '_', $oBkgImage->getIndexedFilename($numSlide)));
     $objWriter->writeAttribute('xlink:type', 'simple');
     $objWriter->writeAttribute('xlink:show', 'embed');
     $objWriter->writeAttribute('xlink:actuate', 'onLoad');
     $objWriter->endElement();
 }
 /**
  * Extract style
  * @param \DOMElement $nodeStyle
  */
 protected function loadStyle(\DOMElement $nodeStyle)
 {
     $keyStyle = $nodeStyle->getAttribute('style:name');
     $nodeDrawingPageProps = $this->oXMLReader->getElement('style:drawing-page-properties', $nodeStyle);
     if ($nodeDrawingPageProps) {
         // Read Background Color
         if ($nodeDrawingPageProps->hasAttribute('draw:fill-color') && $nodeDrawingPageProps->getAttribute('draw:fill') == 'solid') {
             $oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Color();
             $oColor = new Color();
             $oColor->setRGB(substr($nodeDrawingPageProps->getAttribute('draw:fill-color'), -6));
             $oBackground->setColor($oColor);
         }
         // Read Background Image
         if ($nodeDrawingPageProps->getAttribute('draw:fill') == 'bitmap' && $nodeDrawingPageProps->hasAttribute('draw:fill-image-name')) {
             $nameStyle = $nodeDrawingPageProps->getAttribute('draw:fill-image-name');
             if (!empty($this->arrayCommonStyles[$nameStyle]) && $this->arrayCommonStyles[$nameStyle]['type'] == 'image' && !empty($this->arrayCommonStyles[$nameStyle]['path'])) {
                 $tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderODPBkg');
                 $contentImg = $this->oZip->getFromName($this->arrayCommonStyles[$nameStyle]['path']);
                 file_put_contents($tmpBkgImg, $contentImg);
                 $oBackground = new Image();
                 $oBackground->setPath($tmpBkgImg);
             }
         }
     }
     $nodeGraphicProps = $this->oXMLReader->getElement('style:graphic-properties', $nodeStyle);
     if ($nodeGraphicProps) {
         // Read Shadow
         if ($nodeGraphicProps->hasAttribute('draw:shadow') && $nodeGraphicProps->getAttribute('draw:shadow') == 'visible') {
             $oShadow = new Shadow();
             $oShadow->setVisible(true);
             if ($nodeGraphicProps->hasAttribute('draw:shadow-color')) {
                 $oShadow->getColor()->setRGB(substr($nodeGraphicProps->getAttribute('draw:shadow-color'), -6));
             }
             if ($nodeGraphicProps->hasAttribute('draw:shadow-opacity')) {
                 $oShadow->setAlpha(100 - (int) substr($nodeGraphicProps->getAttribute('draw:shadow-opacity'), 0, -1));
             }
             if ($nodeGraphicProps->hasAttribute('draw:shadow-offset-x') && $nodeGraphicProps->hasAttribute('draw:shadow-offset-y')) {
                 $offsetX = substr($nodeGraphicProps->getAttribute('draw:shadow-offset-x'), 0, -2);
                 $offsetY = substr($nodeGraphicProps->getAttribute('draw:shadow-offset-y'), 0, -2);
                 $distance = 0;
                 if ($offsetX != 0) {
                     $distance = $offsetX < 0 ? $offsetX * -1 : $offsetX;
                 } elseif ($offsetY != 0) {
                     $distance = $offsetY < 0 ? $offsetY * -1 : $offsetY;
                 }
                 $oShadow->setDirection(rad2deg(atan2($offsetY, $offsetX)));
                 $oShadow->setDistance(CommonDrawing::centimetersToPixels($distance));
             }
         }
     }
     $nodeTextProperties = $this->oXMLReader->getElement('style:text-properties', $nodeStyle);
     if ($nodeTextProperties) {
         $oFont = new Font();
         if ($nodeTextProperties->hasAttribute('fo:color')) {
             $oFont->getColor()->setRGB(substr($nodeTextProperties->getAttribute('fo:color'), -6));
         }
         if ($nodeTextProperties->hasAttribute('fo:font-family')) {
             $oFont->setName($nodeTextProperties->getAttribute('fo:font-family'));
         }
         if ($nodeTextProperties->hasAttribute('fo:font-weight') && $nodeTextProperties->getAttribute('fo:font-weight') == 'bold') {
             $oFont->setBold(true);
         }
         if ($nodeTextProperties->hasAttribute('fo:font-size')) {
             $oFont->setSize(substr($nodeTextProperties->getAttribute('fo:font-size'), 0, -2));
         }
     }
     $nodeParagraphProps = $this->oXMLReader->getElement('style:paragraph-properties', $nodeStyle);
     if ($nodeParagraphProps) {
         $oAlignment = new Alignment();
         if ($nodeParagraphProps->hasAttribute('fo:text-align')) {
             $oAlignment->setHorizontal($nodeParagraphProps->getAttribute('fo:text-align'));
         }
     }
     if ($nodeStyle->nodeName == 'text:list-style') {
         $arrayListStyle = array();
         foreach ($this->oXMLReader->getElements('text:list-level-style-bullet', $nodeStyle) as $oNodeListLevel) {
             $oAlignment = new Alignment();
             $oBullet = new Bullet();
             $oBullet->setBulletType(Bullet::TYPE_NONE);
             if ($oNodeListLevel->hasAttribute('text:level')) {
                 $oAlignment->setLevel((int) $oNodeListLevel->getAttribute('text:level') - 1);
             }
             if ($oNodeListLevel->hasAttribute('text:bullet-char')) {
                 $oBullet->setBulletChar($oNodeListLevel->getAttribute('text:bullet-char'));
                 $oBullet->setBulletType(Bullet::TYPE_BULLET);
             }
             $oNodeListProperties = $this->oXMLReader->getElement('style:list-level-properties', $oNodeListLevel);
             if ($oNodeListProperties) {
                 if ($oNodeListProperties->hasAttribute('text:min-label-width')) {
                     $oAlignment->setIndent((int) round(CommonDrawing::centimetersToPixels(substr($oNodeListProperties->getAttribute('text:min-label-width'), 0, -2))));
                 }
                 if ($oNodeListProperties->hasAttribute('text:space-before')) {
                     $iSpaceBefore = CommonDrawing::centimetersToPixels(substr($oNodeListProperties->getAttribute('text:space-before'), 0, -2));
                     $iMarginLeft = $iSpaceBefore + $oAlignment->getIndent();
                     $oAlignment->setMarginLeft($iMarginLeft);
                 }
             }
             $oNodeTextProperties = $this->oXMLReader->getElement('style:text-properties', $oNodeListLevel);
             if ($oNodeTextProperties) {
                 if ($oNodeTextProperties->hasAttribute('fo:font-family')) {
                     $oBullet->setBulletFont($oNodeTextProperties->getAttribute('fo:font-family'));
                 }
             }
             $arrayListStyle[$oAlignment->getLevel()] = array('alignment' => $oAlignment, 'bullet' => $oBullet);
         }
     }
     $this->arrayStyles[$keyStyle] = array('alignment' => isset($oAlignment) ? $oAlignment : null, 'background' => isset($oBackground) ? $oBackground : null, 'font' => isset($oFont) ? $oFont : null, 'shadow' => isset($oShadow) ? $oShadow : null, 'listStyle' => isset($arrayListStyle) ? $arrayListStyle : null);
     return true;
 }
use PhpOffice\PhpPresentation\Style\Color as StyleColor;
use PhpOffice\PhpPresentation\Slide\Background\Image;
// Create new PHPPresentation object
echo date('H:i:s') . ' Create new PHPPresentation object' . EOL;
$objPHPPresentation = new PhpPresentation();
// Create slide
echo date('H:i:s') . ' Create slide' . EOL;
$oSlide1 = $objPHPPresentation->getActiveSlide();
$oSlide1->addShape(clone $oShapeDrawing);
$oSlide1->addShape(clone $oShapeRichText);
// Slide > Background > Color
$oBkgColor = new Color();
$oBkgColor->setColor(new StyleColor(StyleColor::COLOR_DARKGREEN));
$oSlide1->setBackground($oBkgColor);
// Create slide
echo date('H:i:s') . ' Create slide' . EOL;
$oSlide2 = $objPHPPresentation->createSlide();
$oSlide2->addShape(clone $oShapeDrawing);
$oSlide2->addShape(clone $oShapeRichText);
// Slide > Background > Image
/*
 * @link : http://publicdomainarchive.com/public-domain-images-cave-red-rocks-light-beam-cavern/
 */
$oBkgImage = new Image();
$oBkgImage->setPath('./resources/background.jpg');
$oSlide2->setBackground($oBkgImage);
// Save file
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
if (!CLI) {
    include_once 'Sample_Footer.php';
}
 public function testSlideBackground()
 {
     $oPhpPresentation = new PhpPresentation();
     $oSlide = $oPhpPresentation->getActiveSlide();
     $oBkgImage = new Image();
     $oBkgImage->setPath(PHPPRESENTATION_TESTS_BASE_DIR . '/resources/images/PhpPresentationLogo.png');
     $oSlide->setBackground($oBkgImage);
     $pres = TestHelperDOCX::getDocument($oPhpPresentation, 'ODPresentation');
     $element = '/manifest:manifest/manifest:file-entry[5]';
     $this->assertTrue($pres->elementExists($element, 'META-INF/manifest.xml'));
     $this->assertEquals('Pictures/' . str_replace(' ', '_', $oBkgImage->getIndexedFilename(0)), $pres->getElementAttribute($element, 'manifest:full-path', 'META-INF/manifest.xml'));
 }
 /**
  * @param XMLReader $xmlReader
  * @param \DOMElement $oElement
  * @param AbstractSlide $oSlide
  */
 private function loadSlideBackground(XMLReader $xmlReader, \DOMElement $oElement, AbstractSlide $oSlide)
 {
     // Background color
     $oElementColor = $xmlReader->getElement('p:bgPr/a:solidFill/a:srgbClr', $oElement);
     if ($oElementColor) {
         // Color
         $oColor = new Color();
         $oColor->setRGB($oElementColor->hasAttribute('val') ? $oElementColor->getAttribute('val') : null);
         // Background
         $oBackground = new \PhpOffice\PhpPresentation\Slide\Background\Color();
         $oBackground->setColor($oColor);
         // Slide Background
         $oSlide->setBackground($oBackground);
     }
     // Background scheme color
     $oElementSchemeColor = $xmlReader->getElement('p:bgRef/a:schemeClr', $oElement);
     if ($oElementSchemeColor) {
         // Color
         $oColor = new SchemeColor();
         $oColor->setValue($oElementSchemeColor->hasAttribute('val') ? $oElementSchemeColor->getAttribute('val') : null);
         // Background
         $oBackground = new \PhpOffice\PhpPresentation\Slide\Background\SchemeColor();
         $oBackground->setSchemeColor($oColor);
         // Slide Background
         $oSlide->setBackground($oBackground);
     }
     // Background image
     $oElementImage = $xmlReader->getElement('p:bgPr/a:blipFill/a:blip', $oElement);
     if ($oElementImage) {
         $relImg = $this->arrayRels[$oSlide->getRelsIndex()][$oElementImage->getAttribute('r:embed')];
         if (is_array($relImg)) {
             // File
             $pathImage = 'ppt/slides/' . $relImg['Target'];
             $pathImage = explode('/', $pathImage);
             foreach ($pathImage as $key => $partPath) {
                 if ($partPath == '..') {
                     unset($pathImage[$key - 1]);
                     unset($pathImage[$key]);
                 }
             }
             $pathImage = implode('/', $pathImage);
             $contentImg = $this->oZip->getFromName($pathImage);
             $tmpBkgImg = tempnam(sys_get_temp_dir(), 'PhpPresentationReaderPpt2007Bkg');
             file_put_contents($tmpBkgImg, $contentImg);
             // Background
             $oBackground = new Image();
             $oBackground->setPath($tmpBkgImg);
             // Slide Background
             $oSlide->setBackground($oBackground);
         }
     }
 }