Skip to content

Commit

Permalink
Update image size customization docs
Browse files Browse the repository at this point in the history
We found that the documentation on customizing image sizes for products and
variants was outdated and suggested users introduce a decorator. This was
fixed previously in the Legacy guides and we have ported the updated documentation
to the new version here.

Fixes #94

Co-authored-by: Cameron Day <[email protected]>
  • Loading branch information
forkata and camerond594 committed Feb 14, 2023
1 parent 419834f commit 43324f1
Showing 1 changed file with 35 additions and 25 deletions.
60 changes: 35 additions & 25 deletions docs/advanced-solidus/images-and-image-processing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,48 @@ image = Spree::Image.first
image.url(:product)
```

If you're building a custom storefront, you may also want to change the sizes of the images in your
store. You can do this by applying an override to `Spree::Image`:
If you're building a custom storefront, you may also want to change the sizes of
the images in your store or add additional sizes. The default sizes can be changed
using the `Spree::Config.product_image_styles` option.

```ruby title="app/overrides/amazing\_store/spree/image/customize\_sizes.rb"
module AmazingStore
module Spree
module Image
module CustomizeSizes
def self.prepended(klass)
klass.attachment_definitions[:attachment][:styles] = {
mini: '48x48>',
small: '400x400>',
product: '680x680>',
large: '1200x1200>',
jumbo: '1600x1600>'
}
end

::Spree::Image.prepend self
end
end
end
end
For example, in your `config/initializers/spree.rb` file. You can set
new defaults like this:

```ruby
# config/initializers/spree.rb
# E.g. these are the default values for Solidus up to version 3.3
config.product_image_styles = {
mini: '48x48>',
small: '100x100>',
product: '240x240>',
large: '600x600>',
jumbo: '1600x1600>'
}
```

Similar to product and variant image styles, you can customize the taxon image
styles using the `Spree::Config.taxon_image_styles` configuration option.

:::caution

[Active Storage][activestorage] will automatically generate the sizes upon
initial request. If you change the default image sizes and are using Paperclip,
you must regenerate the thumbnails by running a Rake task:

```bash
bundle exec rake paperclip:refresh:thumbnails CLASS=Spree::Image
```

:::

Now that you changed your sizes, try getting the URL for your new `jumbo` size or `large` sizes:

```ruby
image = Spree::Image.first
image = Spree::Product.first.images.first
image.url(:jumbo)
taxon = Spree::Taxon.first
taxon.url(:large)

icon = Spree::Taxon.first.icon
icon.url(:mini)
```

Solidus will generate the new sizes and return their URLs!
Expand Down

0 comments on commit 43324f1

Please sign in to comment.