在其他参数之前停止回形剪裁和调整大小(Stop paperclip from cropping and resizing before other parameters)

编程入门 行业动态 更新时间:2024-10-17 22:18:12
在其他参数之前停止回形剪裁和调整大小(Stop paperclip from cropping and resizing before other parameters)

我正在使用Paperclip来存储我的图像,我想创建一个裁剪/旋转的图像作为缩略图。 这是Paperclip应该运行的PROPER命令:

convert [file].jpg -gravity center -distort SRT -30 -quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0 -crop 433x433+69+88 +repage -resize "300x300>" [file].jpg

这产生了我想要的结果。 我已经在安装了imagemagick的电脑上直接测试了它。 但是查看我的服务器上的日志,这些参数的顺序是不同的。 Paperclip想要a)把-resize "300x300>"命令-crop 433x433+69+88 ,然后把-crop 433x433+69+88 SECOND,然后把其余的参数放在-crop 433x433+69+88 。 这会改变最终图像的外观! 不是我想要的。 这是它在日志中输出的内容:

convert [file].jpg -auto-orient -resize "300x300>" -crop 433x433+69+88 +repage -gravity center -distort SRT -30 -quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0 [file].jpg

...这是我模型中的配置:

Wine.rb

has_attached_file :photo, :styles => { :thumb => { :geometry => "300x300>", :format => :jpg, :processors => [:cropper, :recursive_thumbnail], :thumbnail => :croppable }, :general => ["150x375", :jpg], :show => ["x425", :jpg], :croppable => ["1200x1200>", :jpg] }, :url => "/assets/wines/:style/:wine_name", :path => ":rails_root/public:url", :default_url => ":wine_default", :default_path => ":rails_root/public:wine_default", :default_style => :show, :convert_options => { :thumb => '-gravity center -distort SRT -30', :croppable => '-gravity center -extent 1200x1200', :general => '-gravity center -extent 150x375 -quality 95', :all => '-quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0' }, :processors => [:thumbnail, :compression]

基本上它按以下顺序运行convert.exe:[:geometry] [:transformations] [:convert_options]。

我如何按照我想要的顺序得到东西?

recursive_thumbnail.rb - 用于运行:thumb thumbnail生成:croppable而不是原始文件(因为裁剪时出现水平填充问题)

module Paperclip class RecursiveThumbnail < Thumbnail def initialize file, options = {}, attachment = nil super Paperclip.io_adapters.for(attachment.styles[options[:thumbnail] || :original]), options, attachment end end end

cropper.rb

module Paperclip class Cropper < Thumbnail def transformation_command if crop_command super.join(' ').sub(/ -crop \S+/, '').split(' ') + crop_command else super end end def crop_command target = @attachment.instance if target.cropping? ["+repage", "-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}", "+repage"] end end end end

I am using Paperclip to store my image, and I want to create a cropped/rotated image as a thumbnail. Here is the PROPER command that Paperclip should be running:

convert [file].jpg -gravity center -distort SRT -30 -quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0 -crop 433x433+69+88 +repage -resize "300x300>" [file].jpg

This produces the result I want. I've tested it directly on my pc having installed imagemagick. However looking at the logs on my server, the order of these arguments is different. Paperclip wants to a) Put the -resize "300x300>" command FIRST, THEN puts -crop 433x433+69+88 SECOND, and THEN put the rest of the arguments afterwards. This changes the look of the final image! Not what I want. Here's what it outputs in the logs:

convert [file].jpg -auto-orient -resize "300x300>" -crop 433x433+69+88 +repage -gravity center -distort SRT -30 -quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0 [file].jpg

...and here's my config in my model:

Wine.rb

has_attached_file :photo, :styles => { :thumb => { :geometry => "300x300>", :format => :jpg, :processors => [:cropper, :recursive_thumbnail], :thumbnail => :croppable }, :general => ["150x375", :jpg], :show => ["x425", :jpg], :croppable => ["1200x1200>", :jpg] }, :url => "/assets/wines/:style/:wine_name", :path => ":rails_root/public:url", :default_url => ":wine_default", :default_path => ":rails_root/public:wine_default", :default_style => :show, :convert_options => { :thumb => '-gravity center -distort SRT -30', :croppable => '-gravity center -extent 1200x1200', :general => '-gravity center -extent 150x375 -quality 95', :all => '-quality 100 -antialias -flatten -background white -unsharp 0.3x0.3+5+0' }, :processors => [:thumbnail, :compression]

Basically it runs convert.exe in this order: [:geometry][:transformations][:convert_options].

How do I get things in the order I want?

recursive_thumbnail.rb -- for running the :thumb thumbnail generation off of :croppable and not the original file (because of horizontal padding issues when cropping)

module Paperclip class RecursiveThumbnail < Thumbnail def initialize file, options = {}, attachment = nil super Paperclip.io_adapters.for(attachment.styles[options[:thumbnail] || :original]), options, attachment end end end

cropper.rb

module Paperclip class Cropper < Thumbnail def transformation_command if crop_command super.join(' ').sub(/ -crop \S+/, '').split(' ') + crop_command else super end end def crop_command target = @attachment.instance if target.cropping? ["+repage", "-crop", "#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}", "+repage"] end end end end

最满意答案

我尝试创建自己的回形针处理器,但我遇到了问题。 我的处理器覆盖了cropper.rb处理器,因为他们都试图在缩略图处理器中使用相同的方法; 所以我可以将resize命令移动到参数列表的末尾,但庄稼参数无处可寻。 我无法弄清楚如何在我的自定义处理器中包含裁剪处理器,整个事情看起来像是一团糟,它无法正常工作。

找不到答案。 我最终切换到Carrierwave并遇到另一组问题,我终于找到了解决方案。 太糟糕Carrierwave没有记录图像的处理,但这是一个很小的代价。

Carrierwave RMagick没有删除转换为jpg的透明度 Carrierwave + repage选项不起作用

I tried creating my own paperclip processor, but I ran into issues. My processor was overriding the cropper.rb processor as they both tried to use the same method within the Thumbnail processor; so I could move the resize command to the end of the arguments list but the crop argument was nowhere to be found. I couldn't figure out how to include the cropper processor inside of my custom processor, and the whole thing just seemed like a mess and it wasn't working.

No answer found. I ended up switching to Carrierwave and encountering another set of problems which I've finally arrived at a solution for. Too bad Carrierwave doesn't log the processing of images, but that's a small price to pay.

Carrierwave RMagick not removing transparency in convert to jpg Carrierwave +repage option not working

更多推荐

本文发布于:2023-07-15 05:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110749.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:剪裁   大小   参数   Stop   parameters

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!