php - 使用 ImageMagick 从 SVG 转换为 PNG 时裁剪图像

标签 php svg imagemagick imagemagick-convert

我有一个类似于

的 SVG 文件

http://www.fileformat.info/info/unicode/char/00c1/latin_capital_letter_a_with_acute.svg

当我使用 ImageMagick 将其转换为 PNG 时:

convert latin_capital_letter_a_with_acute.svg tmp.png

那么只有字符的顶部是可见的。

SVG 文件没有指定任何高度和宽度。如果我在 XML 文件中手动指定 width="25cm"height="25cm"则角落中的完整字符作为小图像,大背景填充图像的其余部分。我在 Stackexchange 和各种论坛中进行了搜索,但找不到解决方案。

我也使用 PHP 尝试了以下操作。但它仍在生成裁剪后的图像。

我认为这是检测 SVG 图像的正确大小的问题。但是想不出一个简单的方法来做到这一点。

最佳答案

我使用此命令成功地从您的链接转换了 SVG:

convert    \
 http://www.fileformat.info/info/unicode/char/00c1/latin_capital_letter_a_with_acute.svg \
 latin_capital_letter_a_with_acute.png

这是结果,我觉得还不错:

PNG, generated from SVG

我的 ImageMagick 版本是(根据 convert -version):

 Version: ImageMagick 6.9.0-0 Q16 x86_64 2014-12-06 http://www.imagemagick.org
 Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
 Features: DPC Modules
 Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib gvc \
                       jbig jng jp2 jpeg lcms lqr ltdl lzma openexr \
                       pangocairo png ps rsvg tiff webp wmf x xml zlib

However, it is not ImageMagick directly and all by itself that does the heavy-lifting of interpreting the SVG. My ImageMagick uses an external delegate to achieve this. How exactly this works can be made visible by adding the -verbose setting to the command line:

convert -verbose letter_a_with_acute.svg letter_a_with_acute.png

 "/opt/local/bin/inkscape" "/var/tmp/magick-12470O5QRRVap0Ub4" \
     --export-eps="/var/tmp/magick-124705G1EV-reRRrb" --export-dpi="90,90" \
     --export-background="rgb(100%,100%,100%)" --export-background-opacity="1" \
       > "/var/tmp/magick-12470W9feuKm0JHUA" 2>&1

 /var/tmp/magick-12470qEPdaNM3mKYw1 PNG 155x209 155x209+0+0 8-bit sRGB 3.04KB 0.000u 0:00.000
 /var/tmp/magick-124705G1EV-reRRrb PS 155x209 155x209+0+0 16-bit sRGB 3.04KB 0.000u 0:00.000

 letter_a_with_acute.svg=>/var/tmp/magick-124705G1EV-reRRrb PS 155x209 155x209+0+0 16-bit sRGB 3.04KB 0.000u 0:00.000
 letter_a_with_acute.svg=>latin_capital_letter_a_with_acute.png PS 155x209 155x209+0+0 8-bit sRGB 17c 2.93KB 0.000u 0:00.000

 [ghostscript library] -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT \
                       -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 \
                      "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 \
                      "-r72x72" -g155x209  "-sOutputFile=/var/tmp/magick-12470qEPdaNM3mKYw%d" \
                      "-f/var/tmp/magick-12470Xt3b3Qubkxx2" \
                      "-f/var/tmp/magick-12470VRcUz0MbmdiC"

As you can clearly see, convert employs two delegates in two different steps to bring about the conversion:

  1. First, it runs an inkscape command. This command exports the SVG to EPS.

  2. Second, it runs a Ghostscript command. This command converts the EPS from step 1 to the final PNG.

  3. In between the above two steps, it runs some other command, probably identify, in order to find out the dimensions of the EPS that was produced.

In other words, if you are not able to set up your ImageMagick delegates correctly so that they will process SVG files for you, you could always use Inkscape directly on the command line to create the PNG:

inkscape                                    \
  --without-gui                             \
  --export-png=out.png                      \
  --export-dpi="90,90"                      \
  --export-background="rgb(100%,100%,100%)" \
  --export-background-opacity="1"           \
    input.svg

看看结果,out.png,它似乎在字母周围使用了太多的空白:

  identify out.png  
   out.png PNG 744x1052 744x1052+0+0 8-bit sRGB 13.1KB 0.000u 0:00.000

这可以通过 ImageMagick 纠正:

  convert out.png -trim +repage trimmed.png

检查:

  identify trimmed.png 
   trimmed.png PNG 193x260 193x260+0+0 8-bit sRGB 256c 3.53KB 0.000u 0:00.000

关于php - 使用 ImageMagick 从 SVG 转换为 PNG 时裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478030/

相关文章:

php - 在 Imagemagick 和 GD 之间选择缩略图创建

ubuntu - 使用 imagemagick 从命令行添加可见的登录 pdf

php - 使用过滤器 wpseo_title 更改标题标签

php - 我们在处理 session 时必须使用 mysql_real_escape_string() 吗?

javascript - 如何将德国 map 放在 HTML 中的 svg 上? D3js

php - 从PNG图像中提取元数据

php - PHP 中的数组引用混淆

c# - Asp.net 呈现编码错误的字符串,但 PHP 不呈现 (MySQL)

javascript - 在javascript中将svg路径转换为多边形

javascript - 用 d3 以 Angular 放大 svg