作物与蜻蜓铁路的圆形图像 [英] Crop circular image with dragonfly rails

查看:142
本文介绍了作物与蜻蜓铁路的圆形图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 dragonfly~> 0.9.15

鉴于图像,我对如何使用蜻蜓中的转换方法从图像中裁剪圆形部分感到困惑透明背景。

Given an image, I'm confused at how to use the convert method in dragonfly to crop a circular portion from the image with transparent background.

我可以使用直接图像magick命令从命令行运行它,但我找到的示例命令使用实际文件,我不知道如何在dragonfly处理它的同时获取文件。

I am able to use a direct image magick command to run it from command line but the example command I found uses actual files and I'm unaware of how to get the file while dragonfly processes it on the fly.

这是我从imagemagick的堆栈溢出问题中获取的实际命令。

Here's the actual command I took from a stack overflow question with imagemagick.

https://stackoverflow.com/a/999563/1664852

convert -size 200x200 xc:none -fill walter.jpg -draw "circle 100,100 100,1" circle_thumb.png

当我试图用蜻蜓实现相同时,这就是配置:

When I tried to achieve the same with dragonfly, this is the config:

require 'dragonfly/rails/images'

Dragonfly[:images].configure do |c|
  c.job :crop_circle do
    process :resize, "320x440"
    encode :png
    process :convert, '-virtual-pixel HorizontalTile -background transparent -draw "circle 400,400 400,1" -compose Copy_Opacity -composite'
  end
end

I当它在服务器日志中运行时,得到错误没有这样的图像

I get the error no such image when this runs in the server log.

如何配置使用imagemagick命令转换函数?

推荐答案

也许我的解决方案可以帮助其他人正在寻找一种使用Dragonfly宝石生成圆形图像的方法。

Perhaps my solution can help others who are looking for a way to generate rounded images with the Dragonfly gem.

我无法找到现成的解决方案,但我设法将一些东西拼凑起来这里和那里都有一点点。

I was not able to find a solution readily available, but I managed to piece something together by taking a little bit here and there.

事实证明,有一种非常简单的方法可以使用在此解释。

Turns out there is a very easy way to make rounded images with ImageMagick (6.8.9-1) using the vignette option which is explained here.

以下命令行将生成一个透明背景图像并舍入图像:

The following command line will generate an image with transparent background and the image rounded:

convert profile.png -alpha set -background none -thumbnail 50x50^ -vignette 0x0 rounded_profile.png

我们现在可以通过在dragonfly.rb初始值设定项中添加:舍入处理器来获取个人资料图片的圆形图像,如下所示:

We can now get rounded images for profile pictures by adding a :rounded processor to the dragonfly.rb initializer as shown below:

require 'dragonfly'

# Configure
Dragonfly.app.configure do
  plugin :imagemagick

  # Fictive secret no worries
  secret "64d123456dafb767892c1d28ca6d123456ea4cc373dac117d6d1123456a29d6e"

  url_format "/media/:job/:name"

  datastore :file,
    root_path: Rails.root.join('public/system/dragonfly', Rails.env),
    server_root: Rails.root.join('public')

  processor :rounded do |content, size|
    content.shell_update ext: 'png' do |old_path, new_path|
      "/usr/local/bin/convert #{old_path} -alpha set -background none -thumbnail #{size}^ -vignette 0x0 #{new_path}"
    end
  end
end

请注意,您可能需要更改convert命令的路径,具体取决于您所在的平台运行,我在Mac OS上,ImageMagick是通过Homebrew安装的。

Notice you might have to change the path for your convert command depending on which platform you are running on, I'm on Mac OS and ImageMagick is installed through Homebrew.

现在任何有Dragonfly处理过的图像的模型都可以调用:

Now from any model having an image handled by Dragonfly you can call:

a_model_instance.an_image.rounded('50x50').url

返回50px乘50px的圆角图像。

To return a rounded image which is 50px by 50px.

这篇关于作物与蜻蜓铁路的圆形图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