Reputation: 377
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
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
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