# frozen_string_literal: true
class UserUploader < ApplicationUploader
include CarrierWave::MiniMagick
def generate_image(text)
tmp_path = File.join(Dir.tmpdir, "#{SecureRandom.hex}.png")
MiniMagick::Tool::Convert.new do |img|
img.size "500x200"
img.xc "white" # Background color
img.gravity "Center" # Center the text
img.pointsize "36" # Font size
img.fill "black" # Text color
img.draw "text 0,0 '#{text}'"
img.font "#{Rails.root.join('public', 'font', 'Franklin_Gothic_Heavy_Regular.ttf')}"
img << tmp_path # Save image to this path
end
File.open(tmp_path) # Return file handle
end
end
我从前端发送参数,我在前端使用的字体是 Franklin_Gothic_Heavy_Regular.ttf,定义在 app/assets/fonts/Franklin_Gothic_Heavy_Regular.ttf
为了在上传器中使用它,我在 public/font/Franklin_Gothic_Heavy_Regular.ttf 中添加了相同的字体文件,但是存储图像时它不会显示正确的字体。
我在前端使用 vuejs,在后端使用 rails。请帮我修复这个问题。
ImageMagick 信息
Version: ImageMagick 7.1.0-58 Q16-HDRI x86_64 20802 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(5.0)
Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib
Compiler: gcc (4.2)
放入 img.command.join(" ") 返回
magick convert -size 500x200 xc:white -gravity Center -pointsize 36 -fill black -draw text 0,0 'Testing Font' -font /Users/Documents/work/company/public/font/Franklin_Gothic_Heavy_Regular.ttf /var/folders/tc/3b0wh1xs7z7dms5rxj1l23_00000gn/T/74a90ba3c194bda014cb2706a3fcdd32.png