PHP code example of resvg-php / resvg
1. Go to this page and download the library: Download resvg-php/resvg library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
resvg-php / resvg example snippets
$renderer = new Resvg\Renderer();
$png = $renderer->render(file_get_contents('chart.svg'));
file_put_contents('chart.png', $png);
$renderer = new Resvg\Renderer();
$png = $renderer->render('<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16">
<circle cx="8" cy="8" r="7" fill="#2b6cb0"/>
</svg>');
file_put_contents('icon.png', $png);
$renderer = new Resvg\Renderer(); // once, e.g. in a service container
foreach ($icons as $icon) {
$png = $renderer->render($icon); // many
}
$renderer->render($svg, ['width' => 512]); // scale to width
$renderer->render($svg, ['height' => 512]); // scale to height
$renderer->render($svg, ['width' => 512, 'height' => 512]); // exact pixel size
$renderer->render($svg, ['zoom' => 2.0]); // scale by factor
$renderer->render($svg, ['background' => '#ffffff']);
$renderer = new Resvg\Renderer([
'fontFamily' => 'Inter',
'fontSize' => 16,
'languages' => ['en', 'bn'],
'fontFiles' => ['/var/www/fonts/Inter-Regular.ttf'],
]);
['width' => $w, 'height' => $h] = $renderer->measure($svg, ['width' => 512]);
$tree = $renderer->parse(file_get_contents('badge.svg'));
$small = $tree->render(['width' => 64]);
$large = $tree->render(['width' => 512, 'background' => '#ffffff']);
$tree->renderNode('chart-legend');
$tree->renderNode('chart-legend', ['exportArea' => 'page']); // on a full-page canvas
$normalized = $tree->toSvg(); // text converted to paths
$withText = $tree->toSvg(['preserveText' => true]); // keep <text> elements
$pretty = $tree->toSvg(['indent' => 'tabs']);
$stream = fopen('/var/www/out.png', 'wb');
$renderer->render($bigSvg, ['output' => $stream]); // true
fclose($stream);
$renderer->render($bigSvg, ['output' => '/var/www/out.png']); // path also accepted
$renderer = new Resvg\Renderer([
'resourcesDir' => '/var/www/svg-assets',
'confineResources' => true, // absolute hrefs and `..` escapes are refused
]);
Resvg\Renderer::version(); // "0.3.0+resvg.0.48.1"
try {
$png = $renderer->render($userSuppliedSvg);
} catch (Resvg\Exception $e) {
// malformed SVG, invalid size, unparseable background, unencodable output
}
sh
pie install resvg-php/resvg
sh
dnf install ./php-pecl-resvg-0.3.0-1.el8.x86_64.rpm
apt install ./php8.3-resvg_0.3.0-1~noble_amd64.deb