Module: Fleximage::Model::InstanceMethods

Defined in:
lib/fleximage/model.rb

Overview

Provides methods that every model instance that acts_as_fleximage needs.

Instance Method Summary collapse

Instance Method Details

#delete_image_fileObject

Delete the image file for this record. This is automatically ran after this record gets destroyed, but you can call it manually if you want to remove the image from the record.



522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/fleximage/model.rb', line 522

def delete_image_file
  return unless self.class.has_store?
  
  if self.class.db_store?
    update_attribute :image_file_data, nil unless frozen?
  else
    File.delete(file_path) if File.exists?(file_path)
  end
  
  clear_magic_attributes
  
  self
end

#directory_pathObject

Returns the path to the master image file for this record.

@some_image.directory_path #=> /var/www/myapp/uploaded_images

If this model has a created_at field, it will use a directory structure based on the creation date, to prevent hitting the OS imposed limit on the number files in a directory.

@some_image.directory_path #=> /var/www/myapp/uploaded_images/2008/3/30


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/fleximage/model.rb', line 251

def directory_path
  directory = self.class.image_directory
  raise 'No image directory was defined, cannot generate path' unless directory
  
  # base directory
  directory = "#{Rails.root}/#{directory}" unless /^\// =~ directory
  
  # specific creation date based directory suffix.
  creation = self[:created_at] || self[:created_on]
  if self.class.use_creation_date_based_directories && creation 
    "#{directory}/#{creation.year}/#{creation.month}/#{creation.day}"
  else
    directory
  end
end

#extensionObject

Returns original format of the image if the image_format column exists otherwise returns the globally set format.



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/fleximage/model.rb', line 276

def extension
  if self.respond_to?( :image_format)
    case image_format
    when "JPEG"
      "jpg"
    else
      image_format ? image_format.downcase : self.class.image_storage_format
    end
  else
    self.class.image_storage_format
  end
end

#file_pathObject

Returns the path to the master image file for this record.

@some_image.file_path #=> /var/www/myapp/uploaded_images/123.png


270
271
272
# File 'lib/fleximage/model.rb', line 270

def file_path
  "#{directory_path}/#{id}.#{extension}"
end

#has_image?Boolean

Return true if this record has an image.

Returns:

  • (Boolean)


432
433
434
# File 'lib/fleximage/model.rb', line 432

def has_image?
  @uploaded_image || @output_image || has_saved_image?
end

#has_saved_image?Boolean

Returns:

  • (Boolean)


436
437
438
439
440
441
442
# File 'lib/fleximage/model.rb', line 436

def has_saved_image?
  if self.class.db_store?
    !!image_file_data
  elsif self.class.file_store?
    File.exists?(file_path)
  end
end

#image_fileObject



353
354
355
# File 'lib/fleximage/model.rb', line 353

def image_file
  has_image?
end

#image_file=(file) ⇒ Object

Sets the image file for this record to an uploaded file. This can be called directly, or passively like from an ActiveRecord mass assignment.

Rails will automatically call this method for you, in most of the situations you would expect it to.

# via mass assignment, the most common form you'll probably use
Photo.new(params[:photo])
Photo.create(params[:photo])

# via explicit assignment hash
Photo.new(:image_file => params[:photo][:image_file])
Photo.create(:image_file => params[:photo][:image_file])

# Direct Assignment, usually not needed
photo = Photo.new
photo.image_file = params[:photo][:image_file]

# via an association proxy
p = Product.find(1)
p.images.create(params[:photo])


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/fleximage/model.rb', line 315

def image_file=(file)
  if self.class.image_file_exists(file)
    
    file_path=file.path unless file.is_a? StringIO
    file_path=file.tempfile.path  if file.is_a?( ActionDispatch::Http::UploadedFile ) 

    # Create RMagick Image object from uploaded file
    if file_path
      @uploaded_image = Magick::Image.read(file_path).first
    else
      @uploaded_image = Magick::Image.from_blob(file.read).first
    end
    
    # Sanitize image data
    @uploaded_image.colorspace  = Magick::RGBColorspace
    @uploaded_image.density     = '72'
    
    # Save meta data to database
    set_magic_attributes(file)
    
    # Success, make sure everything is valid
    @invalid_image = false
    save_temp_image(file) unless @dont_save_temp
  end
rescue Magick::ImageMagickError => e
  error_strings = [
    'Improper image header',
    'no decode delegate for this image format',
    'UnableToOpenBlob',
    'Must specify image size'
  ]
  if e.to_s =~ /#{error_strings.join('|')}/
    @invalid_image = true
  else
    raise e
  end
end

#image_file_base64=(data) ⇒ Object

Set the image for this record by reading in a file as a base64 encoded string.

data = Base64.encode64(File.read('my_image_file.jpg'))
photo = Photo.find(123)
photo.image_file_base64 = data
photo.save


403
404
405
# File 'lib/fleximage/model.rb', line 403

def image_file_base64=(data)
  self.image_file_string = Base64.decode64(data)
end

#image_file_string=(data) ⇒ Object

Set the image for this record by reading in file data as a string.

data = File.read('my_image_file.jpg')
photo = Photo.find(123)
photo.image_file_string = data
photo.save


393
394
395
# File 'lib/fleximage/model.rb', line 393

