Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#123 : Support for Video #209

Merged
merged 20 commits into from
Mar 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- ODPresentation & PowerPoint2007 & Serialized Writer : Support for Zip Adapter - @Progi1984 GH-176
- ODPresentation & PowerPoint2007 Writer : language property to TextElement - @skrajewski & @Progi1984 GH-180
- ODPresentation & PowerPoint2007 Writer : Add Font Support For Chart Axis - @jrking4 GH-186
- ODPresentation & PowerPoint2007 Writer : Support for video - @Progi1984 GH-123
- PowerPoint2007 Writer : Presentation with predefined View Type - @Progi1984 GH-120

## 0.6.0 - 2016-01-24
Expand Down
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ PHPPresentation is a library written in pure PHP that provides a set of classes

shapes_chart
shapes_comment
shapes_media
shapes_richtext
shapes_table

Expand Down
19 changes: 0 additions & 19 deletions docs/shapes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,11 @@ Example:
->setOffsetX(170)
->setOffsetY(180);

Rich text
---------

The Rich text has :ref:`its own page <shapes_richtext>`.

Line
----

To create a line, use `createLineShape` method of slide.

Chart
-----

The Chart has :ref:`its own page <shapes_chart>`.

Comment
-------

The Comment has :ref:`its own page <shapes_comment>`.

Drawing
-------
Expand All @@ -58,8 +44,3 @@ To create a drawing, use `createDrawingShape` method of slide.
$drawing->setName('Unique name')
->setDescription('Description of the drawing')
->setPath('/path/to/drawing.filename');

Table
-----

The Table has :ref:`its own page <shapes_table>`.
36 changes: 36 additions & 0 deletions docs/shapes_media.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.. _shapes_table:

Media
=====

To create a video, create an object `Media`.

Example:

.. code-block:: php

use PhpOffice\PhpPresentation\Shape\Media;

$oMedia = new Media();
$oMedia->setPath('file.mp4');
// $oMedia->setPath('file.ogv');
$oSlide->addShape($oMedia);

You can define text and date with setters.

Example:

.. code-block:: php

use PhpOffice\PhpPresentation\Shape\Media;

$oMedia = new Media();
$oMedia->setName('Name of the Media');
$oSlide->addShape($oMedia);


Quirks
------

For PowerPoint2007 Writer, the prefered file format is MP4.
For ODPresentation Writer, the prefered file format is OGV.
27 changes: 20 additions & 7 deletions samples/Sample_03_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

// Create new PHPPresentation object
Expand All @@ -24,13 +25,13 @@
echo date('H:i:s') . ' Add a drawing to the slide'.EOL;
$shape = new MemoryDrawing();
$shape->setName('Sample image')
->setDescription('Sample image')
->setImageResource($gdImage)
->setRenderingFunction(MemoryDrawing::RENDERING_JPEG)
->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT)
->setHeight(36)
->setOffsetX(10)
->setOffsetY(10);
->setDescription('Sample image')
->setImageResource($gdImage)
->setRenderingFunction(MemoryDrawing::RENDERING_JPEG)
->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT)
->setHeight(36)
->setOffsetX(10)
->setOffsetY(10);
$currentSlide->addShape($shape);

// Add a file drawing (GIF) to the slide
Expand All @@ -55,6 +56,18 @@
->setOffsetY(200);
$currentSlide->addShape($shape);

// 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.mp4' : './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) {
Expand Down
Binary file added samples/resources/sintel_trailer-480p.mp4
Binary file not shown.
Binary file added samples/resources/sintel_trailer-480p.ogv
Binary file not shown.
Binary file removed samples/resources/sintel_trailer-480p.wmv
Binary file not shown.
27 changes: 27 additions & 0 deletions src/PhpPresentation/Shape/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* This file is part of PHPPresentation - A pure PHP library for reading and writing
* presentations documents.
*
* PHPPresentation is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
*
* @link https://github.com/PHPOffice/PHPPresentation
* @copyright 2009-2015 PHPPresentation contributors
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/

