Skip to content

Commit 9c738f7

Browse files
committed
Refactor Element\Image and some samples
1 parent 04b14ea commit 9c738f7

File tree

4 files changed

+114
-65
lines changed

4 files changed

+114
-65
lines changed

samples/Sample_07_TemplateCloneRow.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@
6060
$document->saveAs($name);
6161
rename($name, "results/{$name}");
6262

63-
include_once 'Sample_Footer.php';
63+
$writers = array('Word2007' => 'docx');
64+
echo getEndingNotes($writers);
65+
if (!CLI) {
66+
include_once 'Sample_Footer.php';
67+
}

samples/Sample_23_TemplateBlock.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@
1818
$document->saveAs($name);
1919
rename($name, "results/{$name}");
2020

21-
include_once 'Sample_Footer.php';
21+
$writers = array('Word2007' => 'docx');
22+
echo getEndingNotes($writers);
23+
if (!CLI) {
24+
include_once 'Sample_Footer.php';
25+
}

samples/Sample_Header.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,17 @@
4646
}
4747

4848
/**
49-
* Get results
49+
* Write documents
5050
*
5151
* @param \PhpOffice\PhpWord\PhpWord $phpWord
5252
* @param string $filename
5353
* @param array $writers
54-
* @return string
5554
*/
5655
function write($phpWord, $filename, $writers)
5756
{
5857
$result = '';
5958

60-
// Write
59+
// Write documents
6160
foreach ($writers as $writer => $extension) {
6261
$result .= date('H:i:s') . " Write to {$writer} format";
6362
if (!is_null($extension)) {
@@ -70,6 +69,20 @@ function write($phpWord, $filename, $writers)
7069
$result .= EOL;
7170
}
7271

72+
$result .= getEndingNotes($writers);
73+
74+
return $result;
75+
}
76+
77+
/**
78+
* Get ending notes
79+
*
80+
* @param array $writers
81+
*/
82+
function getEndingNotes($writers)
83+
{
84+
$result = '';
85+
7386
// Do not show execution time for index
7487
if (!IS_INDEX) {
7588
$result .= date('H:i:s') . " Done writing file(s)" . EOL;

src/PhpWord/Element/Image.php

Lines changed: 88 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,27 @@
1818
*/
1919
class Image extends AbstractElement
2020
{
21+
/**
22+
* Image source type constants
23+
*/
24+
const SOURCE_LOCAL = 'local'; // Local images
25+
const SOURCE_GD = 'gd'; // Generated using GD
26+
const SOURCE_ARCHIVE = 'archive'; // Image in archives zip://$archive#$image
27+
2128
/**
2229
* Image source
2330
*
2431
* @var string
2532
*/
2633
private $source;
2734

35+
/**
36+
* Source type: local|gd|archive
37+
*
38+
* @var string
39+
*/
40+
private $sourceType;
41+
2842
/**
2943
* Image style
3044
*
@@ -199,49 +213,87 @@ public function getIsMemImage()
199213
*/
200214
private function checkImage($source)
201215
{
202-
$isArchive = strpos($source, 'zip://') !== false;
216+
$this->setSourceType($source);
203217

204-
// Check is memory image
205-
if (stripos(strrev($source), strrev('.php')) === 0) {
206-
$this->isMemImage = true;
207-
} elseif ($isArchive) {
208-
$this->isMemImage = false;
209-
} else {
210-
$this->isMemImage = (filter_var($source, FILTER_VALIDATE_URL) !== false);
211-
}
212-
213-
// Define supported types
214-
if ($this->isMemImage) {
215-
$supportedTypes = array(
216-
IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG
217-
);
218-
} else {
219-
$supportedTypes = array(
220-
IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG,
221-
IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM
222-
);
223-
}
224-
225-
// Check from zip file or actual file
226-
if ($isArchive) {
227-
$imageData = $this->getArchivedImageSize($source);
218+
// Check image data
219+
if ($this->sourceType == self::SOURCE_ARCHIVE) {
220+
$imageData = $this->getArchiveImageSize($source);
228221
} else {
229222
$imageData = @getimagesize($source);
230223
}
231-
232-
// Check if image exists by detecting image data
233224
if (!is_array($imageData)) {
234225
throw new InvalidImageException();
235226
}
236-
// Put image data into variables
237227
list($actualWidth, $actualHeight, $imageType) = $imageData;
238-
// Check if image type is supported
228+
229+
// Check image type support
230+
$supportedTypes = array(IMAGETYPE_JPEG, IMAGETYPE_GIF, IMAGETYPE_PNG);
231+
if ($this->sourceType != self::SOURCE_GD) {
232+
$supportedTypes = array_merge($supportedTypes, array(IMAGETYPE_BMP, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM));
233+
}
239234
if (!in_array($imageType, $supportedTypes)) {
240235
throw new UnsupportedImageTypeException();
241236
}
242237

243238
// Define image functions
244239
$this->imageType = image_type_to_mime_type($imageType);
240+
$this->setFunctions();
241+
$this->setProportionalSize($actualWidth, $actualHeight);
242+
}
243+
244+
/**
245+
* Set source type
246+
*
247+
* @param string $source
248+
*/
249+
private function setSourceType($source)
250+
{
251+
if (stripos(strrev($source), strrev('.php')) === 0) {
252+
$this->isMemImage = true;
253+
$this->sourceType = self::SOURCE_GD;
254+
} elseif (strpos($source, 'zip://') !== false) {
255+
$this->isMemImage = false;
256+
$this->sourceType = self::SOURCE_ARCHIVE;
257+
} else {
258+
$this->isMemImage = (filter_var($source, FILTER_VALIDATE_URL) !== false);
259+
$this->sourceType = $this->isMemImage ? self::SOURCE_GD : self::SOURCE_LOCAL;
260+
}
261+
}
262+
263+
/**
264+
* Get image size from archive
265+
*
266+
* @param string $source
267+
* @return array|null
268+
*/
269+
private function getArchiveImageSize($source)
270+
{
271+
$imageData = null;
272+
$source = substr($source, 6);
273+
list($zipFilename, $imageFilename) = explode('#', $source);
274+
$tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
275+
276+
$zip = new \ZipArchive();
277+
if ($zip->open($zipFilename) !== false) {
278+
if ($zip->locateName($imageFilename)) {
279+
$imageContent = $zip->getFromName($imageFilename);
280+
if ($imageContent !== false) {
281+
file_put_contents($tempFilename, $imageContent);
282+
$imageData = @getimagesize($tempFilename);
283+
unlink($tempFilename);
284+
}
285+
}
286+
$zip->close();
287+
}
288+
289+
return $imageData;
290+
}
291+
292+
/**
293+
* Set image functions and extensions
294+
*/
295+
private function setFunctions()
296+
{
245297
switch ($this->imageType) {
246298
case 'image/png':
247299
$this->imageCreateFunc = 'imagecreatefrompng';
@@ -254,6 +306,7 @@ private function checkImage($source)
254306
$this->imageExtension = 'gif';
255307
break;
256308
case 'image/jpeg':
309+
case 'image/jpg':
257310
$this->imageCreateFunc = 'imagecreatefromjpeg';
258311
$this->imageFunc = 'imagejpeg';
259312
$this->imageExtension = 'jpg';
@@ -267,8 +320,13 @@ private function checkImage($source)
267320
$this->imageExtension = 'tif';
268321
break;
269322
}
323+
}
270324

271-
// Check image width & height
325+
/**
326+
* Set proportional width/height if one dimension not available
327+
*/
328+
private function setProportionalSize($actualWidth, $actualHeight)
329+
{
272330
$styleWidth = $this->style->getWidth();
273331
$styleHeight = $this->style->getHeight();
274332
if (!($styleWidth && $styleHeight)) {
@@ -281,35 +339,5 @@ private function checkImage($source)
281339
$this->style->setWidth($actualWidth * ($styleHeight / $actualHeight));
282340
}
283341
}
284-
285-
}
286-
287-
/**
288-
* Get image size from archive
289-
*
290-
* @param string $source
291-
* @return array|null
292-
*/
293-
private function getArchivedImageSize($source)
294-
{
295-
$imageData = null;
296-
$source = substr($source, 6);
297-
list($zipFilename, $imageFilename) = explode('#', $source);
298-
$tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
299-
300-
$zip = new \ZipArchive();
301-
if ($zip->open($zipFilename) !== false) {
302-
if ($zip->locateName($imageFilename)) {
303-
$imageContent = $zip->getFromName($imageFilename);
304-
if ($imageContent !== false) {
305-
file_put_contents($tempFilename, $imageContent);
306-
$imageData = @getimagesize($tempFilename);
307-
unlink($tempFilename);
308-
}
309-
}
310-
$zip->close();
311-
}
312-
313-
return $imageData;
314342
}
315343
}

0 commit comments

Comments
 (0)