faker-ruby/faker · error · ArgumentError

Size should be specified in format 300x300

Error message

Size should be specified in format 300x300

What it means

Raised by Faker::Placeholdit.image when the size string does not match /\A[0-9]+x[0-9]+\z/ (the anchor `$` after `\z` is redundant). The via.placeholder.com URL embeds size directly, so anything but digits-'x'-digits — '50', '50X50', '50 x 50', '50.5x50', nil — is rejected.

Source

Thrown at lib/faker/default/placeholdit.rb:33

      # @param text [String] Specifies a custom text to be used.
      # @return [String]
      #
      # @example
      #     # Keyword arguments: size, format, background_color, text_color, text
      #   Faker::Placeholdit.image #=> "https://via.placeholder.com/300x300.png"
      #   Faker::Placeholdit.image(size: '50x50') #=> "https://via.placeholder.com/50x50.png"
      #   Faker::Placeholdit.image(size: '50x50', format: 'jpg') #=> "https://via.placeholder.com/50x50.jpg"
      #   Faker::Placeholdit.image(size: '50x50', format: 'gif', background_color: 'ffffff') #=> "https://via.placeholder.com/50x50.gif/ffffff"
      #   Faker::Placeholdit.image(size: '50x50', format: 'jpeg', background_color: :random) #=> "https://via.placeholder.com/50x50.jpeg/39eba7"
      #   Faker::Placeholdit.image(size: '50x50', format: 'jpeg', background_color: 'ffffff', text_color: '000') #=> "https://via.placeholder.com/50x50.jpeg/ffffff/000"
      #   Faker::Placeholdit.image(size: '50x50', format: 'jpg', background_color: 'ffffff', text_color: '000', text: 'Some Custom Text') #=> "https://via.placeholder.com/50x50.jpg/ffffff/000?text=Some Custom Text"
      #
      # @faker.version 1.6.0
      def image(size: '300x300', format: 'png', background_color: nil, text_color: nil, text: nil)
        background_color = generate_color if background_color == :random
        text_color = generate_color if text_color == :random

        raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /\A[0-9]+x[0-9]+\z$/
        raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format)
        raise ArgumentError, "background_color must be a hex value without '#'" unless background_color.nil? || background_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/
        raise ArgumentError, "text_color must be a hex value without '#'" unless text_color.nil? || text_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/

        image_url = "https://via.placeholder.com/#{size}.#{format}"
        image_url += "/#{background_color}" if background_color
        image_url += "/#{text_color}" if text_color
        image_url += "?text=#{text}" if text
        image_url
      end

      private

      def generate_color
        format('%06x', rand * 0xffffff)
      end
    end
  end

View on GitHub (pinned to cca4184947)

Solutions

  1. Pass size as 'WIDTHxHEIGHT' with ASCII 'x', e.g. size: '50x50' (default '300x300').
  2. Interpolate with to_i: size: "#{width.to_i}x#{height.to_i}".
  3. Pre-validate with size =~ /\A[0-9]+x[0-9]+\z/ before calling.

Example fix

# before
Faker::Placeholdit.image(size: "#{params[:w]}x#{params[:h]}") # blank params -> 'x'

# after
Faker::Placeholdit.image(size: "#{params[:w].to_i}x#{params[:h].to_i}")
Defensive patterns

Strategy: validation

Validate before calling

size = "#{width.to_i}x#{height.to_i}"
size = '300x300' unless size.match?(/\A[0-9]+x[0-9]+\z/)
Faker::Placeholdit.image(size: size)

Type guard

def valid_placeholdit_size?(size)
  size.is_a?(String) && size.match?(/\A[0-9]+x[0-9]+\z/)
end

Try / catch

begin
  Faker::Placeholdit.image(size: size)
rescue ArgumentError
  Faker::Placeholdit.image # default 300x300
end

Prevention

When it happens

Trigger: Faker::Placeholdit.image(size: '100') or size: "#{w}*#{h}", size: "#{w}x" (nil height) raises 'Size should be specified in format 300x300'.

Common situations: Building size from ERB/view helpers where a dimension is nil or blank; user-provided dimensions from a query param; capital-X or Unicode multiplication-sign typos.

Related errors


AI-assisted analysis of faker-ruby/faker@cca4184947 (2026-08-21). Data as JSON: /api/errors/2252044f0e2886be. Report an issue: GitHub.