Guest User

Untitled

a guest
Jan 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. module Paperclip
  2. class Watermark < Processor
  3.  
  4. ##
  5. # A Paperclip::Processor for watermarking images with imagemagick's
  6. # composite command.
  7. #
  8. # Place this code in lib/paperclip_processors/watermark.rb or into a Rails initializer.
  9. #
  10. # Example: All orginal files are resized to be at most 480 pixels in
  11. # height.
  12. #
  13. # class User < ActiveRecord::Base
  14. # has_attached_file :avatar, :processors => [:thumbnail, :watermark],
  15. # :styles => { :original => "x480>" }
  16. #
  17. # attr_accessor :watermark
  18. # end
  19. #
  20. # Creation: the watermark attribute *must* be set on the new instance
  21. # before a file is assigned to the attachment e.g.
  22. #
  23. # user = User.create! do |user|
  24. # user.watermark = Rails.root.join('public/images/rails.png').to_s
  25. # user.avatar = File.new('/home/mike/Desktop/photo.jpg')
  26. # end
  27.  
  28. def initialize file, options = {}, attachment = nil
  29. @file = file
  30. @options = options
  31. @attachment = attachment
  32.  
  33. @enabled = attachment.content_type =~ /^image\/.*/
  34. @whiny = options[:whiny].nil? ? true : options[:whiny]
  35. @format = options[:format]
  36. @current_format = File.extname(@file.path)
  37. @basename = File.basename(@file.path, @current_format)
  38.  
  39. @watermark = attachment.instance.watermark
  40. end
  41.  
  42. def make
  43. if @enabled
  44. watermark
  45. else
  46. noop
  47. end
  48. end
  49.  
  50. def watermark
  51. src = @file
  52. format = @format.blank? ? @current_format : @format
  53. basename = @basename.blank? ? 'file' : @basename
  54.  
  55. dst = Tempfile.new([basename, format].compact.join("."))
  56. dst.binmode
  57.  
  58. parameters = "-gravity southeast :watermark :source :dest"
  59. begin
  60. success = Paperclip.run("composite", parameters,
  61. :watermark => @watermark,
  62. :source => File.expand_path(src.path),
  63. :dest => File.expand_path(dst.path))
  64.  
  65. rescue Cocaine::CommandLineError
  66. raise PaperclipError, "There was an command line error with imagemagick's composite command for #{@basename}" if @whiny
  67. rescue Cocaine::ExitStatusError => e
  68. raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny
  69. rescue Cocaine::CommandNotFoundError => e
  70. raise Paperclip::CommandNotFoundError.new("Could not run the `convert` command. Please install imagemagick.")
  71. end
  72.  
  73. dst
  74. end
  75.  
  76. def noop
  77. src = @file
  78. format = @format.blank? ? @current_format : @format
  79. basename = @basename.blank? ? 'file' : @basename
  80.  
  81. dst = Tempfile.new([@basename, format].compact.join("."))
  82. dst.binmode
  83.  
  84. src.rewind
  85. dst.write(src.read)
  86. dst.flush
  87. dst.rewind
  88.  
  89. dst
  90. end
  91.  
  92. end
  93. end
Add Comment
Please, Sign In to add comment