Skip to content

Commit

Permalink
Merge pull request #404 from Progi1984/issue370
Browse files Browse the repository at this point in the history
#370 : Support for fill for image
  • Loading branch information
Progi1984 authored Sep 8, 2017
2 parents 7bd91a4 + 2c3c6d7 commit e67d8ef
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 51 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

### Changes
- PHP 7.1 is now supported - @Progi1984 GH-355
- PhpOffice\PhpPresentation\Style\Color : Define only the transparency - @Progi1984 GH-370

### Features
- ODPresentation Writer : Support for the position of Legend - @Progi1984 GH-355
- ODPresentation Writer : Support for DoughnutChart - @Progi1984 GH-355
- PowerPoint2007 Writer : Support for DoughnutChart - @Progi1984 GH-355
- ODPresentation Reader : Support for fill for image - @Progi1984 GH-370
- PowerPoint2007 Reader : Support for fill for image - @Progi1984 GH-370
- ODPresentation Writer : Support for fill for transparent image - @Progi1984 GH-370
- PowerPoint2007 Writer : Support for fill for transparent image - @JewrassicPark GH-370

## 0.9.0 - 2017-07-05

Expand Down
28 changes: 15 additions & 13 deletions samples/Sample_03_Image.php

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions samples/Sample_03_Video.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

include_once 'Sample_Header.php';

use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\Drawing;
use PhpOffice\PhpPresentation\Shape\Media;

// 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;
$currentSlide = $objPHPPresentation->getActiveSlide();

// Add a video to the slide
$shape = new Media();
$shape->setName('Video')
->setDescription('Video')
->setPath(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? './resources/sintel_trailer-480p.wmv' : './resources/sintel_trailer-480p.ogv')
->setResizeProportional(false)
->setHeight(90)
->setWidth(90)
->setOffsetX(10)
->setOffsetY(300);
$currentSlide->addShape($shape);

// Save file
echo write($objPHPPresentation, basename(__FILE__, '.php'), $writers);
if (!CLI) {
include_once 'Sample_Footer.php';
}
37 changes: 29 additions & 8 deletions samples/Sample_Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* Header file
*/
use PhpOffice\PhpPresentation\Autoloader;
use PhpOffice\PhpPresentation\Settings;
use PhpOffice\PhpPresentation\IOFactory;
use PhpOffice\PhpPresentation\Slide;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\AbstractShape;
use PhpOffice\PhpPresentation\DocumentLayout;
use PhpOffice\PhpPresentation\Shape\Drawing;
use PhpOffice\PhpPresentation\Shape\Group;
use PhpOffice\PhpPresentation\Shape\RichText;
use PhpOffice\PhpPresentation\Shape\RichText\BreakElement;
use PhpOffice\PhpPresentation\Shape\RichText\TextElement;
Expand Down Expand Up @@ -69,12 +69,16 @@


