Class: Lcms::Engine::GenerateThumbnails
- Inherits:
-
Object
- Object
- Lcms::Engine::GenerateThumbnails
- Defined in:
- app/services/lcms/engine/generate_thumbnails.rb
Constant Summary collapse
- MEDIAS =
to keep only (facebook pinterest)
SocialThumbnail::MEDIAS.slice(1, 2)
Instance Attribute Summary collapse
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Instance Method Summary collapse
- #convert_to_png(file_path) ⇒ Object
- #generate ⇒ Object
-
#initialize(resource) ⇒ GenerateThumbnails
constructor
A new instance of GenerateThumbnails.
- #resource_type ⇒ Object
- #svg_content(media) ⇒ Object
- #tmp_dir ⇒ Object
Constructor Details
#initialize(resource) ⇒ GenerateThumbnails
Returns a new instance of GenerateThumbnails.
10 11 12 13 |
# File 'app/services/lcms/engine/generate_thumbnails.rb', line 10 def initialize(resource) @resource = resource FileUtils.mkdir_p tmp_dir end |
Instance Attribute Details
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
6 7 8 |
# File 'app/services/lcms/engine/generate_thumbnails.rb', line 6 def resource @resource end |
Instance Method Details
#convert_to_png(file_path) ⇒ Object
45 46 47 48 49 50 51 |
# File 'app/services/lcms/engine/generate_thumbnails.rb', line 45 def convert_to_png(file_path) tmp_path = file_path.to_s.gsub('.svg', '.tmp.png') png_path = file_path.to_s.gsub('.svg', '.png') `svgexport #{file_path} #{tmp_path}` `pngquant #{tmp_path} -o #{png_path} --speed=1 --force` png_path end |
#generate ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/services/lcms/engine/generate_thumbnails.rb', line 15 def generate MEDIAS.each do |media| file_path = tmp_dir.join("#{resource_type}_#{resource.id}_#{media}.svg") # generate tmp svg file File.open(file_path, 'w') { |f| f.write(svg_content(media)) } # convert to png png_path = convert_to_png(file_path) # save thumbnail to s3 thumb = SocialThumbnail.find_or_initialize_by(media: media.to_sym, target: resource) thumb.image = File.open(png_path) thumb.save end # clean tmp image files files = Dir.glob(tmp_dir.join "#{resource_type}_#{resource.id}_*.*") FileUtils.rm files end |
#resource_type ⇒ Object
37 38 39 |
# File 'app/services/lcms/engine/generate_thumbnails.rb', line 37 def resource_type resource.class.name.underscore end |
#svg_content(media) ⇒ Object
41 42 43 |
# File 'app/services/lcms/engine/generate_thumbnails.rb', line 41 def svg_content(media) SVGSocialThumbnail.new(resource, media: media).render end |
#tmp_dir ⇒ Object
33 34 35 |
# File 'app/services/lcms/engine/generate_thumbnails.rb', line 33 def tmp_dir Rails.root.join('tmp', 'svgs') end |