You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

thumbnail.rb 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require 'fileutils'
  19. module Redmine
  20. module Thumbnail
  21. extend Redmine::Utils::Shell
  22. CONVERT_BIN = (Redmine::Configuration['imagemagick_convert_command'] || 'convert').freeze
  23. GS_BIN = (
  24. Redmine::Configuration['gs_command'] ||
  25. ('gswin64c' if Redmine::Platform.mswin?) ||
  26. 'gs'
  27. ).freeze
  28. ALLOWED_TYPES = %w(image/bmp image/gif image/jpeg image/png application/pdf)
  29. # Generates a thumbnail for the source image to target
  30. def self.generate(source, target, size, is_pdf = false)
  31. return nil unless convert_available?
  32. return nil if is_pdf && !gs_available?
  33. unless File.exist?(target)
  34. # Make sure we only invoke Imagemagick if the file type is allowed
  35. mime_type = File.open(source) {|f| Marcel::MimeType.for(f)}
  36. return nil if !ALLOWED_TYPES.include? mime_type
  37. return nil if is_pdf && mime_type != "application/pdf"
  38. directory = File.dirname(target)
  39. FileUtils.mkdir_p directory
  40. size_option = "#{size}x#{size}>"
  41. if is_pdf
  42. cmd = "#{shell_quote CONVERT_BIN} #{shell_quote "#{source}[0]"} -thumbnail #{shell_quote size_option} #{shell_quote "png:#{target}"}"
  43. else
  44. cmd = "#{shell_quote CONVERT_BIN} #{shell_quote source} -auto-orient -thumbnail #{shell_quote size_option} #{shell_quote target}"
  45. end
  46. unless system(cmd)
  47. logger.error("Creating thumbnail failed (#{$?}):\nCommand: #{cmd}")
  48. return nil
  49. end
  50. end
  51. target
  52. end
  53. def self.convert_available?
  54. return @convert_available if defined?(@convert_available)
  55. begin
  56. `#{shell_quote CONVERT_BIN} -version`
  57. @convert_available = $?.success?
  58. rescue
  59. @convert_available = false
  60. end
  61. logger.warn("Imagemagick's convert binary (#{CONVERT_BIN}) not available") unless @convert_available
  62. @convert_available
  63. end
  64. def self.gs_available?
  65. return @gs_available if defined?(@gs_available)
  66. begin
  67. `#{shell_quote GS_BIN} -version`
  68. @gs_available = $?.success?
  69. rescue
  70. @gs_available = false
  71. end
  72. logger.warn("gs binary (#{GS_BIN}) not available") unless @gs_available
  73. @gs_available
  74. end
  75. def self.logger
  76. Rails.logger
  77. end
  78. end
  79. end