4

I would like to have images displayed in my HTML widget.

Unfortunately, the initial script generated by QGIS doesn't work.

 <tr>
 <td>Images</td>
 <td><script>document.write(expression.evaluate("\"Image\""));</script></td>
 </tr>

It comes with the text string only.

I found some solution at Display photo stored as blob in GPKG but it shows a Python function instead of a front-end solution.

I need my image related to the feature, but not in the HTML display section, but in HTML widget - see Show images related to features in QGIS?

There are some solutions here but don't work in my case:

Is there any way to show in the HTML widget the image related to my object?

enter image description here

Even if I place the link to static image, the problem is the same:

 <tr>
 <td>Images</td>
 <td><img src = "Supporting_Docs\Images\S1.jpg" width="300" height="225" 
 alt="Alias Name"/></td>
 </tr>

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 19, 2020 at 17:11
1

1 Answer 1

5

For a static image, you can try the following. Note that I am using an absolute local path with 3 slashes.

<div>Image: <img src = 'file:///D:\temp50円k.png' width='300' height='225' alt='Alias Name'></img></div>

For a dynamic image, you need to build the path. In my example the field photo contains a path like D:\temp50円k.png

<script>document.write(expression.evaluate("'<div>Image: <img src = \"file:///'|| photo ||'\" width=\"300\" height=\"225\" alt=\"Alias Name\"/></div>'"));</script>

The HTML widget it very powerful... but is also very sensitive to quotes, escaping, new lines etc. My tip is to start with a very simple output and to build around it, making and testing one change at a time. It is also useful to do a right click on the preview + inspect and to compare with the input (ex: the path D:\temp50円k.png was changed for file:///D:emp(k.png if I write the true image path instead of reading it from a field, pointing to escaping issues)

To finish, you can try to move part of the html out of the <script> section.... be very careful how the output(s) are appended to each others.

answered Nov 19, 2020 at 19:17
2
  • The first one is fine. The second one doesn't work. My image path is related to the project. I tried to put both a full path from my disk as well as the related to my project <img src = \"file:///Supporting_Docs\Images\'|| Image ||'\" width=\"300\" height=\"225\" alt=\"Alias Name\"/></div>'" is that alright then? Commented Nov 20, 2020 at 9:08
  • 1
    @MKR I don't know for the relative path. The example I provided do work here, so try the inspect trick to see how your path is interpreted Commented Nov 20, 2020 at 13:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.