namespace PhpOffice\PhpPresentation\Shape;

use PhpOffice\PhpPresentation\ComparableInterface;

/**
* Media element
*/
class Media extends Drawing implements ComparableInterface
{
}
10 changes: 9 additions & 1 deletion src/PhpPresentation/Writer/ODPresentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,25 @@ public function save($pFilename)
$oPresentation = $this->getPhpPresentation();
$arrayChart = array();

$arrayFiles = array();
$oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation');
foreach ($oDir as $oFile) {
if (!$oFile->isFile()) {
continue;
}
$class = __NAMESPACE__.'\\ODPresentation\\'.$oFile->getBasename('.php');

$class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php');
$o = new \ReflectionClass($class);

if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) {
continue;
}
$arrayFiles[$oFile->getBasename('.php')] = $o;
}

ksort($arrayFiles);

foreach ($arrayFiles as $o) {
$oService = $o->newInstance();
$oService->setZip($oZip);
$oService->setPresentation($oPresentation);
Expand Down
51 changes: 51 additions & 0 deletions src/PhpPresentation/Writer/ODPresentation/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpOffice\Common\Text;
use PhpOffice\Common\XMLWriter;
use PhpOffice\PhpPresentation\Shape\Comment;
use PhpOffice\PhpPresentation\Shape\Media;
use PhpOffice\PhpPresentation\Slide;
use PhpOffice\PhpPresentation\Shape\AbstractDrawing;
use PhpOffice\PhpPresentation\Shape\Chart;
Expand Down Expand Up @@ -282,6 +283,8 @@ public function writeContent()
$this->writeShapeLine($objWriter, $shape);
} elseif ($shape instanceof Chart) {
$this->writeShapeChart($objWriter, $shape);
} elseif ($shape instanceof Media) {
$this->writeShapeMedia($objWriter, $shape);
} elseif ($shape instanceof AbstractDrawing) {
$this->writeShapePic($objWriter, $shape);
} elseif ($shape instanceof Group) {
Expand Down Expand Up @@ -316,6 +319,54 @@ public function writeContent()
return $objWriter->getData();
}

/**
* Write picture
*
* @param \PhpOffice\Common\XMLWriter $objWriter
* @param \PhpOffice\PhpPresentation\Shape\Media $shape
*/
public function writeShapeMedia(XMLWriter $objWriter, Media $shape)
{
// draw:frame
$objWriter->startElement('draw:frame');
$objWriter->writeAttribute('draw:name', $shape->getName());
$objWriter->writeAttribute('svg:width', Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getWidth()), 3) . 'cm');
$objWriter->writeAttribute('svg:height', Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getHeight()), 3) . 'cm');
$objWriter->writeAttribute('svg:x', Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getOffsetX()), 3) . 'cm');
$objWriter->writeAttribute('svg:y', Text::numberFormat(CommonDrawing::pixelsToCentimeters($shape->getOffsetY()), 3) . 'cm');
$objWriter->writeAttribute('draw:style-name', 'gr' . $this->shapeId);
// draw:frame > draw:plugin
$objWriter->startElement('draw:plugin');
$objWriter->writeAttribute('xlink:href', 'Pictures/' . md5($shape->getPath()) . '.' . $shape->getExtension());
$objWriter->writeAttribute('xlink:type', 'simple');
$objWriter->writeAttribute('xlink:show', 'embed');
$objWriter->writeAttribute('xlink:actuate', 'onLoad');
$objWriter->writeAttribute('draw:mime-type', 'application/vnd.sun.star.media');

