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 変換/回転コマンドに問題があり、操作された要素を台無しにしていることに気付きました。結果のSVGをラスタライズされた画像としてレンダリングするには、あまりにも「inkscape」に切り替えてしまいました。そして、すべて順調です。imagemagick がもたらす違いを相殺するために、私はまだ潜在的な定式的な解決策を掘り下げています。成功した場合は、この質問を再度更新します。