简体   繁体   English

phpspreadsheet如何替换模板中的图像

[英]phpspreadsheet how to replace an image in a template

Is it possible to swap out images in existing template file?是否可以换出现有模板文件中的图像?

In my excel xlsx template file are three images.在我的 excel xlsx 模板文件中有三个图像。 Each image has its own unique name/title (IE: "imageBanner_1").每个图像都有自己唯一的名称/标题(即:“imageBanner_1”)。 Each image is in a location in reference to where certain charts are on the worksheet - but not necessarily bound to a specific cell.每个图像的位置都与工作表上某些图表的位置有关 - 但不一定绑定到特定单元格。

Based on using the image name, is it possible to replace the image with another so that the new image is in the same location as the original and retains all of the original image properties (shadow, width/length, name, etc)?基于使用图像名称,是否可以将图像替换为另一个图像,以便新图像与原始图像位于同一位置并保留所有原始图像属性(阴影、宽度/长度、名称等)?

This is the method I have tried but has not worked:这是我尝试过但没有奏效的方法:

  $fImages = $speadsheet->getSheetByName("Chart Data")->getDrawingCollection();
  foreach ($fImages as $fImage) {
    echo "\n".$fImage->getName() ;
    if ($fImage->getName() == "imageBanner_1") {
      $fImage->setPath("path/to/new/file/fileName.png") ;
    }
  }

How can I get all the properties of $fImage ?如何获取$fImage的所有属性? Maybe one of them is the correct name that will match on imageBanner_1也许其中一个是与imageBanner_1匹配的正确名称

In my excel templates, I have bound the names (in Formulas -> Name manager) as:在我的 excel 模板中,我将名称(在公式 -> 名称管理器中)绑定为:

Name             Value         Refers To
imageBanner_1    Picture 9     =*Picture 9*
imageBanner_2    Picture 18    =*Picture 18*
imageBanner_3    Picture 21    =*Picture 21*

Doing the echo prints out:echo打印出来:

Picture 9
Picture 18
Picture 21

Which tells me the getName() is either a reference to the value OR getName() is actually Picture X .这告诉我getName()要么是对值的引用,要么getName()实际上是Picture X Either way, how can I use the imageBanner_X names as the identifier?无论哪种方式,我如何使用imageBanner_X名称作为标识符? I don't know if my code to simply replace the image with a setPath will work, because at least for now I can't actually get to the image because of the getName() issue.我不知道我的代码是否可以简单地用setPath替换图像,因为至少现在由于getName()问题,我实际上无法访问图像。

======== UPDATE ====== ======== 更新 ======

Well, my above code works IF I change:好吧,如果我改变,我上面的代码就可以工作:

if ($fImage->getName() == "imageBanner_1") {

to

if ($fImage->getName() == "Picture 13") {

...but the problem with this is I have to know the exact picture number I am replacing. ...但问题是我必须知道要替换的确切图片编号。 This is not ideal as the picture number is not nessissarily a static number all the time.这并不理想,因为图片编号始终不是 static 编号。 In some cases I am replicating charts or images, in others I am replacing...esp when it comes to replicating it will be quite challenging to know the exact picture number each time I run the script with different data sets.在某些情况下,我正在复制图表或图像,在其他情况下,我正在替换......尤其是在复制时,每次我使用不同的数据集运行脚本时,要知道确切的图片编号将非常具有挑战性。

Well, this took some time, but I finally got it work.好吧,这花了一些时间,但我终于成功了。

$mainTempName = "$baseDir/reports/MainReportTemplate.xlsx" ;
$mainTempReader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx") ;
$mainTempFile = $mainTempReader->load($mainTempName, \PhpOffice\PhpSpreadsheet\Reader\IReader::LOAD_WITH_CHARTS) ;

$mainTempFile = swapImage($mainTempFile,$localFilePath,$excelImageName,$localFileName) ;

function swapImage($inFile,$iPath,$iName,$iFile) {
  foreach ($inFile->getDefinedNames() as $name) {
    if ($name->getName() == $iName) {
      $picID = str_replace("\"","",$name->getValue()) ;
    }
  }

  foreach ($inFile->getSheetByName("Chart Data")->getDrawingCollection() as $fImage) {
    if ($fImage->getName() == $picID) {
      $fImage->setPath($iPath.$iFile) ;
    }
  }
  return $inFile ;
}

$name->getValue() outputs value in quotes "Picture 9" , thus the need to use str_replace to strip out the " s as the $fImage->getName() value does not have quotes. $name->getValue() 在引号"Picture 9"中输出值,因此需要使用 str_replace 去除" s,因为$fImage->getName()值没有引号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM