# Getting the image from a variation

**URL:** <https://forum.litium.com/t/getting-the-image-from-a-variation/191>\
**Category:** Questions\
**Tags:** media\
**Created:** [August 15, 2018, 6:00am UTC](https://forum.litium.com/t/getting-the-image-from-a-variation/191 "2018-08-15T06:00:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zsrdos](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zsrdos](https://forum.litium.com/u/Zsrdos)\
**Post date:** [August 15, 2018, 6:00am UTC](https://forum.litium.com/t/getting-the-image-from-a-variation/191/1 "2018-08-15T06:00:48Z")

</div>

Hi I have a “Variation v”, what is the simplest way of getting its variation image?

Litium version: 6.2

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [August 15, 2018, 7:40am UTC](https://forum.litium.com/t/getting-the-image-from-a-variation/191/2 "2018-08-15T07:40:26Z")

</div>

If you do something like the following snippet you will fetch the variant and from the fields take the assigned images and create the `ImageModel`. `ImageModel` have a simple method to take out the url for an image in correct max size.

```auto
var variant = _variantService.Get(variantId);
var imageModel = variant.Fields.GetValue<IList<Guid>>(SystemFieldDefinitionConstants.Images)?.FirstOrDefault().MapTo<ImageModel>();
var url = imageModel.GetUrlToImage(...);

```

Some times it is the images on the `BaseProduct` that should be used instead. Then you will do the same but use the `BaseProductService` to fetch that entity instead, the rest of the code is the same.

---

<div class="post-metadata">

**Author:** ![Zsrdos](https://avatars.discourse-cdn.com/v4/letter/z/74df32/32.png) [@Zsrdos](https://forum.litium.com/u/Zsrdos)\
**Post date:** [August 15, 2018, 8:08am UTC](https://forum.litium.com/t/getting-the-image-from-a-variation/191/3 "2018-08-15T08:08:43Z")

</div>

So basically this? Or should the null check be higher up in the logic?

```
var imageModel = variant.Fields.GetValue<IList<Guid>>(SystemFieldDefinitionConstants.Images)?.FirstOrDefault().MapTo<ImageModel>();

        if(imageModel == null)
        {
            BaseProduct baseProduct = _baseProductService.Get(variant.BaseProductSystemId);
             imageModel = baseProduct.Fields.GetValue<IList<Guid>>(SystemFieldDefinitionConstants.Images)?.FirstOrDefault().MapTo<ImageModel>();
        }
```

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [August 15, 2018, 8:52am UTC](https://forum.litium.com/t/getting-the-image-from-a-variation/191/4 "2018-08-15T08:52:42Z")

</div>

That should work.

If we check how the `ProductModel` is working that will take decision from the display template if that is using base product or variant url’s and if you want to use the same logic it can be better to convert the variant into the product model and from that take the image.

```auto
var variant = _variantService.Get(variantId);
var productModel = _productModelBuilder.BuildFromVariant(variant)
var imageModel = productModel.GetValue<IList<Guid>>(SystemFieldDefinitionConstants.Images)?.FirstOrDefault().MapTo<ImageModel>();
var url = imageModel.GetUrlToImage(...);

```
