Skip to content

Commit

Permalink
PHPOffice#192 : Image Adapter (Doc & UnitTests)
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Apr 4, 2016
1 parent f8e6e7c commit 4f04f56
Show file tree
Hide file tree
Showing 32 changed files with 433 additions and 620 deletions.
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_drawing
shapes_media
shapes_richtext
shapes_table
Expand Down
13 changes: 0 additions & 13 deletions docs/shapes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,3 @@ Line
----

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


Drawing
-------

To create a drawing, use `createDrawingShape` method of slide.

.. code-block:: php
$drawing = $slide->createDrawingShape();
$drawing->setName('Unique name')
->setDescription('Description of the drawing')
->setPath('/path/to/drawing.filename');
75 changes: 75 additions & 0 deletions docs/shapes_drawing.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
.. _shapes_drawing:

Drawing
=======

To create a drawing, you have four sources : File, GD, Base64 and ZipFile.

File
----

To create a drawing, use `createDrawingShape` method of slide.

.. code-block:: php
$oShape = $oSlide->createDrawingShape();
$oShape->setName('Unique name')
->setDescription('Description of the drawing')
->setPath('/path/to/drawing.filename');
It's an alias for :

.. code-block:: php
use PhpOffice\PhpPresentation\Shape\Drawing\File;
$oShape = new File();
$oShape->setName('Unique name')
->setDescription('Description of the drawing')
->setPath('/path/to/drawing.filename');
$oSlide->addShape($oShape);
GD
--

.. code-block:: php
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
$gdImage = @imagecreatetruecolor($width, $height);
$oShape = new Gd();
$oShape->setName('Sample image')
->setDescription('Sample image')
->setImageResource($gdImage)
->setRenderingFunction(Drawing\Gd::RENDERING_JPEG)
->setMimeType(Drawing\Gd::MIMETYPE_DEFAULT);
$oSlide->addShape($oShape);
Base64
------

.. code-block:: php
use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
$oShape = new Base64();
$oShape->setName('Sample image')
->setDescription('Sample image')
->setImageResource($gdImage)
->setData('data:image/jpeg;base64,..........');
$oSlide->addShape($oShape);
ZipFile
-------

.. code-block:: php
use PhpOffice\PhpPresentation\Shape\Drawing\ZipFile;
$oShape = new ZipFile();
$oShape->setName('Sample image')
->setDescription('Sample image')
->setPath('zip://myzipfile.zip#path/in/zip/img.ext')
$oSlide->addShape($oShape);
6 changes: 3 additions & 3 deletions docs/shapes_media.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Example:
use PhpOffice\PhpPresentation\Shape\Media;
$oMedia = new Media();
$oMedia->setPath('file.mp4');
$oMedia->setPath('file.wmv');
// $oMedia->setPath('file.ogv');
$oSlide->addShape($oMedia);
Expand All @@ -32,5 +32,5 @@ Example:
Quirks
------

For PowerPoint2007 Writer, the prefered file format is MP4.
For ODPresentation Writer, the prefered file format is OGV.
For Windows readers, the prefered file format is WMV.
For Linux readers, the prefered file format is OGV.
2 changes: 1 addition & 1 deletion samples/Sample_03_Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
$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')
->setPath(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? './resources/sintel_trailer-480p.wmv' : './resources/sintel_trailer-480p.ogv')
->setResizeProportional(false)
->setHeight(90)
->setWidth(90)
Expand Down
Binary file removed samples/resources/sintel_trailer-480p.mp4
Binary file not shown.
Binary file added samples/resources/sintel_trailer-480p.wmv
Binary file not shown.
68 changes: 4 additions & 64 deletions src/PhpPresentation/Shape/Drawing.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,71 +17,11 @@

namespace PhpOffice\PhpPresentation\Shape;

use PhpOffice\PhpPresentation\ComparableInterface;
use PhpOffice\PhpPresentation\Shape\Drawing\File;

/**
* Drawing element
* @deprecated Drawing\File
*/
class Drawing extends AbstractGraphic implements ComparableInterface
{
/**
* Path
*
* @var string
*/
private $path;

/**
* Create a new \PhpOffice\PhpPresentation\Slide\Drawing
*/
public function __construct()
{
// Initialise values
$this->path = '';

// Initialize parent
parent::__construct();
}

/**
* Get Filename
*
* @return string
*/
public function getFilename()
{
return basename($this->path);
}

/**
* Get indexed filename (using image index)
*
* @return string
*/
public function getIndexedFilename()
{
return str_replace('.' . $this->getExtension(), '', $this->getFilename()) . $this->getImageIndex() . '.' . $this->getExtension();
}

/**
* Get Extension
*
* @return string
*/
public function getExtension()
{
$exploded = explode(".", basename($this->path));

return $exploded[count($exploded) - 1];
}

/**
* Get hash code
*
* @return string Hash code
*/
public function getHashCode()
{
return md5($this->path . parent::getHashCode() . __CLASS__);
}
}
class Drawing extends File
{}
9 changes: 7 additions & 2 deletions src/PhpPresentation/Shape/Drawing/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ public function setPath($pValue = '', $pVerifyFile = true)
}
$this->path = $pValue;

