Skip to content
Browse files

merge with akira's version.

  • Loading branch information...
2 parents 96f5d8a + 8db49d8 commit 828ea99a2b6f101f63b5faac7068d2b4ad731e56 @fbottin fbottin committed Sep 23, 2011
View
BIN attachment_fu-1.0.2.gem
Binary file not shown.
View
BIN attachment_fu-1.0.3.gem
Binary file not shown.
View
2 attachment_fu.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = %q{attachment_fu}
- s.version = "1.0.0"
+ s.version = "1.0.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Technoweenie"]
View
15 lib/attachment_fu.rb
@@ -1,7 +1,22 @@
require 'tempfile'
require 'active_record'
require 'active_support/dependencies'
+
+Tempfile.class_eval do
+ # overwrite so tempfiles use the extension of the basename. important for rmagick and image science
+ def make_tmpname(basename, n)
+ if(basename.is_a?(Array)) #for image_magick, it passes an array ex: ["mini_magick", ".jpeg"]
+ ext = basename[1]
+ basename = basename[0]
+ else
+ ext = nil || ''
+ end
+ sprintf('%s%d-%d%s', File::basename(basename, ext), $$, n.to_i, ext)
+ end
+end
+
require 'geometry'
+
require 'technoweenie/attachment_fu'
if RUBY_VERSION < '1.9'
View
8 lib/technoweenie/attachment_fu/backends/s3_backend.rb
@@ -183,8 +183,8 @@ def self.included(base) #:nodoc:
end
begin
- @@s3_config_path = base.attachment_options[:s3_config_path] || (RAILS_ROOT + '/config/amazon_s3.yml')
- @@s3_config = @@s3_config = YAML.load(ERB.new(File.read(@@s3_config_path)).result)[RAILS_ENV].symbolize_keys
+ @@s3_config_path = base.attachment_options[:s3_config_path] || (File.join(Rails.root, 'config/amazon_s3.yml'))
+ @@s3_config = @@s3_config = YAML.load(ERB.new(File.read(@@s3_config_path)).result)[Rails.env].symbolize_keys
#rescue
# raise ConfigFileNotFoundError.new('File %s not found' % @@s3_config_path)
end
@@ -291,9 +291,9 @@ def cloudfront_url(thumbnail = nil)
def public_filename(*args)
if attachment_options[:cloudfront]
- cloudfront_url(args)
+ cloudfront_url(args[0])
else
- s3_url(args)
+ s3_url(args[0])
end
end
View
45 lib/technoweenie/attachment_fu/processors/mini_magick_processor.rb
@@ -12,10 +12,10 @@ module ClassMethods
# Yields a block containing an MiniMagick Image for the given binary data.
def with_image(file, &block)
begin
- binary_data = file.is_a?(MiniMagick::Image) ? file : MiniMagick::Image.from_file(file) unless !Object.const_defined?(:MiniMagick)
+ binary_data = file.is_a?(MiniMagick::Image) ? file : MiniMagick::Image.open(file) unless !Object.const_defined?(:MiniMagick)
rescue
# Log the failure to load the image.
- logger.debug("Exception working with image: #{$!}")
+ Rails.logger.debug("Exception working with image: #{$!}")
binary_data = nil
end
block.call binary_data if block && binary_data
@@ -42,14 +42,15 @@ def resize_image(img, size)
commands.strip unless attachment_options[:keep_profile]
# gif are not handled correct, this is a hack, but it seems to work.
- if img.output =~ / GIF /
- img.format("png")
- end
-
+ #if img.output =~ / GIF /
+ # img.format("png")
+ #end
if size.is_a?(Fixnum) || (size.is_a?(Array) && size.first.is_a?(Fixnum))
if size.is_a?(Fixnum)
- size = [size, size]
+ size = [size, size]
commands.resize(size.join('x'))
+ elsif size.is_a?(Array) && size[0] == size[1]
+ resize_and_crop(img, size[0], commands)
else
commands.resize(size.join('x') + '!')
end
@@ -124,9 +125,31 @@ def calculate_offset(image_width,image_height,image_aspect,thumb_width,thumb_hei
# crop image
command
end
-
-
- end
- end
+
+ def resize_and_crop(image, square_size, commands)
+ if image[:width] < image[:height]
+ shave_off = ((image[:height] - image[:width])/2).round
+ if(parent.x1 && parent.y1)
+ crop_width = parent.x2 - parent.x1
+ crop_height = parent.y2 - parent.y1
+ commands.crop("#{crop_width}x#{crop_height}+#{parent.x1}+#{parent.y1}")
+ else
+ commands.shave("0x#{shave_off}")
+ end
+ elsif image[:width] > image[:height]
+ shave_off = ((image[:width] - image[:height])/2).round
+ if(parent.x1 && parent.y1)
+ crop_width = parent.x2 - parent.x1
+ crop_height = parent.y2 - parent.y1
+ commands.crop("#{crop_width}x#{crop_height}+#{parent.x1}+#{parent.y1}")
+ else
+ commands.shave("#{shave_off}x0")
+ end
+ end
+ commands.resize("#{square_size}x#{square_size}")
+ return image
+ end
+ end
+ end
end
end

0 comments on commit 828ea99

Please sign in to comment.
Something went wrong with that request. Please try again.