Search code examples
phpsvgimagemagickimagickimagemagick-convert

ImageTragick "fix" broke my script and not sure how to fix


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'];

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;

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.


Solution

  • A few things here.

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

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

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

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

    So what's a possible solution?

    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.