if ($this->width == 0 && $this->height == 0) {
list($this->width, $this->height) = getimagesize($this->getPath());
if ($pVerifyFile) {
if ($this->width == 0 && $this->height == 0) {
list($this->width, $this->height) = getimagesize($this->getPath());
}
}

return $this;
Expand Down Expand Up @@ -67,6 +69,9 @@ public function getExtension()
*/
public function getMimeType()
{
if (!file_exists($this->getPath())) {
throw new \Exception('File '.$this->getPath().' does not exist');
}
$image = getimagesize($this->getPath());
return image_type_to_mime_type($image[2]);
}
Expand Down
42 changes: 26 additions & 16 deletions src/PhpPresentation/Shape/Drawing/ZipFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ public function setPath($pValue = '')
*/
public function getContents()
{
$imagePath = substr($this->getPath(), 6);
$imagePathSplitted = explode('#', $imagePath);
if (!CommonFile::fileExists($this->getZipFileOut())) {
throw new \Exception('File '.$this->getZipFileOut().' does not exist');
}

$imageZip = new \ZipArchive();
$imageZip->open($imagePathSplitted[0]);
$imageContents = $imageZip->getFromName($imagePathSplitted[1]);
$imageZip->open($this->getZipFileOut());
$imageContents = $imageZip->getFromName($this->getZipFileIn());
$imageZip->close();
unset($imageZip);
return $imageContents;
Expand All @@ -55,29 +56,24 @@ public function getContents()
*/
public function getExtension()
{
$imagePath = substr($this->getPath(), 6);
$imagePathSplitted = explode('#', $imagePath);
return pathinfo($imagePathSplitted[1], PATHINFO_EXTENSION);
return pathinfo($this->getZipFileIn(), PATHINFO_EXTENSION);
}

/**
* @return string
*/
public function getMimeType()
{
$pZIPFile = str_replace('zip://', '', $this->getPath());
$pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
if (!CommonFile::fileExists($pZIPFile)) {
throw new \Exception("File $pZIPFile does not exist");
if (!CommonFile::fileExists($this->getZipFileOut())) {
throw new \Exception('File '.$this->getZipFileOut().' does not exist');
}
$pImgFile = substr($this->getPath(), strpos($this->getPath(), '#') + 1);
$oArchive = new \ZipArchive();
$oArchive->open($pZIPFile);
$oArchive->open($this->getZipFileOut());
if (!function_exists('getimagesizefromstring')) {
$uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
$uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($this->getZipFileIn()));
$image = getimagesize($uri);
} else {
$image = getimagesizefromstring($oArchive->getFromName($pImgFile));
$image = getimagesizefromstring($oArchive->getFromName($this->getZipFileIn()));
}
return image_type_to_mime_type($image[2]);
}
Expand All @@ -87,11 +83,25 @@ public function getMimeType()
*/
public function getIndexedFilename()
{
$output = substr($this->getPath(), strpos($this->getPath(), '#') + 1);
$output = pathinfo($this->getZipFileIn(), PATHINFO_FILENAME);
$output = str_replace('.' . $this->getExtension(), '', $output);
$output .= $this->getImageIndex();
$output .= '.'.$this->getExtension();
$output = str_replace(' ', '_', $output);
return $output;
}

protected function getZipFileOut()
{
$path = str_replace('zip://', '', $this->getPath());
$path = explode('#', $path);
return empty($path[0]) ? '' : $path[0];
}

protected function getZipFileIn()
{
$path = str_replace('zip://', '', $this->getPath());
$path = explode('#', $path);
return empty($path[1]) ? '' : $path[1];
}
}
3 changes: 3 additions & 0 deletions src/PhpPresentation/Shape/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public function getMimeType()
case 'ogv':
$mimetype = 'video/ogg';
break;
case 'wmv':
$mimetype = 'video/x-ms-wmv';
break;
default:
$mimetype = 'application/octet-stream';
}
Expand Down
28 changes: 28 additions & 0 deletions src/PhpPresentation/Shape/MemoryDrawing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\Shape\Drawing\Gd;

/**
* Memory drawing shape
* @deprecated Drawing\Gd
*/
class MemoryDrawing extends Gd
{
}
Loading

0 comments on commit 4f04f56

Please sign in to comment.