Phpword: Set a hyperlink on a image

Created on 14 May 2019  路  9Comments  路  Source: PHPOffice/PHPWord

This is:

  • [x] a feature request

Expected Behavior

I asked that question on stackoverflow, but I'm sure that's no possible atm.
It should be possible to set an (external, internal) link on an image. At the moment (PHPWord version 0.16.0), it's only allowed to set text to a link (see "src/PhpWord/Element/Link.php"). And on an image element, I can't set a link.

Context

  • PHP version: 7.2.15
  • PHPWord version: 0.16

Most helpful comment

We made this issue working in our environment. I don't know "how nice" we solved it. If it's more or less bulletproof, we can do a PR if you want. Let me check that...

All 9 comments

What is you output format? docx or odt?

ODT has a special anchor named draw:a, so it should a special method. eg:

$section->addImage('img/img1.jpg',
    [
        'width' => 200,
        'height' => 200,
        'link' => '....'
    ]
);

http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#__RefHeading__1415880_253892949

When you set a link on an image the libreoffice writes this three node:

<draw:a>  
  <draw:frame>  
     <draw:image />  
  </draw:frame>
</draw:a>  

unfortunately docx

I'm afraid this is currently not possible. The way word does this is

<w:drawing>
    <wp:inline>
        <wp:docPr id="1" name="Picture 1">
            <a:hlinkClick xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" r:id="rId4"/>
        </wp:docPr>
    </wp:inline>
</w:drawing>

rId4 being defined as a relation.

Do you plan to add this feature in a future release? This is a highly needed feature!

I tested saving a file in odf format using the 'link' property on an image, and the link is not showing up in the odf file. When I right-click on the image and select "hyperlink", no link is defined.

We made this issue working in our environment. I don't know "how nice" we solved it. If it's more or less bulletproof, we can do a PR if you want. Let me check that...

We made this issue working in our environment. I don't know "how nice" we solved it. If it's more or less bulletproof, we can do a PR if you want. Let me check that...

Please post it if you can!

I was able to create a workaround by creating a Word VBA macro to loop through the images and add the links after PHPWord created the files. This worked, but it would be much easier if PHPWord could support this directly.

bountysource? Yeah, I want that baked. Small incentive deposited: https://www.bountysource.com/issues/74132279-set-a-hyperlink-on-a-image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaberu picture jaberu  路  3Comments

taophp picture taophp  路  3Comments

Ryuzakix3 picture Ryuzakix3  路  6Comments

btry picture btry  路  4Comments

tasmer picture tasmer  路  3Comments