ImageTragick“修复”打破我的脚本,不知道如何解决 [英] ImageTragick "fix" broke my script and not sure how to fix

查看:135
本文介绍了ImageTragick“修复”打破我的脚本,不知道如何解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using Imagick, I'm placing custom jpgs into an svg and then converting them to a jpg. Because of the ImageTragick, my placed jpegs never show up and my font doesn't convert. Because of the vulnerability my host upgraded their policy.xml and now it's broken.

<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>

我不知道从哪里开始修复它。我已经阅读过关于确认图像类型的内容,但对代码的位置感到困惑。

I have no idea where to begin fixing it. I've read about confirming the image types but am confused about where that code goes.

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

Basically, I refer to the image I want using...

$imageURL = $_POST['imageURL'];

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

Then I build an svg string using...

$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运行()

Then prep it for a run through 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);

这个工作好几个月了。现在,转换发生但不包括字体,放置的图像仅显示为白色背景。在我们有机会实施新解决方案之前,有关如何使其正常运行的任何建议吗?谢谢。

And this worked fine for many months. Now, the conversion happens but the fonts aren't included and the placed image appears as just a white background. Any suggestions on how to get this up-and-running until we get a chance to implement a new solution? Thanks.

推荐答案

这里有一些事情。

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

<$ c $的政策限制c> URL 和/或 HTTPS 必须阻止此行为。

The policy restriction on URL and/or HTTPS will must likely block this behavior.

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

MVG 限制会阻止读取中间XML到MVG文件。

The MVG restriction would block the intermediate XML-to-MVG file from being read.

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

If you can't read remote resources, or intermediate vector graphics files, then plan on collecting the image parts & building the image directly. Here's an example.

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

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

Of course the above solution can be simplified / reduced, but should get you started.

这篇关于ImageTragick“修复”打破我的脚本,不知道如何解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