From 335c3df0ebc668611adc8ef734cad59c64a3c38b Mon Sep 17 00:00:00 2001 From: Vu Thai Hoc Date: Mon, 20 Apr 2020 17:02:08 +0700 Subject: [PATCH] convert OleObject to image from imagedata (wmf) --- composer.json | 4 ++- src/PhpWord/Reader/Word2007/AbstractPart.php | 23 ++++++++++++++--- src/PhpWord/Shared/WmfToGif.php | 27 ++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 src/PhpWord/Shared/WmfToGif.php diff --git a/composer.json b/composer.json index f5f751ec04..d42757cf3d 100644 --- a/composer.json +++ b/composer.json @@ -60,8 +60,10 @@ "require": { "php": "^5.3.3 || ^7.0", "ext-xml": "*", + "ext-imagick": "*", "zendframework/zend-escaper": "^2.2", - "phpoffice/common": "^0.2.9" + "phpoffice/common": "^0.2.9", + "intervention/image": "^2.5" }, "require-dev": { "ext-zip": "*", diff --git a/src/PhpWord/Reader/Word2007/AbstractPart.php b/src/PhpWord/Reader/Word2007/AbstractPart.php index bb4a3a4944..b41e6ca905 100644 --- a/src/PhpWord/Reader/Word2007/AbstractPart.php +++ b/src/PhpWord/Reader/Word2007/AbstractPart.php @@ -17,12 +17,14 @@ namespace PhpOffice\PhpWord\Reader\Word2007; +use PhpOffice\Common\File; use PhpOffice\Common\XMLReader; use PhpOffice\PhpWord\ComplexType\TblWidth as TblWidthComplexType; use PhpOffice\PhpWord\Element\AbstractContainer; use PhpOffice\PhpWord\Element\TextRun; use PhpOffice\PhpWord\Element\TrackChange; use PhpOffice\PhpWord\PhpWord; +use PhpOffice\PhpWord\Shared\WmfToGif; /** * Abstract part reader @@ -273,16 +275,31 @@ protected function readRunChild(XMLReader $xmlReader, \DOMElement $node, Abstrac $target = $this->getMediaTarget($docPart, $embedId); if (!is_null($target)) { $imageSource = "zip://{$this->docFile}#{$target}"; - $parent->addImage($imageSource, null, false, $name); + try{ + $parent->addImage($imageSource, null, false, $name); + }catch (\Exception $ex){ + echo "Lỗi ảnh " . $ex->getMessage() . "\n"; + } } } elseif ($node->nodeName == 'w:object') { // Object $rId = $xmlReader->getAttribute('r:id', $node, 'o:OLEObject'); + $rIdWmf = $xmlReader->getAttribute('r:id', $node, 'v:shape/v:imagedata'); // $rIdIcon = $xmlReader->getAttribute('r:id', $domNode, 'w:object/v:shape/v:imagedata'); $target = $this->getMediaTarget($docPart, $rId); + $target_wmf = $this->getMediaTarget($docPart, $rIdWmf); if (!is_null($target)) { - $textContent = "<Object: {$target}>"; - $parent->addText($textContent, $fontStyle, $paragraphStyle); + try{ + if(is_null( $target_wmf )){ + throw new \Exception("OOO"); + } + $image = WmfToGif::convert( File::fileGetContents( "zip://" . $this->docFile . "#" . $target_wmf)); + $parent->addImage($image->__toString(), null, false, basename( $target_wmf )); + }catch (\Exception $ex){ + echo "Lỗi wmf " . $ex->getMessage(); + $textContent = "<Object: {$target}>"; + $parent->addText($textContent, $fontStyle, $paragraphStyle); + } } } elseif ($node->nodeName == 'w:br') { $parent->addTextBreak(); diff --git a/src/PhpWord/Shared/WmfToGif.php b/src/PhpWord/Shared/WmfToGif.php new file mode 100644 index 0000000000..e19ac8df0e --- /dev/null +++ b/src/PhpWord/Shared/WmfToGif.php @@ -0,0 +1,27 @@ + 'imagick']))->make($tmp); + $image = $image->resize(null, $image->getHeight() * 2, function ($constraint) { + $constraint->aspectRatio(); + })->encode('png', 100);//->__toString(); + unlink( $tmp ); + return $image; + } + +} \ No newline at end of file