php-svg icon indicating copy to clipboard operation
php-svg copied to clipboard

Image is not included in final file

Open Luke-SF opened this issue 7 months ago • 2 comments

I'm having issues creating a png including an image: and when I run this minimal example, it is not saving the image or the text?

<?php
require 'vendor/autoload.php';

use SVG\SVG;
use SVG\Nodes\Structures\SVGDocumentFragment;
use SVG\Nodes\Embedded\SVGImage;
use SVG\Nodes\Texts\SVGText;
use SVG\Rasterization\Renderers\ImageRenderer;
use SVG\Rasterization\SVGRasterizer;

// Create a new SVG document
$svg = new SVG(100,100);
$doc = $svg->getDocument();

// Define the image parameters
$png = '/yourpathtosome.png'; // Ensure this path is correct
$x = 10;
$y = 10;
$width = 50;
$height = 50;

// Create and add the image to the document
$image = new SVGImage($png, $x, $y, $width, $height);
$doc->addChild($image);

// Optionally, add some text to verify the document structure
$text = new SVGText('Hello, SVG!', 20, 20);
$doc->addChild($text);

// Output the SVG content
#header('Content-Type: image/svg+xml');
echo $svg;
$rasterImage = $svg->toRasterImage(100, 100);
        imagepng($rasterImage, '/tmp/out.png');

This is on PHP8.4, "meyfa/php-svg": "^0.9.1", and gd is installed. It's outputting an empty png. The svg output looks okay.

Luke-SF avatar Mar 03 '25 22:03 Luke-SF