Skip to content

Commit 4f04f56

Browse files
committed
#192 : Image Adapter (Doc & UnitTests)
1 parent f8e6e7c commit 4f04f56

32 files changed

+433
-620
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ PHPPresentation is a library written in pure PHP that provides a set of classes
3434

3535
shapes_chart
3636
shapes_comment
37+
shapes_drawing
3738
shapes_media
3839
shapes_richtext
3940
shapes_table

docs/shapes.rst

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,3 @@ Line
3131
----
3232

3333
To create a line, use `createLineShape` method of slide.
34-
35-
36-
Drawing
37-
-------
38-
39-
To create a drawing, use `createDrawingShape` method of slide.
40-
41-
.. code-block:: php
42-
43-
$drawing = $slide->createDrawingShape();
44-
$drawing->setName('Unique name')
45-
->setDescription('Description of the drawing')
46-
->setPath('/path/to/drawing.filename');

docs/shapes_drawing.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
.. _shapes_drawing:
2+
3+
Drawing
4+
=======
5+
6+
To create a drawing, you have four sources : File, GD, Base64 and ZipFile.
7+
8+
File
9+
----
10+
11+
To create a drawing, use `createDrawingShape` method of slide.
12+
13+
.. code-block:: php
14+
15+
$oShape = $oSlide->createDrawingShape();
16+
$oShape->setName('Unique name')
17+
->setDescription('Description of the drawing')
18+
->setPath('/path/to/drawing.filename');
19+
20+
It's an alias for :
21+
22+
.. code-block:: php
23+
24+
use PhpOffice\PhpPresentation\Shape\Drawing\File;
25+
26+
$oShape = new File();
27+
$oShape->setName('Unique name')
28+
->setDescription('Description of the drawing')
29+
->setPath('/path/to/drawing.filename');
30+
$oSlide->addShape($oShape);
31+
32+
GD
33+
--
34+
35+
.. code-block:: php
36+
37+
use PhpOffice\PhpPresentation\Shape\Drawing\Gd;
38+
39+
$gdImage = @imagecreatetruecolor($width, $height);
40+
41+
$oShape = new Gd();
42+
$oShape->setName('Sample image')
43+
->setDescription('Sample image')
44+
->setImageResource($gdImage)
45+
->setRenderingFunction(Drawing\Gd::RENDERING_JPEG)
46+
->setMimeType(Drawing\Gd::MIMETYPE_DEFAULT);
47+
$oSlide->addShape($oShape);
48+
49+
50+
Base64
51+
------
52+
53+
.. code-block:: php
54+
55+
use PhpOffice\PhpPresentation\Shape\Drawing\Base64;
56+
57+
$oShape = new Base64();
58+
$oShape->setName('Sample image')
59+
->setDescription('Sample image')
60+
->setImageResource($gdImage)
61+
->setData('data:image/jpeg;base64,..........');
62+
$oSlide->addShape($oShape);
63+
64+
ZipFile
65+
-------
66+
67+
.. code-block:: php
68+
69+
use PhpOffice\PhpPresentation\Shape\Drawing\ZipFile;
70+
71+
$oShape = new ZipFile();
72+
$oShape->setName('Sample image')
73+
->setDescription('Sample image')
74+
->setPath('zip://myzipfile.zip#path/in/zip/img.ext')
75+
$oSlide->addShape($oShape);

docs/shapes_media.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Example:
1212
use PhpOffice\PhpPresentation\Shape\Media;
1313
1414
$oMedia = new Media();
15-
$oMedia->setPath('file.mp4');
15+
$oMedia->setPath('file.wmv');
1616
// $oMedia->setPath('file.ogv');
1717
$oSlide->addShape($oMedia);
1818
@@ -32,5 +32,5 @@ Example:
3232
Quirks
3333
------
3434

35-
For PowerPoint2007 Writer, the prefered file format is MP4.
36-
For ODPresentation Writer, the prefered file format is OGV.
35+
For Windows readers, the prefered file format is WMV.
36+
For Linux readers, the prefered file format is OGV.

samples/Sample_03_Image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
$shape = new Media();
7272
$shape->setName('Video')
7373
->setDescription('Video')
74-
->setPath(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? './resources/sintel_trailer-480p.mp4' : './resources/sintel_trailer-480p.ogv')
74+
->setPath(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? './resources/sintel_trailer-480p.wmv' : './resources/sintel_trailer-480p.ogv')
7575
->setResizeProportional(false)
7676
->setHeight(90)
7777
->setWidth(90)
-4.17 MB
Binary file not shown.
6.96 MB
Binary file not shown.

src/PhpPresentation/Shape/Drawing.php

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,71 +17,11 @@
1717

1818
namespace PhpOffice\PhpPresentation\Shape;
1919

20-
use PhpOffice\PhpPresentation\ComparableInterface;
20+
use PhpOffice\PhpPresentation\Shape\Drawing\File;
2121

