gpt4 book ai didi

ruby-on-rails - 如何: Get version image dimensions carrierwave

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:31 25 4
gpt4 key购买 nike

这是我的 uploader :

class PostImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:

def store_dir
"uploads/posts"
end

version :medium do
process :resize_to_limit => [280, nil]
process :convert => 'jpg'
end

def filename
random_token = Digest::SHA2.hexdigest("#{Time.now.utc}--#{model.id.to_s}").first(20)
ivar = "@#{mounted_as}_secure_token"
token = model.instance_variable_get(ivar)
token ||= model.instance_variable_set(ivar, random_token)
"#{model.title.gsub(" ", "-").downcase}-#{token}.jpg" if original_filename
end

def extension_white_list
%w(jpg jpeg gif png)
end

process :store_dimensions

private

def store_dimensions
if file && model.post_image.medium.present?
model.image_width, model.image_height = ::MiniMagick::Image.open(file.file)[:dimensions]
end
end

end

在本教程中,https://github.com/carrierwaveuploader/carrierwave/wiki/How-to%3A-Get-image-dimensions ,可以获得原始图像尺寸。但我想获得版本图像尺寸。像这样:

def store_dimensions
if file && model.post_image.medium.present?
model.image_width, model.image_height = ::MiniMagick::Image.open(model.post_image.medium)[:dimensions]
end
end

代替file.filemodel.post_image.medium

问题在于,当对象先前已创建时,此代码有效。仅在更新图像时有效。

我需要此代码在我第一次创建时能够正常工作。

问候!

最佳答案

您可以尝试使用回调https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-use-callbacks

after :store, :store_dimensions

# rest of uploader codes...

def store_dimensions(file)
model.image_width, model.image_height = ::MiniMagick::Image.open(model.post_image.medium)[:dimensions]
model.save!
end

关于ruby-on-rails - 如何: Get version image dimensions carrierwave,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31771499/

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