PHP, 原创,

PHPOffice\PHPPresentation添加背景格式(图案填充)功能

使用PHPPresentation时我们发现,该开源软件竟然不支持图案填充功能,于是就自己补充了下,(本想将自己代码贡献给PHPOffice官方,因下载下来的开发版本无法通过单元测试,只能暂时写到此处供参考)

以下方式可以使PHPPresentation支持背景图案填充。

1.首先创建背景填充类文件 PHPOffice/PHPPresentation/src/PhpPresentation/Slide/Background/FillType.php

代码如下

<?php
/**
* Created by chenxue4076@163.com.
* User: chenxue4076@163.com
* Date: 2019/02/27
* Time: 14:46
* File name: FillType.php
*/
namespace PhpOffice\PhpPresentation\Slide\Background;

use PhpOffice\PhpPresentation\Slide\AbstractBackground;
use PhpOffice\PhpPresentation\Style\Fill as StyleFill;

class FillType extends AbstractBackground
{
const FILL_PATTERN_BACKGROUND = array(
"cross",
"dashDnDiag", //dashed downward diagonal 下对角虚线
"dashHorz", //dashed horizontal 横虚线
"dashUpDiag", //dashed upward diagonal 上对角虚线
"dashVert", //dashed vertical 竖虚线
"diagBrick", //diagonal brick 对角砖型
"diagCross",
"divot", //divot 草皮
"dkDnDiag", //dark downward diagonal 深色下对角线
"dkHorz", //dark horizontal 深色横线
"dkUpDiag", //dark upward diagonal 深色上对角线
"dkVert", //dark vertical 深色竖线
"dnDiag",
"dotDmnd", //dotted diamond 点式菱形
"dotGrid", //dotted grid 虚线网格
"horz",
"horzBrick", //horizontal brick 横向砖型
"lgCheck", //large checker board 大棋盘
"lgConfetti", //large confetti 大纸屑
"lgGrid", //large grid 大网格
"ltDnDiag", //light downward diagonal 浅色下对角线
"ltHorz", //light horizontal 浅色横线
"ltUpDiag", //light upward diagonal 浅色上对角线
"ltVert", //light vertical 浅色竖线
"narHorz", //narrow horizontal 窄横线
"narVert", //narrow vertical 窄竖线
"openDmnd", //open diamond 轮廓式菱形?
"pct5", //5%
"pct10", //10%
"pct20", //20%
"pct25", //25%
"pct30", //30%
"pct40", //40%
"pct50", //50%
"pct60", //60%
"pct70", //70%
"pct75", //75%
"pct80", //80%
"pct90", //90%
"plaid", //plaid 苏格兰方格
"shingle", //shingle卵石 瓦形
"smCheck", //small checker board 小棋盘
"smConfetti", //small confetti 小纸屑
"smGrid", //small grid 小网格
"solidDmnd", //solid diamond 实心菱形
"sphere", //ball 球体
"trellis", //trellises 棚架
"upDiag",
"vert",
"wave", //wave 波浪
"wdDnDiag", //wide downward diagonal 宽下对角线
"wdUpDiag", //wide upward diagonal 宽上对角线
"weave", //weave 编织物
"zigZag" //zig zag 之字形
);
/**
*
@var StyleFill
*/
protected $fill;

/**
*
@param StyleFill|null $fill
*
@return $this
*/
public function setFillBackground(StyleFill $fill = null)
{
$this->fill = $fill;
return $this;
}

/**
*
@return StyleFill
*/
public function getFillBackground()
{
return $this->fill;
}
}

2.修改文件
PHPOffice/PHPPresentation/src/PhpPresentation/Writer/PowerPoint2007/AbstractSlide.php

找到函数 writeSlideBackground

在下面代码前

// > p:bg
$objWriter->endElement();

添加判断代码

//chenxue4076@163.com add FillType
if($oBackground instanceof Slide\Background\FillType) {
// p:bgPr
$objWriter->startElement('p:bgPr');
$this->writePatternFill($objWriter, $oBackground->getFillBackground());
// > p:bgPr
$objWriter->endElement();
}
//chenxue4076@163.com add FillType end

3.修改文件
PHPOffice/PHPPresentation/src/PhpPresentation/Writer/PowerPoint2007/AbstractDecoratorWriter.php

1)顶部添加引用

use PhpOffice\PhpPresentation\Slide\Background\FillType;

2) 找到函数 writePatternFill

在代码后面

// a:pattFill
$objWriter->startElement('a:pattFill');

添加如下代码

//chenxue4076@163.com add fill style
if( in_array($pFill->getFillType(), FillType::FILL_PATTERN_BACKGROUND)) {
$objWriter->writeAttribute('prst', $pFill->getFillType());
}
//chenxue4076@163.com add fill style end

经过以上三个文件的修改,就可以使用图案填充了,图案填充的代码调用如下

/**
*
@param PhpOffice\PhpPresentation\Slide $oSlide
*
@param string $fillType "pct5","pct10","pct20","pct25","pct30","pct40","pct50","pct60","pct70","pct75","pct80","pct90","horz","vert","ltHorz","ltVert","dkHorz","dkVert","narHorz","narVert","dashHorz","dashVert","cross","dnDiag","upDiag","ltDnDiag","ltUpDiag","dkDnDiag","dkUpDiag","wdDnDiag","wdUpDiag","dashDnDiag","dashUpDiag","diagCross","smCheck","lgCheck","smGrid","lgGrid","dotGrid","smConfetti","lgConfetti","horzBrick","diagBrick","solidDmnd","openDmnd","dotDmnd","plaid","sphere","weave","divot","shingle","wave","trellis","zigZag"
*
@param string $startColor
*
@param string $endColor
*
@return PhpOffice\PhpPresentation\Slide $oSlide
*
@throws Exception
*/
protected function shapeFillBackground($oSlide, $fillType, $startColor, $endColor)
{
$fill = new Fill();
$fill->setFillType($fillType)->setStartColor(new Color($startColor))->setEndColor(new Color($endColor));
$oBkgFill = new PhpOffice\PhpPresentation\Slide\Background\FillType();
$oBkgFill->setFillBackground($fill);
$oSlide->setBackground($oBkgFill);
return $oSlide;
}

完成。

(25)

文章为个人原创:如需转载,请注明出处。谢谢!

Author Since: Jul 05, 2018

Related Post