$objWriter->startElement('draw:param');
$objWriter->writeAttribute('draw:name', 'Loop');
$objWriter->writeAttribute('draw:value', 'false');
$objWriter->endElement();
$objWriter->startElement('draw:param');
$objWriter->writeAttribute('draw:name', 'Mute');
$objWriter->writeAttribute('draw:value', 'false');
$objWriter->endElement();
$objWriter->startElement('draw:param');
$objWriter->writeAttribute('draw:name', 'VolumeDB');
$objWriter->writeAttribute('draw:value', 0);
$objWriter->endElement();
$objWriter->startElement('draw:param');
$objWriter->writeAttribute('draw:name', 'Zoom');
$objWriter->writeAttribute('draw:value', 'fit');
$objWriter->endElement();

// draw:frame > ## draw:plugin
$objWriter->endElement();

// ## draw:frame
$objWriter->endElement();
}

/**
* Write picture
*
Expand Down
2 changes: 1 addition & 1 deletion src/PhpPresentation/Writer/PowerPoint2007/PptMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function render()
{
for ($i = 0; $i < $this->getDrawingHashTable()->count(); ++$i) {
$shape = $this->getDrawingHashTable()->getByIndex($i);
if ($shape instanceof Drawing) {
if ($shape instanceof Drawing || $shape instanceof Media) {
$imagePath = $shape->getPath();
if (strpos($imagePath, 'zip://') !== false) {
$imagePath = substr($imagePath, 6);
Expand Down
30 changes: 28 additions & 2 deletions src/PhpPresentation/Writer/PowerPoint2007/PptSlides.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpOffice\PhpPresentation\Shape\Drawing as ShapeDrawing;
use PhpOffice\PhpPresentation\Shape\Group;
use PhpOffice\PhpPresentation\Shape\Line;
use PhpOffice\PhpPresentation\Shape\Media;
use PhpOffice\PhpPresentation\Shape\MemoryDrawing as MemoryDrawing;
use PhpOffice\PhpPresentation\Shape\RichText;
use PhpOffice\PhpPresentation\Shape\RichText\BreakElement;
Expand Down Expand Up @@ -716,7 +717,33 @@ protected function writeShapePic(XMLWriter $objWriter, AbstractDrawing $shape, $
$objWriter->endElement();

// p:nvPr
$objWriter->writeElement('p:nvPr', null);
$objWriter->startElement('p:nvPr');
/**
* @link : https://github.com/stefslon/exportToPPTX/blob/master/exportToPPTX.m#L2128
*/
if ($shape instanceof Media) {
// p:nvPr > a:videoFile
$objWriter->startElement('a:videoFile');
$objWriter->writeAttribute('r:link', $shape->relationId);
$objWriter->endElement();
// p:nvPr > p:extLst
$objWriter->startElement('p:extLst');
// p:nvPr > p:extLst > p:ext
$objWriter->startElement('p:ext');
$objWriter->writeAttribute('uri', '{DAA4B4D4-6D71-4841-9C94-3DE7FCFB9230}');
// p:nvPr > p:extLst > p:ext > p14:media
$objWriter->startElement('p14:media');
$objWriter->writeAttribute('r:embed', $shape->relationId);
$objWriter->writeAttribute('xmlns:p14', 'http://schemas.microsoft.com/office/powerpoint/2010/main');
// p:nvPr > p:extLst > p:ext > ##p14:media
$objWriter->endElement();
// p:nvPr > p:extLst > ##p:ext
$objWriter->endElement();
// p:nvPr > ##p:extLst
$objWriter->endElement();
}
// ##p:nvPr
$objWriter->endElement();
$objWriter->endElement();

