How to integrate ImageKit on Shopify store
ImageKit.io is an image management service for online businesses. It provides intelligent real-time image optimization, resizing, cropping, and fast CDN delivery. Integrating ImageKit on Shopify store allows you to improve your image quality and delivery speed.
Here are the steps to optimize images on your Shopify website.
Step 1: Configure origin in ImageKit.io dashboard
Configure a web server origin based on where your images are stored. In Shopify, this is your CDN base URL i.e. https://cdn.shopify.com
, https://cdn2.shopify.com
or https://cdn.shopifycloud.com
. You can check this under the network panel tab in Chrome like shown in the screenshot below:
Step 2: Fetch image through ImageKit.io endpoint
Let’s quickly fetch the image using ImageKit.io URL-endpoint and see if it’s working.
When you add the first origin in your account, it automatically becomes accessible through the default URL-endpoint, that is https://ik.imagekit.io/your_imagekit_id
. Otherwise, you will have to configure an existing URL endpoint or create a new one to fetch images from this newly added origin.
If your old image URL was https://cdn.shopify.com/img/s/files/1/1510/6482/t/22/assets/logo-mobile.png
, then the same image should be accessible through ImageKit.io URL-endpoint, i.e., https://ik.imagekit.io/your_imagekit_id/img/s/files/1/1510/6482/t/22/assets/logo-mobile.png
Step 3: Create settings_schema.json
If the above image did work correctly, we can now proceed to make the changes in our Shopify settings and theme files to switch the image delivery and optimization to ImageKit.
From your Shopify admin, click Online Store > Themes. Find the theme you want to edit, click the … button, and then click Edit HTML/CSS.
Under Config, click settings_schema.json and copy the code below as the last section of config file and hit Save.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | { "name": "ImageKit", "settings": [ { "type": "paragraph", "content": "Check out ImageKit's [ImageKit.io and Shopify integration](https://docs.imagekit.io/imagekit-docs/shopify) to learn more about this." }, { "type": "checkbox", "id": "enableImageKit", "label": "Enable Imagekit" }, { "type": "text", "id": "imagekitUrl", "label": "ImageKit url endpoint", "info":"The url endpoint you set within ImageKit. Example: `https://ik.imagekit.io/your_imagekit_id/`." }, { "type": "text", "id": "imagekitShopifyCdnUrl", "label": "Shopify CDN domain", "default":"//cdn.shopify.com", "info":"Do not change this unless you have a proxy in place. Not sure? Leave it as is." } ] } |
Step 4: Create imagekit.liquid file
Create a new file imagekit.liquid under Snippets directory. Copy the code below into that file, and save it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | {% capture IMAGEKIT %} {% if settings.enableImageKit %} {% for i in (1..1) %} {% unless src or settings.imagekitUrl != blank %} {{ src }} {% break %} {% endunless %} {% assign cdnUrls = settings.imagekitShopifyCdnUrl | split: ',' %} {% if cdnUrls.size == 0 %} {{ src }} {% break %} {% endif %} {% assign cdnUrl = cdnUrls[0] %} {% for temp in cdnUrls %} {% if src contains temp %} {% assign cdnUrl = temp %} {% break %} {% endif %} {% endfor %} {% assign cdnUrl = cdnUrl | strip %} {% unless src contains cdnUrl %} {{ src }} {% break %} {% endunless %} {% assign imagekitUrl = settings.imagekitUrl | strip %} {% assign tempImagekitUrl = imagekitUrl %} {% assign lastChar = imagekitUrl | slice:-1 %} {% assign imagekitUrlLength = imagekitUrl | size %} {% assign newImagekitUrl = imagekitUrlLength | minus:1 %} {% if lastChar == "/" %} {% assign tempImagekitUrl = imagekitUrl | slice:0,newImagekitUrl %} {% endif %} {% assign newSrc = src | strip | replace:cdnUrl,tempImagekitUrl %} {{ newSrc | default:src }} {% endfor %} {% else %} {{ src }} {% endif %} {% endcapture %}{{ IMAGEKIT | strip | replace:' ' | strip_newlines }} |
Step 5: Enable ImageKit.io
Navigate to Online store > Themes > Customize theme. In the sidebar, under general settings open ImageKit and enable it. Fill out the below two fields:
- Default URL endpoint – It should be
https://ik.imagekit.io/your_imagekit_id
- Shopify CDN domain – Its value should be
//cdn2.shopify.com
,//cdn.shopify.com
- Hit the “Save” button.
Step 6: Edit theme files
Now we get to editing your theme files. We first need to find out the files which are responsible for the output of image on your store and start editing them.
Here are a couple of examples indicating the change that needs to be made in the theme files. You can follow similar steps to change all of your theme files.
Example 1:
1 2 3 4 5 6 | Before <img class="grid-view-item__image" src="{% product.featured_image.src | img_url: grid_image_width %}" alt="{{ product.featured_image.alt }}"> After {% assign old_img_url = product.featured_image.src | img_url:grid_image_width %} <img class="grid-view-item__image" src="{% include 'imagekit' src:old_img_url %}" alt="{{ product.featured_image.alt }}"> |
Example 2:
1 2 3 4 5 6 | Before: <img src="{% featured_img_src | img_url: product_image_size %}" alt="{{ featured_img_alt }}" id="FeaturedImage-{{ section.id }}" class="product-featured-img{% if enable_zoom %} js-zoom-enabled{% endif %}"> After: {% assign old_img_url = featured_img_src | img_url: product_image_size %} <img src="{% include 'imagekit' src:old_img_url %}" alt="{{ featured_img_alt }}" id="FeaturedImage-{{ section.id }}" class="product-featured-img{% if enable_zoom %} js-zoom-enabled{% endif %}"> |
Example 3:
If there is a tag inside image URL and you cannot simply assign it, use {% capture %}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Before: <img width="{{ image.width }}" height="{{ image.height }}" src="{{ image | img_url: '50x50' }}" class="lazyload attachment-shop_single size-shop_single sp-post-image" alt="{{image.alt}}" title="{{product.title}}" data-src="{{ img_url }}" data-large_image="{%-include 'gl_image_format',cache:true, src: image.src, size: product_image_photowipe -%}" data-large_image_width="{{ image.width }}" data-large_image_height="{{ image.height }}" data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]" data-aspectratio="{{ image.aspect_ratio }}" data-sizes="auto"> After: {% assign old_img_src = image | img_url: '50x50' %} {% assign old_img_data_src = img_url %} {% capture old_img_data_large_image %} {%-include 'gl_image_format',cache:true, src: image.src, size: product_image_photowipe -%} {% endcapture %} <img width="{{ image.width }}" height="{{ image.height }}" src="{% include 'imagekit' src:old_img_src %}" class="lazyload attachment-shop_single size-shop_single sp-post-image" alt="{{image.alt}}" title="{{product.title}}" data-src="{% include 'imagekit' src:old_img_data_src %}" data-large_image="{% include 'imagekit' src:old_img_data_large_image %}" data-large_image_width="{{ image.width }}" data-large_image_height="{{ image.height }}" data-widths="[180, 360, 540, 720, 900, 1080, 1296, 1512, 1728, 2048]" data-aspectratio="{{ image.aspect_ratio }}" data-sizes="auto"> |
Source: ImageKit