Skip to content

Commit 004edd5

Browse files
committed
Merge pull request #169 from ivanlanin/#160-refactoring
#160 refactoring: Merge TableFull-Table Style + Merge MemoryImage-Image
2 parents 2d7126e + 3055a0e commit 004edd5

33 files changed

+1154
-1630
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ This release marked the transformation to namespaces (PHP 5.3+).
88

99
### Features
1010

11-
None yet.
11+
- Image: Ability to use remote or GD images using `addImage()` on sections, headers, footer, cells, and textruns - @ivanlanin
12+
- Header: Ability to use remote or GD images using `addWatermark()` - @ivanlanin
1213

1314
### Bugfixes
1415

@@ -21,6 +22,8 @@ None yet.
2122
- Use `PhpOffice\PhpWord` namespace for PSR compliance - @RomanSyroeshko @gabrielbull GH-159 GH-58
2223
- Restructure folders and change folder name `Classes` to `src` and `Tests` to `test` for PSR compliance - @RomanSyroeshko @gabrielbull
2324
- Compliance to phpDocumentor - @ivanlanin
25+
- Merge Style\TableFull into Style\Table. Style\TableFull is deprecated - @ivanlanin GH-160
26+
- Merge Section\MemoryImage into Section\Image. Section\Image is deprecated - @ivanlanin GH-160
2427

2528
## 0.8.1 - 17 Mar 2014
2629

docs/elements.rst

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,15 @@ See ``Sample_09_Tables.php`` for more code sample.
239239
Images
240240
------
241241

242-
To add an image, use the ``addImage`` or ``addMemoryImage`` method. The
243-
first one is used when your source is stored locally, the later is used
244-
when your source is a remote URL, either another script that create
245-
image or an image on the internet.
246-
247-
Syntax:
242+
To add an image, use the ``addImage`` method to sections, headers, footers,
243+
textruns, or table cells.
248244

249245
.. code-block:: php
250246
251247
$section->addImage($src, [$style]);
252-
$section->addMemoryImage($link, [$style]);
248+
249+
- `source` String path to a local image or URL of a remote image
250+
- `styles` Array fo styles for the image. See below.
253251

254252
Examples:
255253

@@ -266,9 +264,10 @@ Examples:
266264
'wrappingStyle' => 'behind'
267265
)
268266
);
269-
270-
$section->addMemoryImage('http://example.com/image.php');
271-
$section->addMemoryImage('http://php.net/logo.jpg');
267+
$footer = $section->createFooter();
268+
$footer->addImage('http://example.com/image.php');
269+
$textrun = $section->createTextRun();
270+
$textrun->addImage('http://php.net/logo.jpg');
272271
273272
Image styles
274273
~~~~~~~~~~~~
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* PHPWord
4+
*
5+
* Copyright (c) 2014 PHPWord
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation; either
10+
* version 2.1 of the License, or (at your option) any later version.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* @category PHPWord
22+
* @package PHPWord
23+
* @copyright Copyright (c) 2014 PHPWord
24+
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
25+
* @version 0.8.0
26+
*/
27+
namespace PhpOffice\PhpWord\Exceptions;
28+
29+
/**
30+
* Exception used for when an image is not found
31+
*/
32+
class InvalidObjectException extends Exception
33+
{
34+
}

src/PhpWord/Media.php

Lines changed: 42 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
namespace PhpOffice\PhpWord;
2727

28-
use PhpOffice\PhpWord\Section\MemoryImage;
28+
use PhpOffice\PhpWord\Section\Image;
2929

