es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

PHP Imagick no aplica la fuente al convertir SVG a PNG.

Estoy tratando de guardar un archivo SVG que contiene texto con una fuente no instalada en el sistema como un archivo PNG. He convertido la fuente a data64 ya que leí que eso debería evitar que el host web necesite tener la fuente instalada. Aunque el SVG se muestra normalmente en un navegador web, cuando abro el PNG creado, el texto se muestra sin la fuente aplicada. ¿Qué estoy haciendo mal?

Datos SVG:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="3200" height="3200" viewbox="0 0 400 400" xml:space="preserve">
<desc>Created with Fabric.js 3.2.0</desc>
<defs>
    <style type="text/css">
        @font-face {
            font-family: 'ABeeZee';
            src: local('ABeeZee Regular'), local('ABeeZee-Regular'), url(data:font/woff;base64,[[Colocador de base64 de fuente debido al límite de caracteres]]) format('woff');
        }
    </style>
</defs>
<g transform="matrix(1 0 0 1 200 23.5)" style="font-family:'ABeeZee' !important;">
        <text xml:space="preserve" font-family="ABeeZee" font-size="32.410852713178286" font-style="normal" font-weight="normal" style="font-family:'ABeeZee' !important; stroke: none; stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1; white-space: pre;"><tspan x="-102.09418605" y="10.18154527">Kent Patrick</tspan></text>
</g>
</svg>

Código PHP:

$im = new \Imagick();
$im->setBackgroundColor(new \ImagickPixel('transparent'));
$im->readImageBlob($svg);
$im->setImageFormat("png24");
$im->resizeImage(1000, 1000, \Imagick::FILTER_LANCZOS, 1);  // Opcional, si necesitas cambiar el tamaño

$im->writeImage($save_file);

header('Content-type: image/png');
echo $im;

$im->clear();
$im->destroy();
Tags:  , , , ,

Answer

  1. Avatar for davy.ai

    El problema puede estar en la forma en que estás convirtiendo la fuente a data64. Verifica que la cadena codificada en base64 sea correcta y esté correctamente formateada. Además, asegúrate de que la fuente esté incluida correctamente en el archivo SVG y que esté referenciada correctamente en el elemento de texto. También, intenta establecer el atributo “font-family” en el propio elemento SVG para asegurarte de que la fuente se cargue antes de que se dibuje cualquier texto. Si el problema persiste, prueba a utilizar un método diferente para convertir la fuente, como utilizar un servicio de fuentes como Google Fonts o Adobe Fonts, o incrustar la fuente directamente en el archivo PNG.

Comments are closed.