// Populate samples
$files = '';
$files = array();
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if (preg_match('/^Sample_\d+_/', $file)) {
$name = str_replace('_', ' ', preg_replace('/(Sample_|\.php)/', '', $file));
$files .= "<li><a href='{$file}'>{$name}</a></li>";
$group = substr($name, 0, 1);
if (!isset($files[$group])) {
$files[$group] = '';
}
$files[$group] .= "<li><a href='{$file}'>{$name}</a></li>";
}
}
closedir($handle);
Expand Down Expand Up @@ -248,7 +252,7 @@ protected function displayShape(AbstractShape $shape)
$this->append('<li><span class="shape" id="div'.$shape->getHashCode().'">Shape "Drawing\File"</span></li>');
} elseif($shape instanceof Drawing\Base64) {
$this->append('<li><span class="shape" id="div'.$shape->getHashCode().'">Shape "Drawing\Base64"</span></li>');
} elseif($shape instanceof Drawing\Zip) {
} elseif($shape instanceof Drawing\ZipFile) {
$this->append('<li><span class="shape" id="div'.$shape->getHashCode().'">Shape "Drawing\Zip"</span></li>');
} elseif($shape instanceof RichText) {
$this->append('<li><span class="shape" id="div'.$shape->getHashCode().'">Shape "RichText"</span></li>');
Expand Down Expand Up @@ -334,7 +338,22 @@ protected function displayShapeInfo(AbstractShape $oShape)
$this->append('<dt>Width</dt><dd>'.$oShape->getWidth().'</dd>');
$this->append('<dt>Rotation</dt><dd>'.$oShape->getRotation().'°</dd>');
$this->append('<dt>Hyperlink</dt><dd>'.ucfirst(var_export($oShape->hasHyperlink(), true)).'</dd>');
$this->append('<dt>Fill</dt><dd>@Todo</dd>');
$this->append('<dt>Fill</dt>');
if (is_null($oShape->getFill())) {
$this->append('<dd>None</dd>');
} else {
switch($oShape->getFill()->getFillType()) {
case \PhpOffice\PhpPresentation\Style\Fill::FILL_NONE:
$this->append('<dd>None</dd>');
break;
case \PhpOffice\PhpPresentation\Style\Fill::FILL_SOLID:
$this->append('<dd>Solid (');
$this->append('Color : #'.$oShape->getFill()->getStartColor()->getRGB());
$this->append(' - Alpha : '.$oShape->getFill()->getStartColor()->getAlpha().'%');
$this->append(')</dd>');
break;
}
}
$this->append('<dt>Border</dt><dd>@Todo</dd>');
$this->append('<dt>IsPlaceholder</dt><dd>' . ($oShape->isPlaceholder() ? 'true' : 'false') . '</dd>');
if($oShape instanceof Drawing\Gd) {
Expand All @@ -346,7 +365,7 @@ protected function displayShapeInfo(AbstractShape $oShape)
ob_end_clean();
$this->append('<dt>Mime-Type</dt><dd>'.$oShape->getMimeType().'</dd>');
$this->append('<dt>Image</dt><dd><img src="data:'.$oShape->getMimeType().';base64,'.base64_encode($sShapeImgContents).'"></dd>');
} elseif($oShape instanceof Drawing) {
} elseif($oShape instanceof Drawing\AbstractDrawingAdapter) {
$this->append('<dt>Name</dt><dd>'.$oShape->getName().'</dd>');
$this->append('<dt>Description</dt><dd>'.$oShape->getDescription().'</dd>');
} elseif($oShape instanceof RichText) {
Expand Down Expand Up @@ -456,10 +475,12 @@ protected function getConstantName($class, $search, $startWith = '') {
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<?php foreach ($files as $key => $fileStr) :?>
<li class="dropdown active">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-code fa-lg"></i>&nbsp;Samples<strong class="caret"></strong></a>
<ul class="dropdown-menu"><?php echo $files; ?></ul>
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-code fa-lg"></i>&nbsp;Samples <?php echo $key?>x<strong class="caret"></strong></a>
<ul class="dropdown-menu"><?php echo $fileStr; ?></ul>
</li>
<?php endforeach; ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="https://github.com/PHPOffice/PHPPresentation"><i class="fa fa-github fa-lg" title="GitHub"></i>&nbsp;</a></li>
Expand Down
Binary file added samples/resources/logo_ubuntu_transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/PhpPresentation/Reader/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use PhpOffice\PhpPresentation\Slide\Background\Image;
use PhpOffice\PhpPresentation\Style\Bullet;
use PhpOffice\PhpPresentation\Style\Color;
use PhpOffice\PhpPresentation\Style\Fill;
use PhpOffice\PhpPresentation\Style\Font;
use PhpOffice\PhpPresentation\Style\Shadow;
use PhpOffice\PhpPresentation\Style\Alignment;
Expand Down Expand Up @@ -247,6 +248,26 @@ protected function loadStyle(\DOMElement $nodeStyle)
$oShadow->setDistance(CommonDrawing::centimetersToPixels($distance));
}
}
// Read Fill
if ($nodeGraphicProps->hasAttribute('draw:fill')) {
$value = $nodeGraphicProps->getAttribute('draw:fill');

switch ($value) {
case 'none':
$oFill = new Fill();
$oFill->setFillType(Fill::FILL_NONE);
break;
case 'solid':
$oFill = new Fill();
$oFill->setFillType(Fill::FILL_SOLID);
if ($nodeGraphicProps->hasAttribute('draw:fill-color')) {
$oColor = new Color();
$oColor->setRGB(substr($nodeGraphicProps->getAttribute('draw:fill-color'), 1));
$oFill->setStartColor($oColor);
}
break;
}
}
}

$nodeTextProperties = $this->oXMLReader->getElement('style:text-properties', $nodeStyle);
Expand Down Expand Up @@ -316,6 +337,7 @@ protected function loadStyle(\DOMElement $nodeStyle)
$this->arrayStyles[$keyStyle] = array(
'alignment' => isset($oAlignment) ? $oAlignment : null,
'background' => isset($oBackground) ? $oBackground : null,
'fill' => isset($oFill) ? $oFill : null,
'font' => isset($oFont) ? $oFont : null,
'shadow' => isset($oShadow) ? $oShadow : null,
'listStyle' => isset($arrayListStyle) ? $arrayListStyle : null,
Expand Down Expand Up @@ -395,6 +417,7 @@ protected function loadShapeDrawing(\DOMElement $oNodeFrame)
$keyStyle = $oNodeFrame->getAttribute('draw:style-name');
if (isset($this->arrayStyles[$keyStyle])) {
$oShape->setShadow($this->arrayStyles[$keyStyle]['shadow']);
$oShape->setFill($this->arrayStyles[$keyStyle]['fill']);
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/PhpPresentation/Reader/PowerPoint2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,12 @@ protected function loadShapeDrawing(XMLReader $document, \DOMElement $node, Abst
}
}

$oElement = $document->getElement('p:spPr', $node);
if ($oElement instanceof \DOMElement) {
$oFill = $this->loadStyleFill($document, $oElement);
$oShape->setFill($oFill);
}

$oElement = $document->getElement('p:spPr/a:xfrm', $node);
if ($oElement instanceof \DOMElement) {
if ($oElement->hasAttribute('rot')) {
Expand Down
12 changes: 6 additions & 6 deletions src/PhpPresentation/Shape/AbstractGraphic.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public function getName()
/**
* Set Name
*
* @param string $pValue
* @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
* @param string $pValue
* @return $this
*/
public function setName($pValue = '')
{
Expand All @@ -138,8 +138,8 @@ public function getDescription()
/**
* Set Description
*
* @param string $pValue
* @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing
* @param string $pValue
* @return $this
*/
public function setDescription($pValue = '')
{
Expand All @@ -151,7 +151,7 @@ public function setDescription($pValue = '')
/**
* Set Width
*
* @param int $pValue
* @param int $pValue
* @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
*/
public function setWidth($pValue = 0)
Expand All @@ -171,7 +171,7 @@ public function setWidth($pValue = 0)
/**
* Set Height
*
* @param int $pValue
* @param int $pValue
* @return \PhpOffice\PhpPresentation\Shape\AbstractGraphic
*/
public function setHeight($pValue = 0)
Expand Down
20 changes: 20 additions & 0 deletions src/PhpPresentation/Style/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public function getAlpha()
return $alpha;
}

/**
* Set the alpha % of the ARGB
* @param int $alpha
* @return $this
*/
public function setAlpha($alpha = 100)
{
if ($alpha < 0) {
$alpha = 0;
}
if ($alpha > 100) {
$alpha = 100;
}
$alpha = round(($alpha / 100) * 255);
$alpha = dechex($alpha);
$alpha = str_pad($alpha, 2, '0', STR_PAD_LEFT);
$this->argb = $alpha . substr($this->argb, 2);
return $this;
}

/**
* Get RGB
*
Expand Down
35 changes: 27 additions & 8 deletions src/PhpPresentation/Writer/ODPresentation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,7 @@ public function writeTxtStyle(XMLWriter $objWriter, RichText $shape)
$objWriter->writeAttribute('style:parent-style-name', 'standard');
// style:graphic-properties
$objWriter->startElement('style:graphic-properties');
if ($shape->getShadow()->isVisible()) {
$this->writeStylePartShadow($objWriter, $shape->getShadow());
}
$this->writeStylePartShadow($objWriter, $shape->getShadow());
if (is_bool($shape->hasAutoShrinkVertical())) {
$objWriter->writeAttribute('draw:auto-grow-height', var_export($shape->hasAutoShrinkVertical(), true));
}
Expand Down Expand Up @@ -915,7 +913,7 @@ public function writeTxtStyle(XMLWriter $objWriter, RichText $shape)
* Write the default style information for an AbstractDrawingAdapter
*
* @param \PhpOffice\Common\XMLWriter $objWriter
* @param \PhpOffice\PhpPresentation\Shape\AbstractDrawingAdapter $shape
* @param AbstractDrawingAdapter $shape
*/
public function writeDrawingStyle(XMLWriter $objWriter, AbstractDrawingAdapter $shape)
{
Expand All @@ -928,10 +926,8 @@ public function writeDrawingStyle(XMLWriter $objWriter, AbstractDrawingAdapter $
// style:graphic-properties
$objWriter->startElement('style:graphic-properties');
$objWriter->writeAttribute('draw:stroke', 'none');
$objWriter->writeAttribute('draw:fill', 'none');
if ($shape->getShadow()->isVisible()) {
$this->writeStylePartShadow($objWriter, $shape->getShadow());
}
$this->writeStylePartFill($objWriter, $shape->getFill());
$this->writeStylePartShadow($objWriter, $shape->getShadow());
$objWriter->endElement();

$objWriter->endElement();
Expand Down Expand Up @@ -1305,13 +1301,36 @@ public function writeStyleSlide(XMLWriter $objWriter, Slide $slide, $incPage)
}


/**
* @param XMLWriter $objWriter
* @param Fill $oFill
*/
protected function writeStylePartFill(XMLWriter $objWriter, Fill $oFill)
{
switch ($oFill->getFillType()) {
case Fill::FILL_SOLID:
$objWriter->writeAttribute('draw:fill', 'solid');
$objWriter->writeAttribute('draw:fill-color', '#' . $oFill->getStartColor()->getRGB());
break;
case Fill::FILL_NONE:
default:
$objWriter->writeAttribute('draw:fill', 'none');
break;
}
$objWriter->writeAttribute('style:mirror', 'none');
}


/**
* @param XMLWriter $objWriter
* @param Shadow $oShadow
* @todo Improve for supporting any direction (https://sinepost.wordpress.com/2012/02/16/theyve-got-atan-you-want-atan2/)
*/
protected function writeStylePartShadow(XMLWriter $objWriter, Shadow $oShadow)
{
if (!$oShadow->isVisible()) {
return;
}
$objWriter->writeAttribute('draw:shadow', 'visible');
$objWriter->writeAttribute('draw:shadow-color', '#' . $oShadow->getColor()->getRGB());

Expand Down
11 changes: 5 additions & 6 deletions src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php
Original file line number Diff line number Diff line change
Expand Up @@ -1213,12 +1213,11 @@ protected function writeShapePic(XMLWriter $objWriter, AbstractGraphic $shape, $
// a:avLst
$objWriter->writeElement('a:avLst', null);
$objWriter->endElement();
if ($shape->getBorder()->getLineStyle() != Border::LINE_NONE) {
$this->writeBorder($objWriter, $shape->getBorder(), '');
}
if ($shape->getShadow()->isVisible()) {
$this->writeShadow($objWriter, $shape->getShadow());
}

$this->writeFill($objWriter, $shape->getFill());
$this->writeBorder($objWriter, $shape->getBorder(), '');
$this->writeShadow($objWriter, $shape->getShadow());

$objWriter->endElement();
$objWriter->endElement();
}
Expand Down
11 changes: 3 additions & 8 deletions src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ protected function writeShapeDrawing(XMLWriter $objWriter, ShapeDrawing\Abstract

// a:stretch
$objWriter->startElement('a:stretch');
$objWriter->writeElement('a:fillRect', null);
$objWriter->writeElement('a:fillRect');
$objWriter->endElement();

$objWriter->endElement();
Expand Down Expand Up @@ -859,16 +859,11 @@ protected function writeShapeDrawing(XMLWriter $objWriter, ShapeDrawing\Abstract
// a:prstGeom
$objWriter->startElement('a:prstGeom');
$objWriter->writeAttribute('prst', 'rect');

// a:avLst
// // a:prstGeom/a:avLst
$objWriter->writeElement('a:avLst', null);

// ##a:prstGeom
$objWriter->endElement();

$this->writeBorder($objWriter, $shape->getBorder(), '');

$this->writeShadow($objWriter, $shape->getShadow());

$objWriter->endElement();

$objWriter->endElement();
Expand Down
Loading

0 comments on commit e67d8ef

Please sign in to comment.