Module: PDFThumbnail
- Included in:
- Filters
- Defined in:
- lib/jekyll-pdf-thumbnail.rb
Defined Under Namespace
Modules: Filters
Constant Summary collapse
- CACHE_DIR =
"/assets/pdf_thumbnails/"
- HASH_LENGTH =
32
- @@cache_dir_present =
false
Instance Method Summary collapse
- #_dest_filename(src_path, resize, quality) ⇒ Object
- #_get_cache_dir(site) ⇒ Object
- #_must_create?(src_path, dest_path) ⇒ Boolean
- #generate_thumbnail(pdf, thumbnail, resize = '50%', quality = "80%") ⇒ Object
Instance Method Details
#_dest_filename(src_path, resize, quality) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jekyll-pdf-thumbnail.rb', line 22 def _dest_filename(src_path, resize, quality) # Generates a thumbnail name using the SHA256 digest of the PDF file # # Example: # >> _dest_filename("sample_1.pdf") # => a35383ccca791ba6aa67ab3acde65287.png # # Arguments: # src_path: (String) # resize: (String) as used in the -resize parameter of convert # quality: (String) as used in the -quality parameter of convert hash = Digest::SHA256.file(src_path) short_hash = hash.hexdigest()[0, HASH_LENGTH] basename = short_hash if resize cleaned = resize.gsub(/[^\da-z]+/i, "") basename << "_r#{cleaned}" end if quality cleaned = quality.gsub(/[^\da-z]+/i, "") basename << "_q#{cleaned}" end basename << ".png" end |
#_get_cache_dir(site) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/jekyll-pdf-thumbnail.rb', line 13 def _get_cache_dir(site) full_cache_dir = File.join(site.source, CACHE_DIR) if not @@cache_dir_present FileUtils.mkdir_p(full_cache_dir) @@cache_dir_present = true end full_cache_dir end |
#_must_create?(src_path, dest_path) ⇒ Boolean
47 48 49 50 |
# File 'lib/jekyll-pdf-thumbnail.rb', line 47 def _must_create?(src_path, dest_path) # Returns true if dest_path doesn't exists or src_path is newer than dest_path !File.exist?(dest_path) || File.mtime(dest_path) <= File.mtime(src_path) end |
#generate_thumbnail(pdf, thumbnail, resize = '50%', quality = "80%") ⇒ Object
52 53 54 55 56 |
# File 'lib/jekyll-pdf-thumbnail.rb', line 52 def generate_thumbnail(pdf, thumbnail, resize='50%', quality="80%") first = PDFToImage.open(pdf).first first.args << "-thumbnail #{resize}" first.save(thumbnail) end |