I have a site with an image gallery (all svgs) and would like the user to be able to download them as pngs (in the future they should have a choice to save as an svg, png, jpg, or pdf, but let's handle one thing at a time).
I tried the answer found here. The file downloads as a png, but when opened I get an error that the file is damaged/corrupted.
index.html:
<a href="/path/to/file/download.php?http://example.com/uploads/icon-100.svg">
<button> Download PNG </button>
</a>
download.php:
<?php
function replace_extension($filename, $new_extension) {
$info = pathinfo($filename);
return $info['filename'] . '.' . $new_extension;
}
$url = $_SERVER['QUERY_STRING'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.replace_extension( basename($url), 'png').'"');
readfile($url);
?>
The other answers I've found only address changing the extension or renaming the file, not changing the format/type. See here.
How do I change a file format/type in php?
Thanks!