# Change image based on platform

**URL:** <https://community.bloomreach.com/t/change-image-based-on-platform/602>\
**Category:** Experience Manager (PaaS/OnPrem)\
**Created:** [November 20, 2018, 12:31pm UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602 "2018-11-20T12:31:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marsdev](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@marsdev](https://community.bloomreach.com/u/marsdev)\
**Post date:** [November 20, 2018, 12:31pm UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602/1 "2018-11-20T12:31:07Z")

</div>

I have code that is “working”, but I wanted to make sure this is the recommended approach.

Basically I want to set images based on the “platform” that I have detected in the component.

Here is what I have in a freemarker template that is working:

\<img src="\<@hst.html hippohtml=document.html\> \<#if profile == ‘mobile’\> \<@hst.link hippobean=document.heroimage.mobilehero/\> \<#elseif profile == ‘tablet’\> \<@hst.imagevariant name=“document.heroimage.original”/\> \<#else\>\<@hst.link hippobean=document.heroimage/\>\</#if\> /@hst.html" alt="${document.heroimage.description?html}" class=“img-fluid”\>

“profile” is set by the component and is working for me. Using the approach above, is that the best way to set the image src?

thanks,

John

---

<div class="post-metadata">

**Author:** ![jasper.floor](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.bloomreach.com/jasper.floor/32/28_2.png) [@jasper.floor](https://community.bloomreach.com/u/jasper.floor)\
**Post date:** [November 20, 2018, 1:35pm UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602/2 "2018-11-20T13:35:51Z")

</div>

I feel this is more a front end question than a Bloomreach Experience question, but doing this this way is not responsive. I think there is a way to do this via CSS, but I am no expert on that.

Any front enders reading?

---

<div class="post-metadata">

**Author:** ![marsdev](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@marsdev](https://community.bloomreach.com/u/marsdev)\
**Post date:** [November 20, 2018, 2:21pm UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602/3 "2018-11-20T14:21:32Z")

</div>

I understand that I can use "media queries’ in CSS… I was just wondering if I wanted to change it on the server side if there was a recommended way to do it?

I had found the following and was trying to use it [https://www.onehippo.org/library/concepts/rewriting-rich-text-field-runtime/render-different-image-variants.html](https://www.onehippo.org/library/concepts/rewriting-rich-text-field-runtime/render-different-image-variants.html)

I was having trouble figuring out where the example below would go in the template and how to use “imagevariant”…

`<@hst.html hippohtml=document.html>`

`` `<#if device == 'mobile'>`

`` `<@hst.imagevariant name="hippogogreengallery:mobilethumbnail"/>`

`` `<#elseif device == 'tablet'>`

`` `<@hst.imagevariant name="hippogogreengallery:largethumbnail"/>`

`` `<#else>`

`` `<@hst.imagevariant name="hippogallery:original"/>`

`` `</#if>`

`</@hst.html>`

---

<div class="post-metadata">

**Author:** ![woonsanko](https://avatars.discourse-cdn.com/v4/letter/w/b5a626/32.png) [@woonsanko](https://community.bloomreach.com/u/woonsanko)\
**Post date:** [November 20, 2018, 5:48pm UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602/4 "2018-11-20T17:48:43Z")

</div>

The example in the document is only for rich text fields (containing image links added by end users through CMS rich text editor), not for any other image links (added by end users through CMS link picker). In other words, if the `document` has a `hippostd:html` compound field and so it has a property getter for that, you may follow the example.  
However, if the `document` has an image link property, you cannot use the technique in the example. In that case, your original approach seems okay, but you should not mix `<@hst.html ... />` for a simple image (link) property.  
Suppose `document.heroimage` is a link to an image and you want to generate an image src link from there. Don’t use `<@hst.html />` there because the image link compound is different from `hippostd:html` compound field. Instead you can do the following for example:

```auto
<#if profile == ‘mobile’>
  <@hst.link var="myImageLink" hippobean=document.heroimage.mobilehero />
<#elseif profile == ‘tablet’>
  <@hst.link var="myImageLink" hippobean=document.heroimage.original” />
<#else>
  <@hst.link var="myImageLink" hippobean=document.heroimage” />
</#if>

<img src="${myImageLink}" alt="${document.heroimage.description?html}" class=“img-fluid”>

```

You might want to modularize the logic into a FreeMarker function [1] in an importable [2] library. Then you can possibly make it like this:

```auto
<#import ../includes/myimglib.ftl as myimglib>
<img src="${myimglib.getLink(document.heroimage, profile)}" alt="${document.heroimage.description?html}" class=“img-fluid”>

```

[1] [https://freemarker.apache.org/docs/ref\_directive\_function.html](https://freemarker.apache.org/docs/ref_directive_function.html)  
[2] [https://freemarker.apache.org/docs/ref\_directive\_import.html](https://freemarker.apache.org/docs/ref_directive_import.html)

---

<div class="post-metadata">

**Author:** ![marsdev](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@marsdev](https://community.bloomreach.com/u/marsdev)\
**Post date:** [November 20, 2018, 6:07pm UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602/5 "2018-11-20T18:07:27Z")

</div>

Thank you very much. I’ll try that approach.

---

<div class="post-metadata">

**Author:** ![ThoLe](https://yyz1.discourse-cdn.com/flex027/user_avatar/community.bloomreach.com/thole/32/98_2.png) [@ThoLe](https://community.bloomreach.com/u/ThoLe)\
**Post date:** [November 21, 2018, 2:58am UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602/6 "2018-11-21T02:58:58Z")

</div>

Hi @marsdev  
But I wonder how do you detect the client device from Hst Component ?

---

<div class="post-metadata">

**Author:** ![marsdev](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@marsdev](https://community.bloomreach.com/u/marsdev)\
**Post date:** [November 21, 2018, 3:15am UTC](https://community.bloomreach.com/t/change-image-based-on-platform/602/7 "2018-11-21T03:15:29Z")

</div>

Not very sophisticated:-)

Just checking the user-agent…

```
String userAgent = request.getHeader("user-agent").toLowerCase();

if (userAgent.contains("iphone") || userAgent.contains("android")) {
	request.setAttribute("profile", "mobile");
} else {
	request.setAttribute("profile", "non-mobile");
}
```