// p:blipFill
Expand All @@ -736,7 +763,6 @@ protected function writeShapePic(XMLWriter $objWriter, AbstractDrawing $shape, $

// p:spPr
$objWriter->startElement('p:spPr');

// a:xfrm
$objWriter->startElement('a:xfrm');
$objWriter->writeAttribute('rot', CommonDrawing::degreesToAngle($shape->getRotation()));
Expand Down
15 changes: 15 additions & 0 deletions tests/PhpPresentation/Tests/Shape/MediaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PhpPresentation\Tests\Shape;

use PhpOffice\PhpPresentation\Shape\Media;

class MediaTest extends \PHPUnit_Framework_TestCase
{
public function testInheritance()
{
$object = new Media();

$this->assertInstanceOf('PhpOffice\\PhpPresentation\\Shape\\Drawing', $object);
}
}
43 changes: 43 additions & 0 deletions tests/PhpPresentation/Tests/Writer/ODPresentation/ContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
namespace PhpOffice\PhpPresentation\Tests\Writer\ODPresentation;

use PhpOffice\Common\Drawing as CommonDrawing;
use PhpOffice\Common\Text;
use PhpOffice\PhpPresentation\PhpPresentation;
use PhpOffice\PhpPresentation\Shape\Comment;
use PhpOffice\PhpPresentation\Shape\Media;
use PhpOffice\PhpPresentation\Shape\RichText\Run;
use PhpOffice\PhpPresentation\Slide\Transition;
use PhpOffice\PhpPresentation\Style\Alignment;
Expand Down Expand Up @@ -257,6 +259,47 @@ public function testListWithRichText()
$this->assertTrue($pres->elementExists($element, 'content.xml'));
}

public function testMedia()
{
$expectedName = 'MyName';
$expectedWidth = rand(1, 100);
$expectedHeight = rand(1, 100);
$expectedX = rand(1, 100);
$expectedY = rand(1, 100);

$oMedia = new Media();
$oMedia->setPath(PHPPRESENTATION_TESTS_BASE_DIR . '/resources/videos/sintel_trailer-480p.ogv')
->setName($expectedName)
->setResizeProportional(false)
->setHeight($expectedHeight)
->setWidth($expectedWidth)
->setOffsetX($expectedX)
->setOffsetY($expectedY);

$expectedWidth = Text::numberFormat(CommonDrawing::pixelsToCentimeters($expectedWidth), 3) . 'cm';
$expectedHeight = Text::numberFormat(CommonDrawing::pixelsToCentimeters($expectedHeight), 3) . 'cm';
$expectedX = Text::numberFormat(CommonDrawing::pixelsToCentimeters($expectedX), 3) . 'cm';
$expectedY = Text::numberFormat(CommonDrawing::pixelsToCentimeters($expectedY), 3) . 'cm';

$oPhpPresentation = new PhpPresentation();
$oSlide = $oPhpPresentation->getActiveSlide();
$oSlide->addShape($oMedia);

$xmlObject = TestHelperDOCX::getDocument($oPhpPresentation, 'ODPresentation');
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame';
$this->assertTrue($xmlObject->elementExists($element, 'content.xml'));
$this->assertEquals($expectedName, $xmlObject->getElementAttribute($element, 'draw:name', 'content.xml'));
$this->assertEquals($expectedWidth, $xmlObject->getElementAttribute($element, 'svg:width', 'content.xml'));
$this->assertEquals($expectedHeight, $xmlObject->getElementAttribute($element, 'svg:height', 'content.xml'));
$this->assertEquals($expectedX, $xmlObject->getElementAttribute($element, 'svg:x', 'content.xml'));
$this->assertEquals($expectedY, $xmlObject->getElementAttribute($element, 'svg:y', 'content.xml'));
$element = '/office:document-content/office:body/office:presentation/draw:page/draw:frame/draw:plugin';
$this->assertTrue($xmlObject->elementExists($element, 'content.xml'));
$this->assertEquals('application/vnd.sun.star.media', $xmlObject->getElementAttribute($element, 'draw:mime-type', 'content.xml'));
$this->assertStringStartsWith('Pictures/', $xmlObject->getElementAttribute($element, 'xlink:href', 'content.xml'));
$this->assertStringEndsWith('ogv', $xmlObject->getElementAttribute($element, 'xlink:href', 'content.xml'));
}

public function testNote()
{
$oPhpPresentation = new PhpPresentation();
Expand Down
Loading