如何在PHPOffice/PHPWord模板上添加/设置图像? [英] How to add/set images on PHPOffice/PHPWord Template?
本文介绍了如何在PHPOffice/PHPWord模板上添加/设置图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在PHPWord上有一个模板实例.是否可以替换或添加图像?像setImageValue一样?
I have an instance of a template on PHPWord. Is it possible to replace or add an image? Something like a setImageValue?
$phpWord = new \PhpOffice\PhpWord\Template('a.docx');
$phpWord->setImageValue('IMAGE_PLACEHOLDER', 'a.jpg');
$phpWord->saveAs('b.docx');
这样可能吗?
推荐答案
以下代码是TotPeRo的更新版本(再次感谢您的代码!),对于最新的phpOffice(0.11)有所发展>
Following code is the updated version of the one from TotPeRo (thanks again for your code!), for last phpOffice (0.11) that has evolved a little
/**
* Set a new image
*
* @param string $search
* @param string $replace
*/
public function setImageValue($search, $replace)
{
// Sanity check
if (!file_exists($replace))
{
return;
}
// Delete current image
$this->zipClass->deleteName('word/media/' . $search);
// Add a new one
$this->zipClass->addFile($replace, 'word/media/' . $search);
}
可以通过以下方式调用:
Can be called with:
$document->setImageValue('image1.jpg', 'my_image.jpg');
这篇关于如何在PHPOffice/PHPWord模板上添加/设置图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文