Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Szajbe committed Jul 30, 2008
0 parents commit 8a4baa9
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 0 deletions.
26 changes: 26 additions & 0 deletions MIT-LICENSE
@@ -0,0 +1,26 @@

LICENSE

The MIT License

Copyright (c) 2008 Michal Szajbe (http://codetunes.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


29 changes: 29 additions & 0 deletions README
@@ -0,0 +1,29 @@
=PaperclipExtended

PaperclipExtended extends Paperclip plugin by Jon Yurek and thoughtbot. It enables user to define additional options that will be passed to ImageMagick convert command after thumbnail generation by Paperclip.

Note that PaperclipExtended plugin is not a replacement for Paperclip. It requires that you have Paperclip plugin already installed.

PaperclipExtended is known to work with Paperclip 2.1.2 (current version at the time of development). Note that extensions provided by PaperclipExtended may be included in Paperclip in future, so this plugin will not be needed anymore.

==Usage

In your model:

class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :commands => { :medium => "-background white -gravity center -extent 300x300 +repage" }
end

The string you pass in commands hash will be attached to convert command after usual thumbnail generation by Paperclip.

The result convert command will be now:
convert -scale "300x300>" -background white -gravity center -extent 300x300 +repage

Instead of just:
convert -scale "300x300>"

The commands parameter is optional, also you can define it only for certain styles as above.

Read ImageMagick Command Line Options documentation for information on what can be put in commands parameter (http://www.imagemagick.org/script/command-line-options.php).

Copyright (c) 2008 Michal Szajbe (http://codetunes.com), released under the MIT license
29 changes: 29 additions & 0 deletions README.rdoc
@@ -0,0 +1,29 @@
=PaperclipExtended

PaperclipExtended extends Paperclip plugin by Jon Yurek and thoughtbot. It enables user to define additional options that will be passed to ImageMagick convert command after thumbnail generation by Paperclip.

Note that PaperclipExtended plugin is not a replacement for Paperclip. It requires that you have Paperclip plugin already installed.

PaperclipExtended is known to work with Paperclip 2.1.2 (current version at the time of development). Note that extensions provided by PaperclipExtended may be included in Paperclip in future, so this plugin will not be needed anymore.

==Usage

In your model:

class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :commands => { :medium => "-background white -gravity center -extent 300x300 +repage" }
end

The string you pass in commands hash will be attached to convert command after usual thumbnail generation by Paperclip.

The result convert command will be now:
convert -scale "300x300>" -background white -gravity center -extent 300x300 +repage

Instead of just:
convert -scale "300x300>"

The commands parameter is optional, also you can define it only for certain styles as above.

Read ImageMagick Command Line Options documentation for information on what can be put in commands parameter (http://www.imagemagick.org/script/command-line-options.php).

Copyright (c) 2008 Michal Szajbe (http://codetunes.com), released under the MIT license
22 changes: 22 additions & 0 deletions Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the paperclip_extended plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the paperclip_extended plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'PaperclipExtended'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
1 change: 1 addition & 0 deletions init.rb
@@ -0,0 +1 @@
require File.join(File.dirname(__FILE__), "lib", "paperclip_extended")
2 changes: 2 additions & 0 deletions lib/paperclip_extended.rb
@@ -0,0 +1,2 @@
require 'paperclip_extended/attachment'
require 'paperclip_extended/thumbnail'
28 changes: 28 additions & 0 deletions lib/paperclip_extended/attachment.rb
@@ -0,0 +1,28 @@
module Paperclip
class Attachment
alias original_initialize initialize

def initialize name, instance, options = {}
@commands = options[:commands] ||= nil

original_initialize name, instance, options
end

def post_process
return if @queued_for_write[:original].nil?
@styles.each do |name, args|
begin
dimensions, format = args
commands = @commands[name] ||= nil unless @commands.nil?
@queued_for_write[name] = Thumbnail.make(@queued_for_write[:original],
dimensions,
format,
commands,
@whiny_thumnails)
rescue PaperclipError => e
@errors << e.message if @whiny_thumbnails
end
end
end
end
end
30 changes: 30 additions & 0 deletions lib/paperclip_extended/thumbnail.rb
@@ -0,0 +1,30 @@
module Paperclip
class Thumbnail
def initialize file, target_geometry, format = nil, commands = nil, whiny_thumbnails = true
@file = file
@crop = target_geometry[-1,1] == '#'
@target_geometry = Geometry.parse target_geometry
@current_geometry = Geometry.from_file file
@whiny_thumbnails = whiny_thumbnails

@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)

@commands = commands

@format = format
end

def self.make file, dimensions, format = nil, commands = nil, whiny_thumbnails = true
new(file, dimensions, format, commands, whiny_thumbnails).make
end

def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = "-scale \"#{scale}\""
trans << " -crop \"#{crop}\" +repage" if crop
trans << " #{@commands}" unless @commands.nil?
trans
end
end
end
8 changes: 8 additions & 0 deletions test/paperclip_extended_test.rb
@@ -0,0 +1,8 @@
require 'test/unit'

class PaperclipExtendedTest < Test::Unit::TestCase
# Replace this with your real tests.
def test_this_plugin
flunk
end
end

0 comments on commit 8a4baa9

Please sign in to comment.