使用 Attachment_fu 调整 Flickr 样式大小

发布于 2024-08-21 01:39:41 字数 144 浏览 6 评论 0原文

我希望 Attachment_fu 能够以与 flickr、facebook 和 twitter 处理方式类似的方式调整缩略图大小:如果我想要 100x100 的缩略图,我希望缩略图恰好为 100x100,并剪掉任何多余部分,以便保留纵横比。

有什么想法吗?

I want attachment_fu to resize my thumbnails in a similar way to how flickr, facebook and twitter handle this: If I want a 100x100 thumbnail I want the thumbnail to be exactly 100x100 with any excess cropped off so that the aspect ratio is preserved.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↙温凉少女 2024-08-28 01:39:41

要设置 100x100 缩略图,请将以下内容添加到您的模型中:(

  has_attachment :content_type => :image,
                 :storage => IMAGE_STORAGE,
                 :max_size => 20.megabytes,
                 :thumbnails => {
                   :thumb  => '100x100>',
                   :large  => '800x600>',
                 }

在本示例中,除了保持原始大小之外,我还创建了 100x100 缩略图,以及 800x600“大”尺寸。)

此外,请记住缩略图可能不完全是 100x100;其最大尺寸为 100x100。这意味着,如果原件的宽高比为 4:3,则缩略图将为 100x75。我不太确定这是否是您所说的“正好 100x100,剪掉任何多余部分以保留纵横比”的意思。

To set up the 100x100 thumbnails, add the following to your model:

  has_attachment :content_type => :image,
                 :storage => IMAGE_STORAGE,
                 :max_size => 20.megabytes,
                 :thumbnails => {
                   :thumb  => '100x100>',
                   :large  => '800x600>',
                 }

(In this example, I am creating a 100x100 thumbnail, and also an 800x600 'large' size, in additional to keeping the original size.)

Also, keep in mind that the thumbnail might not be exactly 100x100; it will have a maximum dimension of 100x100. This means that if the original has an aspect ration of 4:3, the thumbnail would be 100x75. I'm not exactly sure if that is what you meant by "exactly 100x100 with any excess cropped off so that the aspect ratio is preserved."

北方。的韩爷 2024-08-28 01:39:41

将其添加到您的模型中

protected  

  # Override image resizing method  
  def resize_image(img, size)  
    # resize_image take size in a number of formats, we just want  
    # Strings in the form of "crop: WxH"  
    if (size.is_a?(String) && size =~ /^crop: (\d*)x(\d*)/i) ||  
        (size.is_a?(Array) && size.first.is_a?(String) &&  
          size.first =~ /^crop: (\d*)x(\d*)/i)  
      img.crop_resized!($1.to_i, $2.to_i)  
      # We need to save the resized image in the same way the  
      # orignal does.  
      self.temp_path = write_to_temp_file(img.to_blob)  
    else  
      super # Otherwise let attachment_fu handle it  
    end  
  end

并将缩略图大小更改为:

:thumbnails => {:thumb => 'crop: 100x100' }

来源:

http://stuff-things.net/2008/02/21/quick-and-dirty-cropping-images-with-attachment_fu/

Add this to your model

protected  

  # Override image resizing method  
  def resize_image(img, size)  
    # resize_image take size in a number of formats, we just want  
    # Strings in the form of "crop: WxH"  
    if (size.is_a?(String) && size =~ /^crop: (\d*)x(\d*)/i) ||  
        (size.is_a?(Array) && size.first.is_a?(String) &&  
          size.first =~ /^crop: (\d*)x(\d*)/i)  
      img.crop_resized!($1.to_i, $2.to_i)  
      # We need to save the resized image in the same way the  
      # orignal does.  
      self.temp_path = write_to_temp_file(img.to_blob)  
    else  
      super # Otherwise let attachment_fu handle it  
    end  
  end

and change the thumbnail size to:

:thumbnails => {:thumb => 'crop: 100x100' }

source:

http://stuff-things.net/2008/02/21/quick-and-dirty-cropping-images-with-attachment_fu/

网白 2024-08-28 01:39:41

规范中可以给出一个裁剪指令:

has_attachment :content_type => :image,
  :thumbnails => {
    :thumb  => '100x100#'
}

Memonic: '#' 看起来像裁剪工具。

编辑:更正

has_attachment :content_type => :image,
  :thumbnails => {
    :thumb  => '100x100!'
}

以前的方法适用于回形针,它具有不同的符号。

There's a cropping directive that can be given in the specification:

has_attachment :content_type => :image,
  :thumbnails => {
    :thumb  => '100x100#'
}

Memonic: '#' looks like the crop tool.

Edit: Correction

has_attachment :content_type => :image,
  :thumbnails => {
    :thumb  => '100x100!'
}

The previous method was for Paperclip which has a different notation.

莳間冲淡了誓言ζ 2024-08-28 01:39:41

我的解决方案是深入研究 Attachment_fu 插件文件夹(vendor/plugins)并编辑 rmagick_processor.rb 文件。首先,我将 resize_image 重命名为 resize_image_internal,然后添加:

  def resize_image(img, size)  
    # resize_image take size in a number of formats, we just want  
    # Strings in the form of "square: WxH"  
    if (size.is_a?(String) && size =~ /^square: (\d*)x(\d*)/i) ||  
        (size.is_a?(Array) && size.first.is_a?(String) &&  
          size.first =~ /^square: (\d*)x(\d*)/i)  
        iw, ih = img.columns, img.rows
        aspect = iw.to_f / ih.to_f
        if aspect > 1
            shave_off = (iw - ih) / 2
            img.shave!(shave_off, 0)
        else
            shave_off = (ih-iw) / 2
            img.shave!(0, shave_off)
        end
        resize_image_internal(img, "#{$1}x#{$2}!")
    else  
      resize_image_internal(img, size) # Otherwise let attachment_fu handle it  
    end  
  end

我现在可以使用 'square: 100x100' 作为我的几何字符串。请注意,上面的代码假设所需的输出是平方的。

My solution was to delve into the attachment_fu plugin folder (vendor/plugins) and edit the rmagick_processor.rb file. First I renamed resize_image to resize_image_internal, then added:

  def resize_image(img, size)  
    # resize_image take size in a number of formats, we just want  
    # Strings in the form of "square: WxH"  
    if (size.is_a?(String) && size =~ /^square: (\d*)x(\d*)/i) ||  
        (size.is_a?(Array) && size.first.is_a?(String) &&  
          size.first =~ /^square: (\d*)x(\d*)/i)  
        iw, ih = img.columns, img.rows
        aspect = iw.to_f / ih.to_f
        if aspect > 1
            shave_off = (iw - ih) / 2
            img.shave!(shave_off, 0)
        else
            shave_off = (ih-iw) / 2
            img.shave!(0, shave_off)
        end
        resize_image_internal(img, "#{$1}x#{$2}!")
    else  
      resize_image_internal(img, size) # Otherwise let attachment_fu handle it  
    end  
  end

I can now use 'square: 100x100' as my geometry string. Note that the above code assumes the required output is square.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文