3030
/**
3131
* Media
@@ -69,81 +69,56 @@ class Media
6969
*
7070
* @param string $src
7171
* @param string $type
72-
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage
72+
* @param \PhpOffice\PhpWord\Section\Image $image
7373
* @return mixed
7474
*/
75-
public static function addSectionMediaElement($src, $type, MemoryImage $memoryImage = null)
75+
public static function addSectionMediaElement($src, $type, Image $image = null)
7676
{
7777
$mediaId = md5($src);
7878
$key = ($type === 'image') ? 'images' : 'embeddings';
79-
8079
if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) {
8180
$cImg = self::countSectionMediaElements('images');
8281
$cObj = self::countSectionMediaElements('embeddings');
8382
$rID = self::countSectionMediaElements() + 7;
84-
8583
$media = array();
86-
8784
$folder = null;
8885
$file = null;
8986
if ($type === 'image') {
9087
$cImg++;
91-
//Detect if it's a memory image first by php ext and second by regex
92-
$isMemImage = false;
93-
if (stripos(strrev($src), strrev('.php')) === 0) {
94-
$isMemImage = true;
95-
}
96-
if (!$isMemImage) {
97-
$isMemImage = (filter_var($src, \FILTER_VALIDATE_URL) !== false);
88+
if (!is_null($image)) {
89+
$isMemImage = $image->getIsMemImage();
90+
$extension = $image->getImageExtension();
91+
} else {
92+
$isMemImage = false;
9893
}
99-
$extension = '';
10094
if ($isMemImage) {
101-
$extension = $memoryImage->getImageExtension();
10295
$media['isMemImage'] = true;
103-
$media['createfunction'] = $memoryImage->getImageCreateFunction();
104-
$media['imagefunction'] = $memoryImage->getImageFunction();
105-
} else {
106-
$imageType = exif_imagetype($src);
107-
if ($imageType === \IMAGETYPE_JPEG) {
108-
$extension = 'jpg';
109-
} elseif ($imageType === \IMAGETYPE_GIF) {
110-
$extension = 'gif';
111-
} elseif ($imageType === \IMAGETYPE_PNG) {
112-
$extension = 'png';
113-
} elseif ($imageType === \IMAGETYPE_BMP) {
114-
$extension = 'bmp';
115-
} elseif ($imageType === \IMAGETYPE_TIFF_II || $imageType === \IMAGETYPE_TIFF_MM) {
116-
$extension = 'tif';
117-
}
96+
$media['createfunction'] = $image->getImageCreateFunction();
97+
$media['imagefunction'] = $image->getImageFunction();
11898
}
119-
12099
$folder = 'media';
121100
$file = $type . $cImg . '.' . strtolower($extension);
122101
} elseif ($type === 'oleObject') {
123102
$cObj++;
124103
$folder = 'embedding';
125104
$file = $type . $cObj . '.bin';
126105
}
127-
128106
$media['source'] = $src;
129107
$media['target'] = "$folder/section_$file";
130108
$media['type'] = $type;
131109
$media['rID'] = $rID;
132-
133110
self::$_sectionMedia[$key][$mediaId] = $media;
134-
135111
if ($type === 'oleObject') {
136112
return array($rID, ++self::$_objectId);
137113
}
138-
139114
return $rID;
115+
} else {
116+
if ($type === 'oleObject') {
117+
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
118+
return array($rID, ++self::$_objectId);
119+
}
120+
return self::$_sectionMedia[$key][$mediaId]['rID'];
140121
}
141-
142-
if ($type === 'oleObject') {
143-
$rID = self::$_sectionMedia[$key][$mediaId]['rID'];
144-
return array($rID, ++self::$_objectId);
145-
}
146-
return self::$_sectionMedia[$key][$mediaId]['rID'];
147122
}
148123

