Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
CGI::HtmlExtension#img
img(src = "", alt = "", width = nil, height = nil)
lib/cgi/html.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.4.1/lib/cgi/html.rb, line 474
def img(src = "", alt = "", width = nil, height = nil)
attributes = if src.kind_of?(String)
{ "SRC" => src, "ALT" => alt }
else
src
end
attributes["WIDTH"] = width.to_s if width
attributes["HEIGHT"] = height.to_s if height
super(attributes)
end
Generate an Image element as a string.
src is the URL of the image. alt is the alternative text for the image. width is the width of the image, and height is its height.
Alternatively, the attributes can be specified as a hash.
Example
img("src", "alt", 100, 50) # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50"> img("SRC" => "src", "ALT" => "alt", "WIDTH" => 100, "HEIGHT" => 50) # <IMG SRC="src" ALT="alt" WIDTH="100" HEIGHT="50">