zerociudo
zerociudo

Reputation: 377

Create get SVG file contents from SVG raw data and download it

I have SVG raw data such like this

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100px" height="100px" viewBox="0 0 100 100"><defs><rect id="r0" width="3" height="3" fill="#000000"/></defs><rect x="0" y="0" width="100" height="100" fill="#fefefe"/><use x="12" y="12" xlink:href="#r0"/><use x="15" y="12" xlink:href="#r0"/><use x="18" y="12" xlink:href="#r0"/><use x="21" y="12" xlink:href="#r0"/><use x="24" y="12" xlink:href="#r0"/><use ..............much more lines </svg>

How can I convert this string into svg file content and download it without saving it in storage or anywhere? Is there a way for this?

I tried

$data = base64_decode($file);
$im = imagecreatefromstring($data);

but this throws imagecreatefromstring(): Data is not in a recognized format

Upvotes: 0

Views: 1408

Answers (2)

S&#233;rgio Reis
S&#233;rgio Reis

Reputation: 2523

Assuming your $file has the entire contents of the svg

return response($file)->withHeaders(['Content-Type' => "image/svg+xml"]);

This is something on the docs 1 , 2

Upvotes: 3

Nico Haase
Nico Haase

Reputation: 12131

Try outputting the image the following way:

header('Content-type: image/svg+xml');
header('Content-Disposition: attachment; filename=image.svg');
echo $data;

Upvotes: 3

Related Questions