gpt4 book ai didi

ruby-on-rails - 如何从图像 url 中获取大小或重量?

转载 作者:行者123 更新时间:2023-12-04 16:19:43 25 4
gpt4 key购买 nike

引用上一个问题methods width and height Mechanize

我想知道如何使用 Mechanize 从网页图像中获取尺寸。

我创建了一个像 helper 一样使用的方法,但这个过程很慢,例如

url = "http://www.birchbox.com"
page = Mechanize.new.get(url)
images_url = page.images.map{|img| img.url.to_s }.compact

这是辅助方法:
def check_image_size
images_urls.each do |image_url|
image = MiniMagick::Image.open(image_url)
if image[:width] < 100
images_urls.delete("#{image_url}")
end
end
return images_urls
end

如果宽度小于 100 像素,则此方法会从数组中删除所有图像。

这种方法的问题是过程非常缓慢。使用此方法加载我的页面需要很长时间。

是否有任何快速简便的方法可以使用 Mechanize 做到这一点?

最佳答案

看看Fast Image

FastImage finds the size or type of an image given its uri by fetching as little as needed



进而...
images.each do |src|
size = FastImage.size(src)
puts "width: #{size[0]}"
puts "height: #{size[1]}"
end
end

关于ruby-on-rails - 如何从图像 url 中获取大小或重量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com