gpt4 book ai didi

ruby-on-rails - Rails 5 - 事件存储 - 变体 - 异常 : "#
转载 作者:行者123 更新时间:2023-12-04 23:13:02 29 4
gpt4 key购买 nike

Rails 5.2.0(作为 API)

/配置/应用程序.rb

config.active_storage.variant_processor = :vips



问题:

/serializers/api/v1/user/current_user_serializer.rb
class Api::V1::User::CurrentUserSerializer < Api::V1::User::BaseSerializer
include Rails.application.routes.url_helpers

attributes(
[...]
:avatar
:created_at
)

def avatar
if object.avatar.attachment
avatar = {
image: url_for( object.avatar ), # This one works
thumb: url_for( object.avatar.variant(resize_to_fit: [800, 800]) ), # EXCEPTION
thumb_test: url_for( object.avatar.variant(resize: '800x800') ) # Returns image of size: 640x800 (expected 800x800)
}
end
end
end

我收到以下异常:
exception: "<MiniMagick::Error: `mogrify -resize-to-fit [800, 800] /tmp/mini_magick20180625-19749-rghjbg.jpg` failed with error: mogrify.im6: unrecognized option `-resize-to-fit' @ error/mogrify.c/MogrifyImageCommand/5519. >"

编辑

谢谢@乔治克拉格霍恩

我现在根据这篇文章创建了自己的变体:
https://prograils.com/posts/rails-5-2-active-storage-new-approach-to-file-uploads

lib/active_storage_variants.rb
class ActiveStorageVariants
class << self
def resize_to_fill(width:, height:, blob:, gravity: 'Center')
blob.analyze unless blob.analyzed?

cols = blob.metadata[:width].to_f
rows = blob.metadata[:height].to_f
if width != cols || height != rows
scale_x = width / cols
scale_y = height / rows
if scale_x >= scale_y
cols = (scale_x * (cols + 0.5)).round
resize = cols.to_s
else
rows = (scale_y * (rows + 0.5)).round
resize = "x#{rows}"
end
end

{
resize: resize,
gravity: gravity,
background: 'rgba(255,255,255,0.0)',
extent: cols != width || rows != height ? "#{width}x#{height}" : ''
}.merge(optimize_hash(blob))
end
end
end

/模型/关注/用户/active_storage_variants.rb
require 'active_storage_variants' # /lib/active_storage_variants.rb

module Users::ActiveStorageVariants

def avatar_thumbnail
variation = ActiveStorage::Variation.new(
ActiveStorageVariants.resize_to_fill(
width: 300, height: 300, blob: avatar.blob
)
)
ActiveStorage::Variant.new(avatar.blob, variation)
end
end

/模型/用户.rb
class User < ApplicationRecord
...

## Concerns
include Users::ActiveStorageVariants

...
end

调用它:

user.avatar_thumbnail

最佳答案

resize_to_fit: [800, 800]ImageProcessing转型。 Rails 5.2 不使用 ImageProcessing,因此不支持 libvips;它直接使用 MiniMagick 代替。

Rails 6 将切换到 ImageProcessing 并添加 libvips 支持。现在要使用 libvips,在 Rails 6 发布之前,在 GitHub 上捆绑 rails/rails 存储库的 master 分支:

# Gemfile
gem "rails", github: "rails/rails"

关于ruby-on-rails - Rails 5 - 事件存储 - 变体 - 异常 : "#<MiniMagick::Error: ` mogrify -resize-to-fit [800, 800]",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51025081/

29 4 0

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