gpt4 book ai didi

ruby-on-rails - 如何使用重新处理将已上传的回形针图像调整为 s3? ( rails )

转载 作者:行者123 更新时间:2023-12-04 05:53:04 24 4
gpt4 key购买 nike

我有一个模型:

class PropertyImage < ActiveRecord::Base
has_attached_file :picture,
storage: :s3,
s3_credentials: CONFIG['s3'],
s3_protocol: (Rails.env.development? ? "http": "https"),
styles: {
thumb: '100x100>',
large: '633x460>',
medium: '301x240>'
}
end

我正在使用 Rails (4.0.1)、可卡因 (0.5.4) 和回形针 (3.5.4)。

我想迁移旧图像(没有拇指、大和中)并调整每个图像的大小,所以我创建了一个 rake 脚本:

namespace :migrate_images do
desc "Resize Images in PropertyImage"

task start_migration: :environment do

PropertyImage.not_migrated.find_each do |pi|
ImageConverter.perform(pi)
end
end

end

和 ImageConverter 类:

class ImageConverter

def self.perform(pi)
begin
pi.picture.reprocess!
pi.update_attributes!({migrated: true})
puts "PropertyImage [#{pi.id}] has been migrated."
rescue Exception => e
puts "PropertyImage [#{pi.id}] has an error. #{e}"
end
end
end

现在,当我运行脚本时,我不断收到以下错误:

⇒  bundle exec rake migrate_images:start_migration
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
PropertyImage [11] has an error. Validation failed: Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError
PropertyImage [12] has an error. Validation failed: Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError, Picture Paperclip::Errors::NotIdentifiedByImageMagickError

请注意,我阅读了类似的文章并在 application.rb 的末尾添加了以下内容:

Paperclip.options[:command_path] = "/usr/local/bin/identify"

并且我确定我已经安装了 imagemagick:

⇒  brew install imagemagick
Warning: You have an outdated version of /usr/bin/install_name_tool installed.
This will cause binary package installations to fail.
This can happen if you install osx-gcc-installer or RailsInstaller.
To restore it, you must reinstall OS X or restore the binary from
the OS packages.
Warning: imagemagick-6.8.9-7 already installed

任何帮助将不胜感激。

如果我尝试使用 Rails 控制台重现问题,请注意:

>> p = Property.find(93746)
>> p.property_images.each do |pi|
?> pi.picture.reprocess!
>> end
PropertyImage Load (1.4ms) SELECT "property_images".* FROM "property_images" WHERE "property_images"."invalid_image" = 'f' AND "property_images"."property_id" = $1 ORDER BY "property_images"."apartment_main" DESC [["property_id", 93746]]
[paperclip] copying /property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg to local file /var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg
[AWS S3 200 1.212736 0 retries] get_object(:bucket_name=>"my_bucket_development",:key=>"property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg")

Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
Command :: identify -format '%wx%h,%[exif:orientation]' '/var/folders/g8/3v37gc0x16b313mvf7464qtc0000gn/T/dfe10026af3ac1b8cc5c94f00437092020141027-6352-12gmkmc.jpg[0]' 2>/dev/null
[paperclip] An error was received while processing: #<Paperclip::Errors::NotIdentifiedByImageMagickError: Paperclip::Errors::NotIdentifiedByImageMagickError>
[paperclip] saving /property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg
[AWS S3 200 0.293473 0 retries] put_object(:acl=>:public_read,:bucket_name=>"my_bucket_development",:content_length=>0,:content_type=>"image/jpeg",:data=>Paperclip::AttachmentAdapter: 722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg,:key=>"property_images/pictures/000/325/476/original/722952f7-4cd4-47bb-bdcf-c7411c9d6021.jpg")

(0.2ms) BEGIN
(0.2ms) ROLLBACK

最佳答案

确保您正在使用的服务器上的 Imagemagick 支持您尝试上传的格式。

要确保这一点,请使用此命令:

convert -list format

如果您尝试使用 Paperclip 上传的格式不在列表中,您需要安装所需的库,然后重新安装/重新编译 Imagemagick。

Here给出了如何在 Unix 机器上执行此操作的示例。要安装 jpg 库,使用命令:

yum install libjpeg libjpeg-devel

如何安装库和重新安装/重新编译 Imagemagick 取决于您使用的操作系统和您需要的图像格式。

关于ruby-on-rails - 如何使用重新处理将已上传的回形针图像调整为 s3? ( rails ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26587317/

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