【问题标题】:ImageTragick "fix" broke my script and not sure how to fixImageTragick“修复”破坏了我的脚本,不知道如何修复
【发布时间】:2016-10-07 19:20:26
【问题描述】:

使用 Imagick,我将自定义 jpg 放入 svg,然后将它们转换为 jpg。由于 ImageTragick,我放置的 jpeg 永远不会出现,我的字体也不会转换。由于这个漏洞,我的主机升级了他们的 policy.xml,现在它已经坏了。

<policymap>
<!-- <policy domain="system" name="precision" value="6"/> -->
<!-- <policy domain="resource" name="temporary-path" value="/tmp"/> -->
<policy domain="resource" name="memory" value="32MiB"/>
<policy domain="resource" name="map" value="32Mib"/>
<!-- <policy domain="resource" name="area" value="1GB"/> -->
<!-- <policy domain="resource" name="disk" value="16EB"/> -->
<!-- <policy domain="resource" name="file" value="768"/> -->
<policy domain="resource" name="thread" value="1"/> -->
<!-- <policy domain="resource" name="throttle" value="0"/> -->
<policy domain="resource" name="time" value="30"/>
<policy domain="coder" rights="none" pattern="EPHEMERAL" />
<policy domain="coder" rights="none" pattern="URL" />
<policy domain="coder" rights="none" pattern="HTTPS" />
<policy domain="coder" rights="none" pattern="MVG" />
<policy domain="coder" rights="none" pattern="MSL" />
</policymap>

我不知道从哪里开始修复它。我已阅读有关确认图像类型的信息,但对代码的去向感到困惑。

基本上,我指的是我要使用的图像...

$imageURL = $_POST['imageURL'];

然后我使用...构建一个 svg 字符串

$svgString = <<<EOD
<svg class="svgImage" viewBox="0 0 1200 630" preserveAspectRatio="xMidYMin slice">
    <image id="placedImage" xlink:href="$imageURL" x="0" y="0" height="630px" width="1200px" />
    <text class="newText topText" id="topTextWhite" style="font-size:24px; font-family: impact, sans-serif;" text-anchor="middle" x="600" y="100" fill="black">Test top words</text>
    <text class="newText bottomText" id="bottomTextWhite" style="font-size:24px; font-family: impact, sans-serif;" text-anchor="middle" x="600" y="600" fill="black">Test bottom words</text>
</svg>
EOD;

然后准备运行 Imagick()

//NEW FILENAME INCLUDING MICROTIME TO PREVENT DUPLICATIONS
$fileName = "name". microtime(true);
$newFileName = 'gallery/' . $fileName . '.jpg';

//THE FILE WE'LL BE WORKING WITH
$file =  'gallery/' . $fileName . '.svg';

//INITIALIZE STRING FOR PROPER SVG CONVERSION
$current = '<?xml version="1.0" encoding="UTF-8" standalone="no"?>';

//APPEND CONTENT STRING
$current .= $svgString;

//WRITE THE FILE CONTENTS INTO THE FILE
//If file already exists, it will replace it.
file_put_contents($file, $current);

//START CONVERSION PROCESS
$svg = file_get_contents($file);

$image = new Imagick();
$image->setFont("Impact.ttf");
$image->readImageBlob($svg);
$image->setCompression(Imagick::COMPRESSION_JPEG);
$image->setCompressionQuality(60);
$image->setImageFormat("jpg");
$image->writeImage($newFileName);
$image->clear();
$image->destroy();

//DESTROY SVG FILE
unlink($file);

这几个月都很好。现在,转换发生了,但字体不包括在内,放置的图像仅显示为白色背景。在我们有机会实施新解决方案之前,关于如何启动并运行它有什么建议吗?谢谢。

【问题讨论】:

    标签: php svg imagemagick imagick imagemagick-convert


    【解决方案1】:

    这里有一些东西。

    <image id="placedImage" xlink:href="$imageURL" x="0" y="0" height="630px" width="1200px" />
                                        ^~~~~~~~^
    

    URL 和/或HTTPS 的政策限制可能会阻止这种行为。

    $image->readImageBlob($svg); // <-- May throw 'not authorized' exception
    

    MVG 限制将阻止读取中间 XML-to-MVG 文件。

    那么可能的解决方案是什么?

    如果您无法读取远程资源或中间矢量图形文件,请计划收集图像部分并直接构建图像。这是一个例子。

    // Create a blank Canvas (YMMV)
    $image = new Imagick();
    $image->newPseudoImage(1200, 630, 'xc:');
    // Read background image
    $background = new Imagick($imageURL); // Can no longer be remote URL/HTTP(S)
    // Resize to canvas
    $background->resizeImage(1200, 630, Imagick::FILTER_LANCZOS, 1.0, false);
    // Copy to canvas (again, YMMY)
    $image->compositeImage($background, Imagick::COMPOSITE_ATOP, 0, 0, Imagick::CHANNEL_ALL);
    // Create a drawing context
    $ctx = new ImagickDraw();
    // Read font as before
    $ctx->setFont("Impact.ttf");
    $ctx->setFontSize(24);
    // Calculate what the font will be ...
    $font_metrics = $image->queryFontMetrics($ctx, "Test top words", false);
    // ... and draw it at position.
    $ctx->annotation(600 - $font_metrics['textWidth'] / 2, 100, "Test top words");
    // repeat as needed
    $font_metrics = $image->queryFontMetrics($ctx, "Test bottom words", false);
    $ctx->annotation(600 - $font_metrics['textWidth'] / 2, 600, "Test bottom words");
    // Draw context
    $image->drawImage($ctx);
    // Write to JPG
    $image->setCompression(Imagick::COMPRESSION_JPEG);
    $image->setCompressionQuality(60);
    $image->writeImage($newFileName);
    

    当然,上述解决方案可以简化/减少,但应该可以帮助您入门。

    【讨论】:

    • 进展!!!因此,要添加具有不同属性的多行文本,例如:“第 1 行文本”是黑色和 24 像素,“第 2 行文本”是白色和 36 像素,我需要创建多个上下文吗?像 $ctx 和 $ctx2 然后做一个 $image->drawImage($ctx);和一个 $image->drawImage($ctx2);?他们会这样叠加吗?
    • 差不多。很难在没有看到实际来源的情况下回答,但您可以使用 ImagickDraw::push 和 ImagickDraw::pop 方法管理堆栈。但这听起来像你在正确的轨道上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-11
    • 2017-06-15
    • 2021-06-19
    • 2014-04-15
    • 2020-08-08
    相关资源
    最近更新 更多