【发布时间】:2012-05-10 03:07:43
【问题描述】:
我将 SVG 文件的字符串表示形式发送到服务器,并使用 Imagick 以下列方式将其转换为 jpeg:
$image = stripslashes($_POST['json']);
$filename = $_POST['filename'];
$unique = time();
$im = new Imagick();
$im->readImageBlob($image);
$im->setImageFormat("jpeg");
$im->writeImage('../photos/' . $type . '/humourised_' . $unique . $filename);
$im->clear();
$im->destroy();
但是,我希望在光栅化 SVG 之前调整其大小,以便生成的图像大于 SVG 文件中指定的尺寸。
我将我的代码修改为以下内容:
$image = stripslashes($_POST['json']);
$filename = $_POST['filename'];
$unique = time();
$im = new Imagick();
$im->readImageBlob($image);
$res = $im->getImageResolution();
$x_ratio = $res['x'] / $im->getImageWidth();
$y_ratio = $res['y'] / $im->getImageHeight();
$im->removeImage();
$im->setResolution($width_in_pixels * $x_ratio, $height_in_pixels * $y_ratio);
$im->readImageBlob($image);
$im->setImageFormat("jpeg");
$im->writeImage('../photos/' . $type . '/humourised_' . $unique . $filename);
$im->clear();
$im->destroy();
这段代码应该计算出分辨率并相应地调整 SVG 的大小。如果 SVG 画布及其元素具有基于“百分比”的宽度,则它可以完美运行,但它似乎不适用于“px”中定义的元素。不幸的是,这是一项要求。
将发送到服务器的典型 SVG 字符串如下所示:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/SVG/DTD/svg10.dtd">
<svg id="tempsvg" style="overflow: hidden; position: relative;" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="333" version="1.1" height="444">
<image transform="matrix(1,0,0,1,0,0)" preserveAspectRatio="none" x="0" y="0" width="333" height="444" xlink:href="http://www.songbanc.com/assets/embed/photos/full/133578615720079914224f9e7aad9ac871.jpg"></image>
<image transform="matrix(1,0,0,1,0,0)" preserveAspectRatio="none" x="85.5" y="114" width="50" height="38" xlink:href="http://www.songbanc.com/assets/embed/humourise/elements/thumb/thumb_lips4.png"></image>
<path transform="matrix(1,0,0,1,0,0)" fill="none" stroke="#000" d="M110.5,133L140.5,133" stroke-dasharray="- " opacity="0.5"></path>
<circle transform="matrix(1,0,0,1,0,0)" cx="140.5" cy="133" r="5" fill="#000" stroke="#000"></circle>
<path transform="matrix(1,0,0,1,0,0)" fill="none" stroke="#000" d="M110.5,133L110.5,155.8" stroke-dasharray="- " opacity="0.5"></path>
<circle transform="matrix(1,0,0,1,0,0)" cx="110.5" cy="155.8" r="5" fill="#000" stroke="#000"></circle>
<circle transform="matrix(1,0,0,1,0,0)" cx="110.5" cy="133" r="5" fill="#000" stroke="#000"></circle>
</svg>
如您所见,构成此 SVG 的元素具有像素定义宽度和高度(遗憾的是,此应用程序不能选择使用百分比)
有没有办法解决这个问题?或任何其他将 SVG 转换为 png 并以给定大小呈现而不损失质量的方法。
谢谢。
编辑:虽然我实际上从未设法找到完美的解决方案。相反,我结束了将 SVG 数据作为 json 发送,在服务器端循环并将像素缩放到预期的高度。
然后,经过多次尝试和错误,我意识到 imagemagick 在标准 SVG 变换/旋转命令方面存在问题,使任何被操纵的元素都失控了。我最终切换了“inkscape”以将生成的 SVG 渲染为光栅化图像。一切都很好。我仍在研究一种潜在的公式化解决方案,以抵消 imagemagick 造成的差异。如果我有任何成功,我会再次更新这个问题。
【问题讨论】:
-
来自your other question 我怀疑您现在已经切换到 Inkscape(它可以使用命令行上的选项缩放图像)。如果是这种情况,您会在问题下方添加评论吗?
:). -
确实...我会相应地编辑我的问题。 :-)
-
希望了解有关您的 Inkscape 解决方案的更多详细信息!
-
对于仍有问题的任何人this 可能有用。我手动替换了
-
使用库contao/imagine-svg,您可以在将 SVG 图像发送到 ImageMagick 之前调整其大小。
标签: php imagemagick imagick