<?php
/**
* @package clPHPppt
* @author {@link mailto:chritian-hide@address.com} Christian Arnold
*/
/**
* Loads the constants
*/
require_once('clPHPppt_constants.php');
/**
* clPHPppt PowerPoint PHP Class
*
* This class should be understood as a wrapper class for the COM powerpoint.application class. Therefore it only works if your PHP script is parsed on a Windows Server with Microsoft PowerPoint installed
*
* @author Christian Arnold
* @version 1.0
* @package clPHPppt
*/
class clPHPppt {
//The Basics
private $powerpnt = null;
private $ppt = null;
private $powerpnt_version = "";
/**
* Creates the PowerPoint Instance
*
* @param bool $visible true|false if the PowerPoint Window will be visible on the servers desktop
* @param string $filename If set, PowerPoint tries to open the given *.ppt or *.pot
*/
function __construct($visible, $filename = "") {
$this->powerpnt = new COM("powerpoint.application");
$this->powerpnt->Visible = $visible;
$this->powerpnt_version = $this->powerpnt->Version;
if(strlen($filename) == 0) {
//Creating a new presentation
$this->ppt = $this->powerpnt->Presentations->Add();
} else {
$this->ppt = $this->powerpnt->Presentations->Open($filename, true, true, $visible);
}
}
/**
* Destructor, ends PowerPoint and cleans up
*/
function __destruct() {
$this->powerpnt->quit();
}
/**
* Returns the PowerPoint Version that can be accessed by PHPppt
* @return string Versionstring, i.e. 11.xx for PowerPoint 2003
*/
public function pptVersion() {
return $this->powerpnt_version;
}
/**
* Creates a slide at the given index
*
* @param int $index The index number that should be assigned to the slide
* @param int $layout The layout style, use ppLayouts for easy selection
*/
public function addSlide($index, $layout) {
$this->ppt->Slides->Add($index, $layout);
}
/**
* Deletes the slide with the given index
*
* @param int $index
*/
public function delSlide($index) {
$this->ppt->Slides($index)->Delete();
}
/**
* Creates a Textbox
*
* Creates a Textbox. Look at the {@link applySettings()} function to see which settings are possible
*
* @param int $slide Slide number
* @param string $text Text that should be desplayed
* @param float $left Distance to the left border
* @param float $top Distance to the top border
* @param float $width Width of the textbox
* @param float $height Height of the textbox
* @param mixed $settings Settings for the TextBox (look at {@link applySettings()})
* @return shapeObject
*/
public function addTextbox($slide, $text, $left, $top, $width, $heigth, $settings = array()) {
$shape = $this->ppt->Slides[$slide]->Shapes->AddTextbox(1, $left, $top, $width, $heigth);
$shapeId = $shape->Id;
$shape->TextFrame->TextRange->Text = $text;
if(count($settings) > 0)
$this->applySettings($slide, $shape, $settings);
return $shape;
}
/**
* Adds a Picture to a specific slide
*
* @param int $slide Slide number
* @param string $filename Path to the filename
* @param float $left Distance to the left border
* @param float $top Distance to the top border
* @param float $width Width of the textbox, -1 for regular image width
* @param float $height Height of the textbox, -1 for regular image height
* @param bool $linkToFile When true, image is relloaded each time the ppt is opened (not recommended)
* @param bool $saveWithDocument When true, the image is saved in the ppt file (recommended)
* @return shapeObject
*/
public function addPicture($slide, $filename, $left, $top, $width = -1, $height = -1, $linkToFile = false, $saveWithDocument = true) {
$shape = $this->ppt->Slides[$slide]->Shapes->AddPicture($filename, $linkToFile, $saveWithDocument, $left, $top, $width, $height);
return $shape;
}
/**
* Creates a AutoShape
*
* @param int $slide Slide number
* @param int $type Autoshape type (look at {@link ppAutoShapes})
* @param float $left Distance to the left border
* @param float $top Distance to the top border
* @param float $width Width of the shape
* @param float $height Height of the shape
* @param mixed $settings Settings for the TextBox (look at {@link applySettings()})
* @return shapeObject
*/
public function addShape($slide, $type, $left, $top, $width, $height, $settings = array()) {
$shape = $this->ppt->Slides[$slide]->Shapes->AddShape($type, $left, $top, $width, $height);
$this->applySettings($slide, $shape, $settings);
return $shape;
}
/**
* Creates a Table
*
* @param int $slide Slide number
* @param int $numRows Quantity of rows
* @param int $numCols Quantity of columns
* @param float $left Distance to the left border, -1 for PowerPoint default
* @param float $top Distance to the top border, -1 for PowerPoint default
* @param float $width Width of the table, -1 for PowerPoint default
* @param float $height Height of the table, -1 for PowerPoint default
* @return shapeObject
*/
public function addTable($slide, $numRows, $numCols, $left = -1, $top = -1, $width = -1, $height = -1, $settings = array()) {
$shape = $this->ppt->Slides[$slide]->Shapes->AddTable($numRows, $numCols, $left, $top, $width, $height);
if(count($settings) > 0)
$this->applySettings($slide, $shape, $settings);
return $shape;
}
/**
* Sets the Text for Shapes
*
* @param int $slide Slide number, starting at 1, -1 to ignore when using shapeObject
* @param mixed Either the shape index number or name or the shapeObject
* @param string $text Text that will be displayed
* @param mixed $settings Settings for the Shape (look at {@link applySettings()})
*/
public function setShapeText($slide, $shape, $text, $settings = array()) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$shape->TextFrame->TextRange->Text = $text;
if(count($settings) > 0)
$this->applySettings($slide, $shape, $settings);
}
/**
* Retrieves the text of the given shape. Will be unformated!
*
* @param int $slide The slide index where the shape is located
* @param mixed $shape Either the shape index number or name or the shapeObject
* @return string
*/
public function getShapeText($slide, $shape) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape]->TextFrame->TextRange;
$text = $shape->Text;
return $text;
}
/**
* Retrieves the text of the given table cell. Will be unformated!
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
* @param int $row The row number
* @param int $col The col number
* @param string $text The text you want to set
* @param mixed $settings Settings for the Cell (look at {@link applySettings()})
* @return string
*/
public function setTableText($slide, $shape, $row, $col, $text, $settings = array()) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$shape = $shape->Table->Cell($row, $col)->Shape;
$shape->TextFrame->TextRange->Text = $text;
if(count($settings) > 0)
$this->applySettings($slide, $shape, $settings);
return $shape;
}
/**
* Retrieves the text of the given shape. Will be unformated!
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
* @param int $row The row number
* @param int $col The columns number
* @return string
*/
public function getTableText($slide, $shape, $row, $col) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$shape = $shape->Table->Cell($row, $col)->Shape;
$text = $shape->TextFrame->TextRange->Text;
}
/**
* Returns the shapeObject for the requested table cell
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
* @param int $row The row number
* @param int $col The columns number
* @return shapeObject
*/
public function getTableCellShape($slide, $shape, $row, $col) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$shape = $shape->Table->Cell($row, $col)->Shape;
return $shape;
}
/**
* Merges table cells
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
* @param int $sRow The start row number
* @param int $sCol The start columns number
* @param int $eRow The end row number
* @param int $eCol The end columns number
*/
public function mergeTableCells($slide, $shape, $sRow, $sCol, $eRow, $eCol) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$table = $shape->Table;
$table->Cell($sRow, $sCol)->Merge($table->Cell($eRow, $eCol));
}
/**
* Splits table cells
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
* @param int $sRow The start row number
* @param int $sCol The start columns number
* @param int $numRows Quantity of rows you want to get
* @param int $numCols Quantity of columns you want to get
*/
public function splitTableCells($slide, $shape, $sRow, $sCol, $numRows, $numCols) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$table = $shape->Table;
$table->Cell($sRow, $sCol)->Split($numRows, $numCols);
}
/**
* Adds a line or arrow
*
* @param int $slide The slide index where the shape should be added
* @param float $sX Starting X coordinate
* @param float $sY Starting Y coordinate
* @param float $eX Ending X coordinate
* @param float $eY Endingng Y coordinate
* @param mixed $settings Settings for the Cell (look at {@link applySettings()})
* @return shapeObject
*/
public function addLine($slide, $sX, $sY, $eX, $eY, $settings = array()) {
$shape = $this->ppt->Slides[$slide]->Shapes->AddLine($sX, $sY, $eX, $eY);
if(count($settings) > 0) {
$this->applySettings(-1, $shape, $settings);
}
return $shape;
}
/**
* Creates a WordArt text effect
*
* @param int $slide The slide index where the shape should be added
* @param float $effect Effect index (look at {@link ppTextEffectStyles()})
* @param float $left Distance to the left border
* @param float $top Distance to the top border
* @param string $text The text that should be displayed
* @param string $fontName Name of the font to use
* @param string $fontSize Size of font in points
* @param bool $fontBold If true, the font will be made bold
* @param bool $fontItalic If true, the font will be made italic
* @return shapeObject
*/
public function addTextEffect($slide, $effect, $left, $top, $text, $fontName, $fontSize, $fontBold = false, $fontItalic = false) {
$shape = $this->ppt->Slides[$slide]->Shapes->AddTextEffect($effect, $text, $fontName, $fontSize, $fontBold, $fontItalic, $left, $top);
return $shape;
}
/**
* Adds an OLE object to a slide
*
* @param int $slide The slide index where the shape should be added
* @param float $left Distance to the left border
* @param float $top Distance to the top border
* @param float $width Width of the table, -1 for PowerPoint default
* @param float $height Height of the table, -1 for PowerPoint default
* @param string $fileName Name of the file to include
* @param bool $displayAsIcon If true, an Icon is used instead of embedding the object visualy
* @param bool $link If true, the file will not be included in the PowerPoint file (not recommended!)
* @param string $iconFileName Name of the *.ico file. Only for use with $displayAsIcon = true. If not set, PowerPoint will load the default icon
* @param int $iconIndex Position of the icon in the *.ico file. Only for use with $displayAsIcon = true. If not set, PowerPoint will load the first icon
* @param string $iconLabel For what should be displayed under the icon. Only for use with $displayAsIcon = true. If not set, PowerPoint will name it the file type name
* @return shapeObject
*/
public function addOLEObject($slide, $left, $top, $width, $height, $fileName, $displayAsIcon = false, $link = false, $iconFileName = "", $iconIndex = 0, $iconLabel = "") {
$shape = $this->ppt->Slides[$slide]->Shapes->AddOLEObject($left, $top, $width, $height, "", $fileName, $displayAsIcon, $iconFileName, $iconIndex, $iconLabel, $link);
return $shape;
}
/**
* Applies settings to a given shape
*
* With applySettings you can change some settings for shapes. Here are all the possibilities you currently got
*
* Fill
* - fillEnabled - bool - Enabling or disabling fill.
* - fillForeColor - long - Set the color to use for filling. Use {@link getVBColorFromHEX()} or {@link getVBColorFromRGB()} for easy conversions.
* - fillTransparency - float - Use values from 0 to 1 to set the transparency of the filling.
*
* Line
* - lineEnabled - bool - Enabling or disabling the line (also used as borders for textboxes and so on).
* - lineWeight - int - Weight of the line or border in pt.
* - lineDashStyle - int - The dash style for the line or border. Use {@link ppLineDashStyles} for easy selection.
* - lineStyle - int - The line style for the line or border. Use {@link ppLineStyles} for easy selection.
* - lineForeColor - long - Set the color to use for the line or border. Use {@link getVBColorFromHEX()} or {@link getVBColorFromRGB()} for easy conversions.
* - lineTransparency - float - Use values from 0 to 1 to set the transparency of the line or border.
* - lineBeginArrowStyle - int - Style of the begin arrow. Use {@link ppArrowStyles} for easy selection.
* - lineBeginArrowWidth - int - Width of the begin arrow. Use {@link ppArrowWidths} for easy selection.
* - lineBeginArrowLength - int - Length of the begin arrow. Use {@link ppArrowLengths} for easy selection.
* - lineEndArrowStyle - int - Style of the end arrow. Use {@link ppArrowStyles} for easy selection.
* - lineEndArrowWidth - int - Width of the end arrow. Use {@link ppArrowWidths} for easy selection.
* - lineEndArrowLength - int - Length of the end arrow. Use {@link ppArrowLengths} for easy selection.
*
* Sizes
* - sizeHeight - float - Height for the shape.
* - sizeWidth - float - Width for the shape.
*
* Shadow
* - shadowType - int - Type of the shadow. Use {@link ppShadowTypes} for easy selection.
* - shadowForeColor - long - Set the color to use for the shadow. Use {@link getVBColorFromHEX()} or {@link getVBColorFromRGB()} for easy conversions.
* - shadowTransparency - float - Use values from 0 to 1 to set the transparency of the shadow.
*
* Text
* - fontName - string - Name of the font to use
* - fontSize - float - Size of the font to use
* - fontBold - bool - If true, the text will be made in bold
* - fontItalic - bool - If true, the text will be made italic
* - fontShadow - bool - If true, the text will have a shadow
* - fontEmboss - bool - If true, the text will be embossed
* - fontUnderline - bool - If true, the text will be underlinded
* - fontColor - long - Set the color to use for the text. Use {@link getVBColorFromHEX()} or {@link getVBColorFromRGB()} for easy conversions.
* - textAlign - int - The text can be aligned left, center and right. Use {@link ppTextAligns} for easy selection.
* - formAutosize - bool - If true, the shape changes it size to the text
* - verticalAnchor - int - The vertical anchor that should be used. Use {@link ppVerticalAnchor} for easy selection.
* - marginTop - float - Free space between top and text
* - marginLeft - float - Free space between left side and text
* - marginBottom - float - Free space between bottom and text
* - marginRight - float - Free space between right side and text
*
* Other
* - rotation - int - set the rotation of the object in degrees
*
* @todo textAutosize is working, but textAutofit is searched!
*
* @param int $slide The slide index where the shape is located
* @param mixed $shape Either the shape index number or name or the shapeObject
* @param mixed $settings The settings which should be applied
* @return shapeObject
*/
public function applySettings($slide, $shape, $settings) {
if(is_numeric($shape)) {
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
}
//Checking if TextRange is available for Shape
$ppST = new ppShapeTypes();
$trange_okay = false;
$table_okay = false;
$shapeType = $shape->Type;
if($shapeType == $ppST->msoCallout || //Table Cell
$shapeType == $ppST->msoPlaceholder || //Placeholder (awesome, where did I get that from?)
$shapeType == $ppST->msoTextBox || //Textbox
$shapeType == $ppST->msoAutoShape //I think you got it, right?
) {
$tframe = $shape->TextFrame;
$trange = $tframe->TextRange;
$trange_okay = true;
}
if($shapeType == $ppST->msoTable) {
$table_okay = true;
}
//Fill
if(isset($settings['fillEnabled']))
$shape->Fill->Visible = $settings['fillEnabled'];
if(isset($settings['fillForeColor']))
$shape->Fill->ForeColor->RGB = $settings['fillForeColor'];
if(isset($settings['fillTransparency']))
$shape->Fill->Transparency = $settings['fillTransparency'];
//Line, Arrows and Shape-Borders
if(isset($settings['lineEnabled']))
$shape->Line->Visible = $settings['lineEnabled'];
if(isset($settings['lineWeight']))
$shape->Line->Weight = $settings['lineWeight'];
if(isset($settings['lineDashStyle']))
$shape->Line->DashStyle = $settings['lineDashStyle'];
if(isset($settings['lineStyle']))
$shape->Line->Style = $settings['lineStyle'];
if(isset($settings['lineForeColor']))
$shape->Line->ForeColor->RGB = $settings['lineForeColor'];
if(isset($settings['lineBackColor']))
$shape->Line->BackColor->RGB = $settings['lineBackColor'];
if(isset($settings['lineTransparency']))
$shape->Line->Transparency = $settings['lineTransparency'];
if(isset($settings['lineBeginArrowStyle']))
$shape->Line->BeginArrowheadStyle = $settings['lineBeginArrowStyle'];
if(isset($settings['lineBeginArrowWidth']))
$shape->Line->BeginArrowheadWidth = $settings['lineBeginArrowWidth'];
if(isset($settings['lineBeginArrowLength']))
$shape->Line->BeginArrowheadLength = $settings['lineBeginArrowLength'];
if(isset($settings['lineEndArrowStyle']))
$shape->Line->EndArrowheadStyle = $settings['lineEndArrowStyle'];
if(isset($settings['lineEndArrowWidth']))
$shape->Line->EndArrowheadWidth = $settings['lineEndArrowWidth'];
if(isset($settings['lineEndArrowLength']))
$shape->Line->EndArrowheadLength = $settings['lineEndArrowLength'];
//Sizes
if(isset($settings['sizeHeight']))
$shape->Height = $settings['sizeHeight'];
if(isset($settings['sizeWidth']))
$shape->Width = $settings['sizeWidth'];
//Shadow and 3D
if(isset($settings['shadowType']))
$shape->Shadow->Type = $settings['shadowType'];
if(isset($settings['shadowForeColor']))
$shape->Shadow->ForeColor->RGB = $settings['shadowForeColor'];
if(isset($settings['shadowTransparency']))
$shape->Shadow->Transparency = $settings['shadowTransparency'];
//Text Stuff
if($trange_okay) {
if(isset($settings['fontName']))
$trange->Font->Name = $settings['fontName'];
if(isset($settings['fontSize']))
$trange->Font->Size = $settings['fontSize'];
if(isset($settings['fontBold']))
$trange->Font->Bold = $settings['fontBold'];
if(isset($settings['fontItalic']))
$trange->Font->Italic = $settings['fontItalic'];
if(isset($settings['fontShadow']))
$trange->Font->Shadow = $settings['fontShadow'];
if(isset($settings['fontEmboss']))
$trange->Font->Emboss = $settings['fontEmboss'];
if(isset($settings['fontUnderline']))
$trange->Font->Underline = $settings['fontUnderline'];
if(isset($settings['fontColor']))
$trange->Font->Color = $settings['fontColor'];
if(isset($settings['textAlign']))
$trange->ParagraphFormat->Alignment = $settings['textAlign'];
if(isset($settings['formAutosize']))
$tframe->AutoSize = $settings['formAutosize'];
if(isset($settings['marginBottom']))
$tframe->MarginBottom = $settings['marginBottom'];
if(isset($settings['marginLeft']))
$tframe->MarginLeft = $settings['marginLeft'];
if(isset($settings['marginRight']))
$tframe->MarginRight = $settings['marginRight'];
if(isset($settings['marginTop']))
$tframe->MarginTop = $settings['marginTop'];
if(isset($settings['verticalAnchor']))
$tframe->VerticalAnchor = $settings['verticalAnchor'];
}
//Table Stuff
if($table_okay) {
$table = $shape->Table;
if(isset($settings['table'])) {
foreach($settings['table'] as $todo) {
if(isset($todo['tableCWidth']) && is_array($todo['tableCWidth']))
$table->Columns($todo['tableCWidth']['col'])->Width = $todo['tableCWidth']['width'];
if(isset($todo['tableRHeight']) && is_array($todo['tableRHeight']))
$table->Rows($todo['tableRHeight']['row'])->Height = $todo['tableRHeight']['height'];
}
}
}
//Other
if(isset($settings['rotation']))
$shape->Rotation = $settings['rotation'];
return $shape;
}
/**
* Sets the indent level for a paragraph
*
* @todo Make use of the $bulletType!
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
* @param int $start Number of the paragraph, starting at 1
* @param int $length Quantity of paragraphs that should be affected by this call
* @param int $level The Level number
* @param int $bulletType Bullet type that should be used for this call
*/
public function setIndent($slide, $shape, $start, $length, $level, $bulletType) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$trange = $shape->TextFrame->TextRange->Paragraphs($start, $length)->IndentLevel = $level;
}
/**
* Method to change the properties for some chars in a text
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
* @param int $charStart Number of the first Char to start edit, starting at 1
* @param int $charCount Quantity of characters that should be affected by this call
* @param mixed $settings You can use all the settings available in the {@link applySettings()} method prefixed with "font"
*/
public function charEdit($slide, $shape, $charStart, $charCount, $settings) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$trange = $shape->TextFrame->TextRange->Characters($charStart, $charCount);
if(isset($settings['fontName']))
$trange->Font->Name = $settings['fontName'];
if(isset($settings['fontSize']))
$trange->Font->Size = $settings['fontSize'];
if(isset($settings['fontBold']))
$trange->Font->Bold = $settings['fontBold'];
if(isset($settings['fontItalic']))
$trange->Font->Italic = $settings['fontItalic'];
if(isset($settings['fontShadow']))
$trange->Font->Shadow = $settings['fontShadow'];
if(isset($settings['fontEmboss']))
$trange->Font->Emboss = $settings['fontEmboss'];
if(isset($settings['fontUnderline']))
$trange->Font->Underline = $settings['fontUnderline'];
if(isset($settings['fontColor']))
$trange->Font->Color = $settings['fontColor'];
}
/**
* Deletes any shape from a slide
*
* Use deleteShape to delete any shape types. This can be text boxes, autoshapes, WordArts, pictures, ...
*
* @param int $slide The slide index where the shape is located
* @param mixed Either the shape index number or name or the shapeObject
*/
public function deleteShape($slide, $shape) {
if(is_numeric($shape))
$shape = $this->ppt->Slides[$slide]->Shapes[$shape];
$shape->Delete();
}
/**
* Converts a RGB to Long Color
*
* @param int $red Red value between 0-255
* @param int $green Green value between 0-255
* @param int $blue Blue value between 0-255
* @return int long color code
*/
public function getVBColorFromRGB($red, $green, $blue) {
return $blue * 65536 + $green * 256 + $red;
}
/**
* Converts a HEX to Long Color
*
* @param string $hex HEX HTML color like #000000
* @return int long color code
*/
public function getVBColorFromHEX($hex) {
$arr = sscanf($hex, '#%2x%2x%2x');
return $this->getVBColorFromRGB($arr[0], $arr[1], $arr[2]);
}
/**
* Saves the PowePoint file to the filesystem under the path and filename given at class initialisation
*
* @param $filename string Filename with path where to save the ppt
*/
public function save($filename) {
$this->powerpnt->Presentations[1]->SaveAs($filename);
}
}
?>