Skip to content

Commit 5038b3b

Browse files
committed
more reduce methods size
1 parent 3f87352 commit 5038b3b

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

src/PhpWord/TemplateProcessor.php

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,55 @@ private function prepareImageAttrs($replaceImage, $varInlineArgs)
403403
return $imageAttrs;
404404
}
405405

406+
private function addImageToRelations($partFileName, $rid, $imgPath, $imageMimeType)
407+
{
408+
// define templates
409+
$typeTpl = '<Override PartName="/word/media/{IMG}" ContentType="image/{EXT}"/>';
410+
$relationTpl = '<Relationship Id="{RID}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/{IMG}"/>';
411+
$newRelationsTpl = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n" . '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>';
412+
$newRelationsTypeTpl = '<Override PartName="/{RELS}" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>';
413+
$extTransform = array(
414+
'image/jpeg' => 'jpeg',
415+
'image/png' => 'png',
416+
'image/bmp' => 'bmp',
417+
'image/gif' => 'gif',
418+
);
419+
420+
// get image embed name
421+
if (isset($this->tempDocumentNewImages[$imgPath])) {
422+
$imgName = $this->tempDocumentNewImages[$imgPath];
423+
} else {
424+
// transform extension
425+
if (isset($extTransform[$imageMimeType])) {
426+
$imgExt = $extTransform[$imageMimeType];
427+
} else {
428+
throw new Exception("Unsupported image type $imageMimeType");
429+
}
430+
431+
// add image to document
432+
$imgName = 'image_' . $rid . '_' . pathinfo($partFileName, PATHINFO_FILENAME) . '.' . $imgExt;
433+
$this->zipClass->pclzipAddFile($imgPath, 'word/media/' . $imgName);
434+
$this->tempDocumentNewImages[$imgPath] = $imgName;
435+
436+
// setup type for image
437+
$xmlImageType = str_replace(array('{IMG}', '{EXT}'), array($imgName, $imgExt), $typeTpl);
438+
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlImageType, $this->tempDocumentContentTypes) . '</Types>';
439+
}
440+
441+
$xmlImageRelation = str_replace(array('{RID}', '{IMG}'), array($rid, $imgName), $relationTpl);
442+
443+
if (!isset($this->tempDocumentRelations[$partFileName])) {
444+
// create new relations file
445+
$this->tempDocumentRelations[$partFileName] = $newRelationsTpl;
446+
// and add it to content types
447+
$xmlRelationsType = str_replace('{RELS}', $this->getRelationsName($partFileName), $newRelationsTypeTpl);
448+
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlRelationsType, $this->tempDocumentContentTypes) . '</Types>';
449+
}
450+
451+
// add image to relations
452+
$this->tempDocumentRelations[$partFileName] = str_replace('</Relationships>', $xmlImageRelation, $this->tempDocumentRelations[$partFileName]) . '</Relationships>';
453+
}
454+
406455
/**
407456
* @param mixed $search
408457
* @param mixed $replace Path to image, or array("path" => xx, "width" => yy, "height" => zz)
@@ -427,20 +476,7 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
427476
$searchReplace[$searchString] = isset($replacesList[$searchIdx]) ? $replacesList[$searchIdx] : $replacesList[0];
428477
}
429478

430-
// define templates
431-
// result can be verified via "Open XML SDK 2.5 Productivity Tool" (http://www.microsoft.com/en-us/download/details.aspx?id=30425)
432-
$imgTpl = '<w:pict><v:shape type="#_x0000_t75" style="width:{WIDTH};height:{HEIGHT}"><v:imagedata r:id="{RID}" o:title=""/></v:shape></w:pict>';
433-
$typeTpl = '<Override PartName="/word/media/{IMG}" ContentType="image/{EXT}"/>';
434-
$relationTpl = '<Relationship Id="{RID}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/{IMG}"/>';
435-
$newRelationsTpl = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n" . '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"></Relationships>';
436-
$newRelationsTypeTpl = '<Override PartName="/{RELS}" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>';
437-
$extTransform = array(
438-
'image/jpeg' => 'jpeg',
439-
'image/png' => 'png',
440-
'image/bmp' => 'bmp',
441-
'image/gif' => 'gif',
442-
);
443-
479+
// collect document parts
444480
$searchParts = array(
445481
$this->getMainPartName() => &$this->tempDocumentMainPart,
446482
);
@@ -451,6 +487,10 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
451487
$searchParts[$this->getFooterName($headerIndex)] = &$this->tempDocumentFooters[$headerIndex];
452488
}
453489

490+
// define templates
491+
// result can be verified via "Open XML SDK 2.5 Productivity Tool" (http://www.microsoft.com/en-us/download/details.aspx?id=30425)
492+
$imgTpl = '<w:pict><v:shape type="#_x0000_t75" style="width:{WIDTH};height:{HEIGHT}"><v:imagedata r:id="{RID}" o:title=""/></v:shape></w:pict>';
493+
454494
foreach ($searchParts as $partFileName => &$partContent) {
455495
$partVariables = $this->getVariablesForPart($partContent);
456496

@@ -463,46 +503,14 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM
463503
$varInlineArgs = $this->getImageArgs($varNameWithArgs);
464504
$preparedImageAttrs = $this->prepareImageAttrs($replaceImage, $varInlineArgs);
465505
$imgPath = $preparedImageAttrs['src'];
466-
$imageMimeType = $preparedImageAttrs['mime'];
467506

468507
// get image index
469508
$imgIndex = $this->getNextRelationsIndex($partFileName);
470509
$rid = 'rId' . $imgIndex;
471510

472-
// get image embed name
473-
if (isset($this->tempDocumentNewImages[$imgPath])) {
474-
$imgName = $this->tempDocumentNewImages[$imgPath];
475-
} else {
476-
// transform extension
477-
if (isset($extTransform[$imageMimeType])) {
478-
$imgExt = $extTransform[$imageMimeType];
479-
} else {
480-
throw new Exception("Unsupported image type $imageMimeType");
481-
}
482-
483-
// add image to document
484-
$imgName = 'image' . $imgIndex . '_' . pathinfo($partFileName, PATHINFO_FILENAME) . '.' . $imgExt;
485-
$this->zipClass->pclzipAddFile($imgPath, 'word/media/' . $imgName);
486-
$this->tempDocumentNewImages[$imgPath] = $imgName;
487-
488-
// setup type for image
489-
$xmlImageType = str_replace(array('{IMG}', '{EXT}'), array($imgName, $imgExt), $typeTpl);
490-
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlImageType, $this->tempDocumentContentTypes) . '</Types>';
491-
}
492-
511+
// replace preparations
512+
$this->addImageToRelations($partFileName, $rid, $imgPath, $preparedImageAttrs['mime']);
493513
$xmlImage = str_replace(array('{RID}', '{WIDTH}', '{HEIGHT}'), array($rid, $preparedImageAttrs['width'], $preparedImageAttrs['height']), $imgTpl);
494-
$xmlImageRelation = str_replace(array('{RID}', '{IMG}'), array($rid, $imgName), $relationTpl);
495-
496-
if (!isset($this->tempDocumentRelations[$partFileName])) {
497-
// create new relations file
498-
$this->tempDocumentRelations[$partFileName] = $newRelationsTpl;
499-
// and add it to content types
500-
$xmlRelationsType = str_replace('{RELS}', $this->getRelationsName($partFileName), $newRelationsTypeTpl);
501-
$this->tempDocumentContentTypes = str_replace('</Types>', $xmlRelationsType, $this->tempDocumentContentTypes) . '</Types>';
502-
}
503-
504-
// add image to relations
505-
$this->tempDocumentRelations[$partFileName] = str_replace('</Relationships>', $xmlImageRelation, $this->tempDocumentRelations[$partFileName]) . '</Relationships>';
506514

507515
// replace variable
508516
$varNameWithArgsFixed = self::ensureMacroCompleted($varNameWithArgs);

0 commit comments

Comments
 (0)