149124
/**
@@ -207,50 +182,42 @@ public static function countSectionMediaElements($key = null)
207182
*
208183
* @param int $headerCount
209184
* @param string $src
210-
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage
185+
* @param \PhpOffice\PhpWord\Section\Image $image
211186
* @return int
212187
*/
213-
public static function addHeaderMediaElement($headerCount, $src, MemoryImage $memoryImage = null)
188+
public static function addHeaderMediaElement($headerCount, $src, Image $image = null)
214189
{
215190
$mediaId = md5($src);
216191
$key = 'header' . $headerCount;
217-
218192
if (!array_key_exists($key, self::$_headerMedia)) {
219193
self::$_headerMedia[$key] = array();
220194
}
221-
222195
if (!array_key_exists($mediaId, self::$_headerMedia[$key])) {
223196
$cImg = self::countHeaderMediaElements($key);
224197
$rID = $cImg + 1;
225-
226198
$cImg++;
227-
$inf = pathinfo($src);
228-
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
229-
230199
$media = array();
200+
if (!is_null($image)) {
201+
$isMemImage = $image->getIsMemImage();
202+
$extension = $image->getImageExtension();
203+
} else {
204+
$isMemImage = false;
205+
}
231206
if ($isMemImage) {
232-
$ext = $memoryImage->getImageExtension();
233207
$media['isMemImage'] = true;
234-
$media['createfunction'] = $memoryImage->getImageCreateFunction();
235-
$media['imagefunction'] = $memoryImage->getImageFunction();
236-
} else {
237-
$ext = $inf['extension'];
238-
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
239-
$ext = 'jpg';
240-
}
208+
$media['createfunction'] = $image->getImageCreateFunction();
209+
$media['imagefunction'] = $image->getImageFunction();
241210
}
242-
$file = 'image' . $cImg . '.' . strtolower($ext);
243-
211+
$file = 'image' . $cImg . '.' . strtolower($extension);
244212
$media['source'] = $src;
245213
$media['target'] = 'media/' . $key . '_' . $file;
246214
$media['type'] = 'image';
247215
$media['rID'] = $rID;
248-
249216
self::$_headerMedia[$key][$mediaId] = $media;
250-
251217
return $rID;
218+
} else {
219+
return self::$_headerMedia[$key][$mediaId]['rID'];
252220
}
253-
return self::$_headerMedia[$key][$mediaId]['rID'];
254221
}
255222

256223
/**
@@ -279,50 +246,41 @@ public static function getHeaderMediaElements()
279246
*
280247
* @param int $footerCount
281248
* @param string $src
282-
* @param \PhpOffice\PhpWord\Section\MemoryImage|null $memoryImage
249+
* @param \PhpOffice\PhpWord\Section\Image $image
283250
* @return int
284251
*/
285-
public static function addFooterMediaElement($footerCount, $src, MemoryImage $memoryImage = null)
252+
public static function addFooterMediaElement($footerCount, $src, Image $image = null)
286253
{
287254
$mediaId = md5($src);
288255
$key = 'footer' . $footerCount;
289-
290256
if (!array_key_exists($key, self::$_footerMedia)) {
291257
self::$_footerMedia[$key] = array();
292258
}
293-
294259
if (!array_key_exists($mediaId, self::$_footerMedia[$key])) {
295260
$cImg = self::countFooterMediaElements($key);
296261
$rID = $cImg + 1;
297-
298262
$cImg++;
299-
$inf = pathinfo($src);
300-
$isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php') ? true : false;
301-
302-
$media = array();
263+
if (!is_null($image)) {
264+
$isMemImage = $image->getIsMemImage();
265+
$extension = $image->getImageExtension();
266+
} else {
267+
$isMemImage = false;
268+
}
303269
if ($isMemImage) {
304-
$ext = $memoryImage->getImageExtension();
305270
$media['isMemImage'] = true;
306-
$media['createfunction'] = $memoryImage->getImageCreateFunction();
307-
$media['imagefunction'] = $memoryImage->getImageFunction();
308-
} else {
309-
$ext = $inf['extension'];
310-
if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg
311-
$ext = 'jpg';
312-
}
271+
$media['createfunction'] = $image->getImageCreateFunction();
272+
$media['imagefunction'] = $image->getImageFunction();
313273
}
314-
$file = 'image' . $cImg . '.' . strtolower($ext);
315-
274+
$file = 'image' . $cImg . '.' . strtolower($extension);
316275
$media['source'] = $src;
317276
$media['target'] = 'media/' . $key . '_' . $file;
318277
$media['type'] = 'image';
319278
$media['rID'] = $rID;
320-
321279
self::$_footerMedia[$key][$mediaId] = $media;
322-
323280
return $rID;
281+
} else {
282+
return self::$_footerMedia[$key][$mediaId]['rID'];
324283
}
325-
return self::$_footerMedia[$key][$mediaId]['rID'];
326284
}
327285

328286
/**

0 commit comments

Comments
 (0)