代码之家  ›  专栏  ›  技术社区  ›  Mike Sutton

使用附件调整Flickr样式的大小

  •  1
  • Mike Sutton  · 技术社区  · 15 年前

    我想用类似于flickr、facebook和twitter的方式来调整我的缩略图大小:如果我想要一个100x100的缩略图,我希望缩略图精确到100x100,任何多余的部分都被剪掉,这样就可以保留纵横比。

    4 回复  |  直到 15 年前
        1
  •  1
  •   pkaeding    15 年前

    要设置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,任何多余的裁剪,以便保持纵横比。”

        2
  •  0
  •   deb    15 年前

    将此添加到您的模型中

    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/

        3
  •  0
  •   tadman    15 年前

    规范中有一个裁剪指令:

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

    编辑:

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

        4
  •  0
  •   Mike Sutton    15 年前

      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”作为几何字符串。注意,上面的代码假设所需的输出是正方形的。

    推荐文章