简体   繁体   中英

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. I know I could use ImageMagick from the command line and split the file using the following:

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.

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:

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.

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.

I really do feel I have read everything there is on CarrierWave and ImageMagick etc, but stumped.

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. If anyone has any improvements I would be grateful.

Thanks to Ryan Bates' Railscast on ImageMagick which pointed me in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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