PHPword setImageValue 设置高度不生效

phpword 版本   "phpoffice/phpword": "^0.18.3

我正在使用这个代码,使用模板,写入动态图片,但问题是图像的大小太小,我需要增加高度和宽度

模板文件 如下

PHPword setImageValue 设置高度不生效_第1张图片

问题写法:

$file_name = "简历";
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('/tmp.docx');
$image_path = "/opt/1.jpg"
$templateProcessor->setImageValue('img1',$image_path);
$templateProcessor->saveAs('docx/'.$file_name.'.docx');

直接这样写,图片显示不正常,图片高度很低,怎么办呢?

如果版本是 "phpoffice/phpword": "^0.18.3  解决代码如下:

$templateProcessor->setImageValue('img1', [
  'path' => $image_path,
  'width' => 600,
  'height' => 400,
]);

若还不行,则新加一个参数 ratio  为false  代码如下:

$templateProcessor->setImageValue('img1', [
  'path' => $image_path,
  'width' => 600,
  'height' => 400,
  'align'=>'center',
  'ratio'=>false
]);

如果版本是 "phpoffice/phpword": "^1.1    

PHPword setImageValue 设置高度不生效_第2张图片

vim composer.json

PHPword setImageValue 设置高度不生效_第3张图片

代码如下:

setValue('name','李四');//替换变量name
//$tmp->setValue('mobile','18888888888');//替换变量mobile
$a_arr = [
     'path'=>'/opt/phpword/1.jpg',
     'width'=>'300',
     'height'=>450
];
$tmp->setImageValue('img1',$a_arr);//替换变量mobile
$tmp->saveAs('简历.docx');//另存为

然后运行php index.php  得出简历.docx  如下图:

PHPword setImageValue 设置高度不生效_第4张图片

如果上诉你还不成功,请重新设置模板文件   有时候莫名其妙的样式,可能导致图片显示不正常也未可知!

你可能感兴趣的:(服务器,php,word)