990 |
990 |
end
|
991 |
991 |
if attachments.present?
|
992 |
992 |
title_and_alt_re = /\s+(title|alt)="([^"]*)"/i
|
|
993 |
style_attr_re = /\s+style="([^"]*)"/i
|
993 |
994 |
|
994 |
995 |
text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png|webp))"([^>]*)/i) do |m|
|
995 |
996 |
filename, ext, other_attrs = $1, $2, $3
|
996 |
997 |
|
|
998 |
# Check if style attribute exists
|
|
999 |
has_style = other_attrs =~ style_attr_re
|
|
1000 |
|
997 |
1001 |
# search for the picture in attachments
|
998 |
1002 |
if found = Attachment.latest_attach(attachments, CGI.unescape(filename))
|
999 |
1003 |
image_url = download_named_attachment_url(found, found.filename, :only_path => only_path)
|
... | ... | |
1003 |
1007 |
title_and_alt = other_attrs.scan(title_and_alt_re).to_h
|
1004 |
1008 |
other_attrs.gsub!(title_and_alt_re, '')
|
1005 |
1009 |
|
|
1010 |
# Process dimensions and scaling
|
|
1011 |
width, height = nil, nil
|
|
1012 |
image = MiniMagick::Image.open(found.diskfile)
|
|
1013 |
width, height = image.width, image.height
|
|
1014 |
|
|
1015 |
# Adjust dimensions for @2x, @3x, etc.
|
|
1016 |
if filename =~ /@(\d+)x\./
|
|
1017 |
scale_factor = $1.to_i
|
|
1018 |
width /= scale_factor
|
|
1019 |
height /= scale_factor
|
|
1020 |
end
|
|
1021 |
|
|
1022 |
# Resize if width exceeds 1000px while maintaining aspect ratio
|
|
1023 |
if width > 1000
|
|
1024 |
scale = 800.0 / width
|
|
1025 |
width = 800
|
|
1026 |
height = (height * scale).round
|
|
1027 |
end
|
|
1028 |
|
|
1029 |
# Add width and height to the img tag only if style attribute is absent
|
|
1030 |
dimension_attrs = (!has_style && width && height) ? " width=\"#{width}\" height=\"#{height}\"" : ""
|
|
1031 |
|
1006 |
1032 |
title_and_alt_attrs = if !desc.blank? && title_and_alt['alt'].blank?
|
1007 |
1033 |
" title=\"#{desc}\" alt=\"#{desc}\""
|
1008 |
1034 |
else
|
1009 |
1035 |
# restore original title and alt attributes
|
1010 |
1036 |
" #{title_and_alt.map { |k, v| %[#{k}="#{v}"] }.join(' ')}"
|
1011 |
1037 |
end
|
1012 |
|
"src=\"#{image_url}\"#{title_and_alt_attrs} loading=\"lazy\"#{other_attrs}"
|
|
1038 |
"src=\"#{image_url}\"#{title_and_alt_attrs}#{dimension_attrs} loading=\"lazy\"#{other_attrs}"
|
1013 |
1039 |
else
|
1014 |
1040 |
m
|
1015 |
1041 |
end
|