Skip to content

SVG Text does not render in 3.8.1 #774

Description

@jkrrv

Hello,

My apologies if I should be submitting this to the ImageMagick issues instead--happy to do so; let me know.

A recent update broke a framework I maintain that, among other things, uses Imagick to convert SVGs to PNGs.

Release Date: 2026-01-18
ImageMagick 7.1.2-13 Q16 x86_64 2fae24192:20260118 https://imagemagick.org

I have the following test fragment.

function svgToPng($svgContent, ?string $backgroundColor = null): string
{
    $svg = new DOMDocument();
    $svg->loadXML($svgContent);

    // won't work without xmlns.  This makes sure it's present.
    $svg->documentElement->setAttribute('xmlns', 'http://www.w3.org/2000/svg');

    // Test if Imagick is available
    if (!extension_loaded('imagick')) {
        throw new Exception('Imagick extension is not available');
    }

    if (count(\Imagick::queryFormats('SVG')) < 1) {
        throw new Exception('Imagick on this server does not support SVG');
    }

    $im = new \Imagick();
    $im->setResolution(300, 300);

    if (!empty($backgroundColor)) {
        try {
            $pxColor = new \ImagickPixel($backgroundColor);
            $im->setBackgroundColor($pxColor);
        } catch (\ImagickPixelException) {
            $im->setBackgroundColor(new \ImagickPixel('transparent'));
        }
    } else {
        $im->setBackgroundColor(new \ImagickPixel('transparent'));
    }

    $im->readImageBlob($svg->saveXML());

    $im->setImageAlphaChannel(\Imagick::ALPHACHANNEL_ACTIVATE);

    $im = $im->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN);

    if (!$im->setImageFormat('png')) {
        throw new Exception('Failed to set image format as png');
    }

    return $im->getImageBlob();
}



header("Content-Type: image/png");
echo svgToPng("<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'>
  <text x='10' y='40' font-size='30' fill='black'>Hello, World!</text>
  </svg>
  ");

In this particular version, this results in:

Image

Expected output: (and actual output on older versions)

Image

Please let me know what I can/should do to either fix this or diagnose it further. My users generally don't have control over extensions installed on their servers, so this is a significant challenge.

Thanks!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions