Skip to content

Commit ff512f4

Browse files
authored
Merge pull request #1 from gatis-ozols/template_processor__set_image_value
Changed logic that determines the extension of image file
2 parents ae27499 + 6f63982 commit ff512f4

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,11 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
299299
$newRelationsTpl = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n" . '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>';
300300
$newRelationsTypeTpl = '<Override PartName="/{RELS}" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>';
301301
$extTransform = array(
302-
'jpg' => 'jpeg',
303-
'JPG' => 'jpeg',
304-
'png' => 'png',
305-
'PNG' => 'png',
306-
);
302+
'image/jpeg' => 'jpeg',
303+
'image/png' => 'png',
304+
'image/bmp' => 'bmp',
305+
'image/gif' => 'gif'
306+
);
307307

308308
$searchParts = array(
309309
$this->getMainPartName() => &$this->tempDocumentMainPart,
@@ -325,8 +325,6 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
325325
}
326326

327327
// get image path and size
328-
$width = 115;
329-
$height = 70;
330328
if (is_array($replace) && isset($replace['path'])) {
331329
$imgPath = $replace['path'];
332330
if (isset($replace['width'])) {
@@ -339,6 +337,20 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
339337
$imgPath = $replace;
340338
}
341339

340+
$imageData = @getimagesize($imgPath);
341+
if (!is_array($imageData)) {
342+
throw new Exception(sprintf('Invalid image: %s', $imgPath));
343+
}
344+
list($actualWidth, $actualHeight, $imageType) = $imageData;
345+
$imageMimeType = image_type_to_mime_type($imageType);
346+
347+
if(!isset($width)) {
348+
$width = $actualWidth;
349+
}
350+
if(!isset($height)) {
351+
$height = $actualHeight;
352+
}
353+
342354
// get image index
343355
$imgIndex = $this->getNextRelationsIndex($partFileName);
344356
$rid = 'rId' . $imgIndex;
@@ -348,9 +360,10 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
348360
$imgName = $this->tempDocumentNewImages[$imgPath];
349361
} else {
350362
// transform extension
351-
$imgExt = pathinfo($imgPath, PATHINFO_EXTENSION);
352-
if (isset($extTransform)) {
353-
$imgExt = $extTransform[$imgExt];
363+
if (isset($extTransform[$imageMimeType])) {
364+
$imgExt = $extTransform[$imageMimeType];
365+
} else {
366+
throw new Exception("Unsupported image type $imageMimeType");
354367
}
355368

356369
// add image to document

0 commit comments

Comments
 (0)