2222
/**
2323
* Drawing element
24+
* @deprecated Drawing\File
2425
*/
25-
class Drawing extends AbstractGraphic implements ComparableInterface
26-
{
27-
/**
28-
* Path
29-
*
30-
* @var string
31-
*/
32-
private $path;
33-
34-
/**
35-
* Create a new \PhpOffice\PhpPresentation\Slide\Drawing
36-
*/
37-
public function __construct()
38-
{
39-
// Initialise values
40-
$this->path = '';
41-
42-
// Initialize parent
43-
parent::__construct();
44-
}
45-
46-
/**
47-
* Get Filename
48-
*
49-
* @return string
50-
*/
51-
public function getFilename()
52-
{
53-
return basename($this->path);
54-
}
55-
56-
/**
57-
* Get indexed filename (using image index)
58-
*
59-
* @return string
60-
*/
61-
public function getIndexedFilename()
62-
{
63-
return str_replace('.' . $this->getExtension(), '', $this->getFilename()) . $this->getImageIndex() . '.' . $this->getExtension();
64-
}
65-
66-
/**
67-
* Get Extension
68-
*
69-
* @return string
70-
*/
71-
public function getExtension()
72-
{
73-
$exploded = explode(".", basename($this->path));
74-
75-
return $exploded[count($exploded) - 1];
76-
}
77-
78-
/**
79-
* Get hash code
80-
*
81-
* @return string Hash code
82-
*/
83-
public function getHashCode()
84-
{
85-
return md5($this->path . parent::getHashCode() . __CLASS__);
86-
}
87-
}
26+
class Drawing extends File
27+
{}

src/PhpPresentation/Shape/Drawing/File.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ public function setPath($pValue = '', $pVerifyFile = true)
3838
}
3939
$this->path = $pValue;
4040

41-
if ($this->width == 0 && $this->height == 0) {
42-
list($this->width, $this->height) = getimagesize($this->getPath());
41+
if ($pVerifyFile) {
42+
if ($this->width == 0 && $this->height == 0) {
43+
list($this->width, $this->height) = getimagesize($this->getPath());
44+
}
4345
}
4446

4547
return $this;
@@ -67,6 +69,9 @@ public function getExtension()
6769
*/
6870
public function getMimeType()
6971
{
72+
if (!file_exists($this->getPath())) {
73+
throw new \Exception('File '.$this->getPath().' does not exist');
74+
}
7075
$image = getimagesize($this->getPath());
7176
return image_type_to_mime_type($image[2]);
7277
}

src/PhpPresentation/Shape/Drawing/ZipFile.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,13 @@ public function setPath($pValue = '')
3838
*/
3939
public function getContents()
4040
{
41-
$imagePath = substr($this->getPath(), 6);
42-
$imagePathSplitted = explode('#', $imagePath);
41+
if (!CommonFile::fileExists($this->getZipFileOut())) {
42+
throw new \Exception('File '.$this->getZipFileOut().' does not exist');
43+
}
4344

4445
$imageZip = new \ZipArchive();
45-
$imageZip->open($imagePathSplitted[0]);
46-
$imageContents = $imageZip->getFromName($imagePathSplitted[1]);
46+
$imageZip->open($this->getZipFileOut());
47+
$imageContents = $imageZip->getFromName($this->getZipFileIn());
4748
$imageZip->close();
4849
unset($imageZip);
4950
return $imageContents;
@@ -55,29 +56,24 @@ public function getContents()
5556
*/
5657
public function getExtension()
5758
{
58-
$imagePath = substr($this->getPath(), 6);
59-
$imagePathSplitted = explode('#', $imagePath);
60-
return pathinfo($imagePathSplitted[1], PATHINFO_EXTENSION);
59+
return pathinfo($this->getZipFileIn(), PATHINFO_EXTENSION);
6160
}
6261

6362
/**
6463
* @return string
6564
*/
6665
public function getMimeType()
6766
{
68-
$pZIPFile = str_replace('zip://', '', $this->getPath());
69-
$pZIPFile = substr($pZIPFile, 0, strpos($pZIPFile, '#'));
70-
if (!CommonFile::fileExists($pZIPFile)) {
71-
throw new \Exception("File $pZIPFile does not exist");
67+
if (!CommonFile::fileExists($this->getZipFileOut())) {
68+
throw new \Exception('File '.$this->getZipFileOut().' does not exist');
7269
}
73-
$pImgFile = substr($this->getPath(), strpos($this->getPath(), '#') + 1);
7470
$oArchive = new \ZipArchive();
75-
$oArchive->open($pZIPFile);
71+
$oArchive->open($this->getZipFileOut());
7672
if (!function_exists('getimagesizefromstring')) {
77-
$uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($pImgFile));
73+
$uri = 'data://application/octet-stream;base64,' . base64_encode($oArchive->getFromName($this->getZipFileIn()));
7874
$image = getimagesize($uri);
7975
} else {
80-
$image = getimagesizefromstring($oArchive->getFromName($pImgFile));
76+
$image = getimagesizefromstring($oArchive->getFromName($this->getZipFileIn()));
8177
}
8278
return image_type_to_mime_type($image[2]);
8379
}
@@ -87,11 +83,25 @@ public function getMimeType()
8783
*/
8884
public function getIndexedFilename()
8985
{
90-
$output = substr($this->getPath(), strpos($this->getPath(), '#') + 1);
86+
$output = pathinfo($this->getZipFileIn(), PATHINFO_FILENAME);
9187
$output = str_replace('.' . $this->getExtension(), '', $output);
9288
$output .= $this->getImageIndex();
9389
$output .= '.'.$this->getExtension();
9490
$output = str_replace(' ', '_', $output);
9591
return $output;
9692
}
93+
94+
protected function getZipFileOut()
95+
{
96+
$path = str_replace('zip://', '', $this->getPath());
97+
$path = explode('#', $path);
98+
return empty($path[0]) ? '' : $path[0];
99+
}
100+
101+
protected function getZipFileIn()
102+
{
103+
$path = str_replace('zip://', '', $this->getPath());
104+
$path = explode('#', $path);
105+
return empty($path[1]) ? '' : $path[1];
106+
}
97107
}

0 commit comments

Comments
 (0)