Ruby - Access Image Sizes for Featured Image

  • I use Ghost on a subdomain for my blog.
  • My main site is built with Ruby on Rails.
  • I connect the two using the Ghost API and pull across the latest blog posts information: image_url, title, url, excerpt, published_at, primary_tag.

The problem is that I can only figure out how to pull in the original image_url which is normally about 2K wide at 500kb. So not ideal when I pull across the latest 5 posts.

This is part of the Ruby code I use:

    def new_post(data)
            image_url = Addressable::URI.heuristic_parse(data['feature_image'])

            if image_url && image_url.site.nil?
              image_url.site = base_url
            end

            { image_url: image_url.to_s,
              title: data['title'],
              url: data['url'],
              excerpt: format_excerpt(data['custom_excerpt'] || data['excerpt']),
              published_at: Time.parse(data['published_at']),
              primary_tag: data['primary_tag'] }
          end

I know to use responsive images I can set a size attribute:

<img src="{{img_url feature_image size="s"}}">

Can someone help me figure out how to add a size attribute? Or let me know if it’s not even possible.

The image sizes in Ghost aren’t really API/Headless compatible at the moment, so you’ll have to re-implement (or extract) the logic that’s used in the img_url helper:

https://github.com/TryGhost/Ghost/blob/master/core/frontend/helpers/img_url.js

1 Like

Thanks @vikaspotluri123, will take a look into this!