def image_file_string=(data)
  self.image_file = StringIO.new(data)
end

#image_file_temp=(file_name) ⇒ Object

Sets the uploaded image to the name of a file in Rails.root/tmp that was just uploaded. Use as a hidden field in your forms to keep an uploaded image when validation fails and the form needs to be redisplayed



410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/fleximage/model.rb', line 410

def image_file_temp=(file_name)
  if !@uploaded_image && file_name && file_name.present? && file_name !~ %r{\.\./}
    @image_file_temp = file_name
    file_path = "#{Rails.root}/tmp/fleximage/#{file_name}"
    
    @dont_save_temp = true
    if File.exists?(file_path)
      File.open(file_path, 'rb') do |f|
        self.image_file = f
      end
    end
    @dont_save_temp = false
  end
end

#image_file_urlObject

Return the @image_file_url that was previously assigned. This is not saved in the database, and only exists to make forms happy.



427
428
429
# File 'lib/fleximage/model.rb', line 427

def image_file_url
  @image_file_url
end

#image_file_url=(file_url) ⇒ Object

Assign the image via a URL, which will make the plugin go and fetch the image at the provided URL. The image will be stored locally as a master image for that record from then on. This is intended to be used along side the image upload to allow people the choice to upload from their local machine, or pull from the internet.

@photo.image_file_url = 'http://foo.com/bar.jpg'


364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/fleximage/model.rb', line 364

def image_file_url=(file_url)
  @image_file_url = file_url
  if file_url =~ %r{^(https?|ftp)://}
    file = open(URI.parse(URI.encode(file_url)))
    
    # Force a URL based file to have an original_filename
    eval <<-CODE
      def file.original_filename
        "#{file_url}"
      end
    CODE
    
    self.image_file = file
    
  elsif file_url.empty?
    # Nothing to process, move along
    
  else
    # invalid URL, raise invalid image validation error
    @invalid_image = true
  end
end

#load_imageObject

Load the image from disk/DB, or return the cached and potentially processed output image.



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/fleximage/model.rb', line 471

def load_image #:nodoc:
  @output_image ||= @uploaded_image
  
  # Return the current image if we have loaded it already
  return @output_image if @output_image
  
  # Load the image from disk
  if self.class.db_store?
    # Load the image from the database column
    if image_file_data && image_file_data.present?
      @output_image = Magick::Image.from_blob(image_file_data).first
    end
            
  else
    # Load the image from the disk
    @output_image = Magick::Image.read(file_path).first
  
  end
  
  if @output_image
    @output_image
  else
    master_image_not_found
  end
  
rescue Magick::ImageMagickError => e
  if e.to_s =~ /unable to open (file|image)/
    master_image_not_found
  else
    raise e
  end
end

#operate(&block) ⇒ Object

Call from a .flexi view template. This enables the rendering of operators so that you can transform your image. This is the method that is the foundation of .flexi views. Every view should consist of image manipulation code inside a block passed to this method.

# app/views/photos/thumb.jpg.flexi
@photo.operate do |image|
  image.resize '320x240'
end


453
454
455
456
457
458
459
# File 'lib/fleximage/model.rb', line 453

def operate(&block)
  self.tap do
    proxy = ImageProxy.new(load_image, self)
    block.call(proxy)
    @output_image = proxy.image
  end
end

#operate!(&block) ⇒ Object

Self destructive operate. This will modify the master image for this record with the updated and processed result of the operation AND SAVES THE RECORD



463
464
465
466
467
# File 'lib/fleximage/model.rb', line 463

def operate!(&block)
  operate(&block)
  self.image_file_string = output_image
  save
end

#output_image(options = {}) ⇒ Object

Convert the current output image to a jpg, and return it in binary form. options support a :format key that can be :jpg, :gif or :png



506
507
508
509
510
511
512
513
514
515
516
517
518
# File 'lib/fleximage/model.rb', line 506

def output_image(options = {}) #:nodoc:
  format = (options[:format] || :jpg).to_s.upcase
  @output_image.format = format
  @output_image.strip!
  if format == 'JPG'
    quality = @jpg_compression_quality || self.class.output_image_jpg_quality
    @output_image.to_blob { self.quality = quality }
  else
    @output_image.to_blob
  end
ensure
  GC.start
end

#url_formatObject



289
290
291
# File 'lib/fleximage/model.rb', line 289

def url_format
  extension.to_sym
end

#validate_imageObject

Execute image presence and validity validations.



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/fleximage/model.rb', line 537

def validate_image #:nodoc:
  field_name = (@image_file_url && @image_file_url.present?) ? :image_file_url : :image_file
  
  # Could not read the file as an image
  if @invalid_image
    errors.add field_name, self.class.invalid_image_message
  
  # no image uploaded and one is required
  elsif self.class.require_image && !has_image?
    errors.add field_name, self.class.missing_image_message
  
  # Image does not meet minimum size
  elsif self.class.validates_image_size && !@uploaded_image.nil?
    x, y = Fleximage::Operator::Base.size_to_xy(self.class.validates_image_size)
    
    if @uploaded_image.columns < x || @uploaded_image.rows < y
      errors.add field_name, self.class.image_too_small_message
    end
    
  end
end