Liquid Snippets by ALSEL
UI部品中級

画像の縦横比と遅延読み込み

Shopify の image_url フィルターを活用して、画像をアスペクト比・サイズ・遅延読み込みオプション付きで出力するスニペット。Retina 対応(2倍解像度)への自動スケーリングにも対応。

用途
商品カード、コレクションギャラリー、バナー画像など、様々なレイアウトで統一した画像表示を実現したいとき。アスペクト比を固定することで CSS での aspect-ratio 設定が不要になる。
設置場所
snippets/image-url.liquid に配置し、`{% render 'image-url', img: product.featured_image, size: 600, orientation: 'ratio-4x3', loading: 'lazy', scale: false, class: 'product-image' %}` のように呼び出す。
注意点
orientation パラメータは 'ratio-1x1'、'ratio-4x3'、'ratio-16x9' など指定値に限定されるため、任意の比率が必要なときは else 分岐を調整する。scale を true にすると width・height が 2倍になるため、元の img 容器の幅・高さ CSSも合わせて確認しておく。width・height 属性が HTML に明示されるので、CSS の max-width: 100% と合わせて指定し、レスポンシブ表示を損なわない。
タグ:imageresponsiveaspect-ratiolazy-loadingretina

コード

74 行 / liquid
{% liquid
  # theme-check-disable ImgLazyLoading 
  # theme-check-disable RemoteAsset
%}

{% comment %}
  Renders an image tag throught the Shopify's, image_url filter
  https://shopify.dev/docs/api/liquid/filters/image_url

  Accepts:
  - img: {object} The image object to apply the filter
  - size: {Number} (Optional) The preferred image size which the image width/height will be handled. (default is 600)
  - orientation: {String} (Optional) The image orientation. (default is square)
  - loading {String} {Optional} The image loading mode. Either lazy or eager. (default is lazy)
  - scale {Boolean} {Optional}. Useful if your images are looking blurred. (default is false)
  - class: {String} (Optional) An optional class to apply to the Img element

  Usage:
  {% render 'image-url' img: product.featured_image, size: 600, orientation: 'square', loading: 'lazy', scale: false, class: "mb-4"  %}
{% endcomment %}

{% liquid
  assign size = size | default: 600
  assign orientation = orientation | default: 'square'
  assign loading = loading | default: 'lazy'

  case orientation
    when 'ratio-1x1'
      assign width = size
      assign height = size 
    when 'ratio-4x3'
      assign width = size
      assign height = size | divided_by: 4 | times: 3 | round
    when 'ratio-16x9'
      assign width = size
      assign height = size | divided_by: 16 | times: 9 | round
      when 'ratio-21x9'
      assign width = size
      assign height = size | divided_by: 21 | times: 9 | round
    when 'ratio-3x4'
      assign width = size | divided_by: 4 | times: 3 | round
      assign height = size
    when 'ratio-9x16'
      assign width = size | divided_by: 16 | times: 9 | round
      assign height = size
    when 'ratio-9x21'
      assign width = size | divided_by: 21 | times: 9 | round
      assign height = size
    else
      assign width  = size
      assign height = size | divided_by: img.aspect_ratio | round
  endcase

  if scale
    assign width_scaled = width | times: 2
    assign height_scaled = height | times: 2
  else
    assign width_scaled = width
    assign height_scaled = height
  endif
%}

<img
  src="{{ img | image_url: width: width_scaled, height: height_scaled, crop: 'center' }}"
  class="img-fluid {{ class }}"
  alt="{{ img.alt }}"
  width="{{ width }}"
  height="{{ height }}"
  loading="{{ loading }}">

{% liquid
  # theme-check-enable ImgLazyLoading 
  # theme-check-enable RemoteAsset
%}

出典・ライセンス

License:
MIT

このコードは kondasoft 著作の MIT ライセンスソースです。 原本の著作権は kondasoft が保有します。日本語訳は ALSEL によるものです。

関連項目

UI部品初級

成功チェックマークアイコン

緑色の円形背景にチェックマークを描いたSVGアイコン。フォームやメッセージの完了状態を視覚的に表現する。

📁 shopify-headless-theme·MIT·6
UI部品初級

エラーアイコン

エラー状態を示す円形アイコン。SVG で描画されたアラート記号を含む UI 部品で、accessibility 対応により視覚障害ユーザーには非表示。

📁 theme-tools·MIT·8
UI部品中級

カスタム要素のラッパーコンポーネント

child-element というカスタム HTML 要素をラップし、スロットに子要素を挿入するスニペット。Web Components パターンで再利用可能なコンポーネント構造を実現する。

📁 theme-tools·MIT·10
UI部品初級

静的コンテンツ

管理画面で入力したテキストコンテンツを静的に描画するブロック。セクション内で複数回使い分けられる汎用コンテナとして機能する。

📁 theme-tools·MIT·11
UI部品初級

テキスト

セクションに追加できるシンプルなテキストブロック。管理画面からドラッグ&ドロップで配置でき、固定テキスト「hello world」を表示する基本的なブロック実装。

📁 theme-tools·MIT·15
UI部品初級

セクション共通ヘッダー(タイトル・説明文)

セクション内で繰り返し使うタイトルと説明文をまとめて描画するスニペット。テーマカスタマイザーから設定したテキストとフォントサイズを条件付きで出力する。

📁 ks-bootshop·MIT·24