Created
August 25, 2010 17:48
-
-
Save sstarr/549959 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use ImageMagick to get the dimensions of an image | |
def get_image_dimensions(image_url) | |
open(image_url, 'rb') do |f| | |
@image = Magick::Image.from_blob(f.read) | |
end | |
"width=\"#{@image.first.columns}\" height=\"#{@image.first.rows}\"" | |
end | |
# Insert the width and height into all img tags in a string | |
def insert_image_dimensions(string) | |
string.scan(/<img (src="(.+?(\.gif|\.jpg|\.png))")(.*?>)/i).each do |result| | |
dimensions = get_image_dimensions(result[1]) | |
string.sub!(result[0], '\0 ' + dimensions + '\3') | |
end | |
return string | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment