5

Does anyone know if it is possible to add hyperlinks to image fields etc. in the pop-up windows of Qgis2web?

I've had a look in the layers javascript file, but the image source path isn't rendered using html, so I can't simply append <a href" to it.

The image is coming in via the usual method of having a field that contains the path of the image file.

I'd like to achieve linking of the image, like in the example below. Alternatively, I could just populate the 'more info' field with the URL, but if I did that I'd like to avoid just doing it by having a field that contains the entire URL, as some may be quite long (these will only be internal links).

enter image description here

2
  • Your screenshot shows OpenLayers 3 output. Do you feel strongly about using that rather than Leaflet? Any answers to your question will be completely different for each format, so we need to know which you want to use. Dec 19, 2016 at 14:38
  • I can't remember why I settled on OpenLayers3, but if it's easier to provide an answer for Leaflet then that's fine, as both seem to work equally well. Dec 19, 2016 at 14:50

2 Answers 2

3

You can show an image in popup window by adding field of type text, select the number of characters to be long, for example 100 characters, and add the following in the Image_Field:

<img src = "../images/Name.JPG"  width="300" height="225" alt="Alias Name"/>

where:

../images : The folder that contains the images located outside the leaflet folder that you saved qgis2web output data.

After creating the link to every image under the Image_Field in the attribute table, then you need to export to leaflet again using qgis2web plugin.

Here is a sample output:

enter image description here

9
  • With the <a href> tags in front, this works well, thanks. I was a bit stumped about how to render the code in the .js file as well, as I looked at it manually. It turns out the following works. <a href=\"http:\/\/www.touringcars.net\"><img src = \"images\/nordschleife.png\" \/><\/a> Dec 20, 2016 at 10:51
  • I am wondering why the down voting. I used the above technique which I described in my answer and it worked perfectly.
    – ahmadhanb
    Dec 20, 2016 at 14:04
  • I have up voted you after amending my original comment... Dec 20, 2016 at 14:25
  • This answer doesn't make use of qgis2web's ability to add images to popups automatically if a field's edit widget is set to 'Photo'. Dec 20, 2016 at 16:49
  • 1
    @StuSmith It is a matter of file management, you can store your images in the same leaflet folder if you want, but you need to adjust the path. But it will be easy to manage your images if they are stored in a separate folder.
    – ahmadhanb
    Jun 30, 2019 at 6:23
1

Firstly, hopefully you are aware that if you set the image field's edit widget in QGIS to 'Photo', the images appear in your popups without any editing of code?

The rest of this answer is untested:

For an OpenLayers 3 export, you need to edit resources/qgis2web.js in a text editor. This will only work if only one of your fields is an image. Search for the following code (around line 119):

popupField += (currentFeature.get(currentFeatureKeys[i]) != null ? '<img src="images/' + currentFeature.get(currentFeatureKeys[i]).replace(/[\\\/:]/g, '_').trim()  + '" /></td>' : '');

Change it to:

popupField += (currentFeature.get(currentFeatureKeys[i]) != null ? '<a href="' + currentFeature.get('URL_COLUMN') + '"><img src="images/' + currentFeature.get(currentFeatureKeys[i]).replace(/[\\\/:]/g, '_').trim()  + '" /></a></td>' : '');

Replace URL_COLUMN with the name of the column which has the address you want to link to.

For a Leaflet export, the file you need to edit is index.html. You need to find the function defined with the following code:

function pop_LAYER_NAME0(feature, layer) {

where LAYER_NAME is the layer which contains your image. Within that function should be some code including the <img> tag (it will vary according to what popup field heading layout you have specified):

<td colspan="2"><strong>IMAGE_COLUMN</strong><br />' + (feature.properties['IMAGE_COLUMN'] !== null ? '<img src="images/' + String(feature.properties['IMAGE_COLUMN']).replace(/[\\/:]/g, '_').trim() + '">' : '') + '</td>

where IMAGE_COLUMN is the name of the field which has your image. Change this to:

<td colspan="2"><strong>IMAGE_COLUMN</strong><br />' + (feature.properties['IMAGE_COLUMN'] !== null ? '<a href="' + feature.properties['URL_COLUMN'] + '"><img src="images/' + String(feature.properties['IMAGE_COLUMN']).replace(/[\\/:]/g, '_').trim() + '"></a>' : '') + '</td>\

where IMAGE_COLUMN is still the column with your image, and URL_COLUMN is the column with the link address.

5
  • Thanks for this. I was using the image field's edit widget on 'Photo', but that's all it did - I couldn't then concatenate the hyperlink tags around it. So I've tried ahmadhanb's method, which is working as desired. It would be nice, albeit non-essential, if the two (photo / link) could be input in seperate fields and then married up in an option within qgis2web. Dec 21, 2016 at 13:44
  • Also, I did briefly look into your method above, but I couldn't get it working - also the first code edit I actually found on line 156... Dec 21, 2016 at 13:46
  • @Tom Chadwin QGIS 3.8 does not have a Photo widget. Is there an alternative?
    – Stu Smith
    Jun 30, 2019 at 0:58
  • @StuSmith In QGIS3, it's the Attachment edit widget. However, this feature has gone untested for a long time now because of an upstream QGIS bug (github.com/qgis/QGIS/issues/26948). This bug has not yet been fixed, so the status of this qgis2web feature is pretty much unknown. Jul 1, 2019 at 9:45
  • @ Tom Chadwin good to know. I'll stay at 2.18 for this!
    – Stu Smith
    Jul 1, 2019 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.