php - ImageTragick "fix"破坏了我的脚本,不知道如何修复

标签 php svg imagemagick imagick imagemagick-convert

使用 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);

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

最佳答案

这里有一些事情。

<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 到 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);

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

关于php - ImageTragick "fix"破坏了我的脚本,不知道如何修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37686984/

相关文章:

php - 如何显示选择框中的选项?

PHP-Imagemagick图像显示

PHP正则表达式将嵌套()替换为[]

svg - 将 kml 多边形转换为 svg 路径

android - 如何为加载了 SkiaSharp 的黑白 SVG 着色?

css - SVG 元素的 3d 变换

ubuntu - ImageMagick 在两个不同的系统上转换不同的结果

c# - 我们可以以编程方式比较具有相同分辨率的不同图像吗?

php - 如何使用来自受信任域的凭据绑定(bind)到 PHP 中的 AD 服务器?

php - Htaccess 重写规则/a 到/a/b