Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
$newRelationsTpl = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n" . '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>';
$newRelationsTypeTpl = '<Override PartName="/{RELS}" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>';
$extTransform = array(
'jpg' => 'jpeg',
'JPG' => 'jpeg',
'png' => 'png',
'PNG' => 'png',
);
'image/jpeg' => 'jpeg',
'image/png' => 'png',
'image/bmp' => 'bmp',
'image/gif' => 'gif'
);

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

// get image path and size
$width = 115;
$height = 70;
if (is_array($replace) && isset($replace['path'])) {
$imgPath = $replace['path'];
if (isset($replace['width'])) {
Expand All @@ -339,6 +337,20 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
$imgPath = $replace;
}

$imageData = @getimagesize($imgPath);
if (!is_array($imageData)) {
throw new Exception(sprintf('Invalid image: %s', $imgPath));
}
list($actualWidth, $actualHeight, $imageType) = $imageData;
$imageMimeType = image_type_to_mime_type($imageType);

if(!isset($width)) {
$width = $actualWidth;
}
if(!isset($height)) {
$height = $actualHeight;
}

// get image index
$imgIndex = $this->getNextRelationsIndex($partFileName);
$rid = 'rId' . $imgIndex;
Expand All @@ -348,9 +360,10 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
$imgName = $this->tempDocumentNewImages[$imgPath];
} else {
// transform extension
$imgExt = pathinfo($imgPath, PATHINFO_EXTENSION);
if (isset($extTransform)) {
$imgExt = $extTransform[$imgExt];
if (isset($extTransform[$imageMimeType])) {
$imgExt = $extTransform[$imageMimeType];
} else {
throw new Exception("Unsupported image type $imageMimeType");
}

// add image to document
Expand Down