Class: Magento::Params::CreateImage

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/magento/params/create_image.rb

Overview

Helper class to create product image params. before generating the hash, the following image treatments are performed:

  • resize image

  • remove alpha

  • leaves square

  • convert image to jpg

Example:

params = Magento::Params::CreateImage.new(
  title: 'Image title',
  path: '/path/to/image.jpg', # or url
  position: 1,
  size: 'small', # options: 'large'(defaut), 'medium' and 'small',
  disabled: true, # default is false,
  main: true, # default is false,
).to_h

Magento::Product.add_media('sku', params)

The resize defaut confiruration is:

Magento.configure do |config|
  config.product_image.small_size  = '200x200>'
  config.product_image.medium_size = '400x400>'
  config.product_image.large_size  = '800x800>'
end

Constant Summary collapse

VARIANTS =
{
  'large'  => :image,
  'medium' => :small_image,
  'small'  => :thumbnail
}.freeze

Instance Method Summary collapse

Instance Method Details

#to_hObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/magento/params/create_image.rb', line 51

def to_h
  {
    "disabled": disabled,
    "media_type": 'image',
    "label": title,
    "position": position,
    "content": {
      "base64_encoded_data": base64,
      "type": mini_type,
      "name": filename
    },
    "types": main ? [VARIANTS[size]] : []
  }
end

#variantsObject

Generates a list containing an Magento::Params::CreateImage instance for each size of the same image.

Example:

params = Magento::Params::CreateImage.new(
  title: 'Image title',
  path: '/path/to/image.jpg', # or url
  position: 1,
).variants

params.map(&:size)
=> ['large', 'medium', 'small']


80
81
82
83
84
# File 'lib/magento/params/create_image.rb', line 80

def variants
  VARIANTS.keys.map do |size|
    CreateImage.new(attributes.merge(size: size, disabled: size != 'large'))
  end
end