简体   繁体   English

Carrierwave,ImageMagick:平铺,存储和调用图块

[英]Carrierwave, ImageMagick: tiling, storing and recalling the tiles of image

I have searched high and low for more information relating to being able to use Carrierwave to process an uploaded file by splitting it into 16 tiles. 我在上下搜索了更多信息,这些信息与能够使用Carrierwave将上传的文件拆分成16个图块来进行处理有关。 I know I could use ImageMagick from the command line and split the file using the following: 我知道我可以从命令行使用ImageMagick并使用以下方法拆分文件:

convert imagename: -crop 4x4@ +repage +adjoin imagename_%d.gif

For example. 例如。 And this works perfectly to tile and save the file from rose_0.gif to rose_15.gif. 这非常适合平铺文件并将其从rose_0.gif保存到rose_15.gif。

I can't find anything using RMagick which will perform the same function, but stumbled across the following code to perform the same function using Ruby on Rails etc: 我找不到使用RMagick可以执行相同功能的任何东西,但是偶然发现以下代码使用Ruby on Rails等来执行相同功能:

def split_images
  #'image' is a ImageMagick Object
  width  = image.cols/number_cols
  height = image.rows/nubmer_rows
  images = []
  0.upto(number_rows-1) do |x|
    0.upto(number_cols-1) do |y|
      images << image.crop( Magick::NorthWestGravity, x*width, y*height, width, height, true)
    end
  end
end

I'm trying to upload an image from an iOS app and have Carrierwave tile the image by writing a version: routine in Carrierwave to handle the processing. 我正在尝试从iOS应用程序上传图像,并通过在Carrierwave中编写version:例程来处理该过程,以使Carrierwave平铺该图像。

Is it possible to be able to do this? 是否有可能做到这一点? If so, how will the files be saved? 如果是这样,将如何保存文件? And, more importantly, how do I then 'call' or reference the files in a view? 而且,更重要的是,然后如何在视图中“调用”或引用文件? Can someone please point me in the right direction? 有人可以指出正确的方向吗? Or suggest an alternative approach? 还是建议其他方法?

I know I could probably write 16 different version: commands, but this seems a little silly. 我知道我可能会写16种不同的版本:命令,但这似乎有点愚蠢。

I really do feel I have read everything there is on CarrierWave and ImageMagick etc, but stumped. 我确实确实已经阅读了CarrierWave和ImageMagick等上的所有内容,但是很困惑。

Any help would be gratefully received. 任何帮助将不胜感激。

Thanks in advance 提前致谢

I believe I have pretty much worked it out using the following and provide the answer just in case anyone else is looking to do the same thing: 我相信我已经使用以下方法解决了这个问题,并提供了答案,以防万一其他人希望做同样的事情:

# encoding: utf-8

class PhotoUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  process :resize_to_limit => [640, 1136]
  process :convert => 'png'

  NUMBER_OF_COLS = 4
  NUMBER_OF_ROWS = 4

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :small do
    process :resize_to_limit => [100, 100]
  end

  0.upto(NUMBER_OF_ROWS-1) do |x|
    0.upto(NUMBER_OF_COLS-1) do |y|
      name = "image#{x}#{y}"
      version(name) {
        process :name => [x,y,160,284]
      }
    end
  end

  def name(x,y,width,height)
    manipulate!(:format => 'png') do |img|
      img.crop(Magick::NorthWestGravity, x*width, y*height, width, height, true)
    end
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_white_list
    %w(jpg jpeg gif png)
  end

end

I'm still not happy with having to hard-code the dimensions of width and height into the code, but couldn't get the @geometry stuff to work to pass me the actual images width and height, but it does the job at the moment. 我仍然对将宽度和高度的尺寸硬编码到代码中仍然不满意,但是无法让@geometry东西起作用以将实际图像的宽度和高度传递给我,但是它确实可以时刻。 If anyone has any improvements I would be grateful. 如果有人有任何改进,我将不胜感激。

Thanks to Ryan Bates' Railscast on ImageMagick which pointed me in the right direction. 多亏了Ryan Bates在ImageMagick上发布的Railscast ,它为我指明了正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM