Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to other image formats #70

Closed
elalemanyo opened this issue Feb 3, 2024 · 3 comments
Closed

Convert to other image formats #70

elalemanyo opened this issue Feb 3, 2024 · 3 comments

Comments

@elalemanyo
Copy link

Hi,
It may not have much to do with the gem, but some good idea to be able to have other formats besides svg, such as jpg or png.
BTW: Thank you very much for the gem

@DannyBen
Copy link
Owner

DannyBen commented Feb 3, 2024

Victor is exclusively an SVG generator.
You can convert SVG to any other format using other tools, such as ImageMagick or my favorite - rsvg-convert.

$ rsvg-convert in.svg > out.png
# or with specific output width of 1000 pixels
$ rsvg-convert -w 1000 in.svg > out.png

@DannyBen DannyBen closed this as completed Feb 6, 2024
@elalemanyo
Copy link
Author

elalemanyo commented Feb 6, 2024

@DannyBen Thanks for your help. I end up using ImageProcessing::MiniMagick. I add a method to my svg service:

  def to_png
    tmp_file_name = SecureRandom.hex(4)
    tmp_svg_path = Rails.root.join("tmp", "#{tmp_file_name}.svg")
    tmp_png_path = Rails.root.join("tmp", "#{tmp_file_name}.png")
    save(tmp_svg_path.to_s)
    ImageProcessing::MiniMagick
      .source(tmp_svg_path.to_s)
      .custom { |magick| magick.alpha("remove") }
      .convert("png")
      .call(destination: tmp_png_path.to_s)
    File.read(tmp_png_path)
  ensure
    File.delete(tmp_svg_path) if tmp_svg_path && File.exist?(tmp_svg_path)
    File.delete(tmp_png_path) if tmp_png_path && File.exist?(tmp_png_path)
  end

@DannyBen
Copy link
Owner

DannyBen commented Feb 6, 2024

Yup. That is also an option. Keep in mind that ImageMagick for me sometimes converted even simple SVGs incorrectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants