'Convert JPG/PNG to SVG format using PHP
How do I convert JPG/PNG to SVG using PHP?
I know that it will not be vectorised, but I need it in a SVG-format.
I dont want to use any other software than PHP.
Something like this:
<?php
$image_to_cenvert = 'image.jpg';
$content = file_get_contents($image_to_cenvert);
$svg_file = fopen('image.svg','w+');
fputs($svg_file,$content);
fclose($svg_file);
?>
Solution 1:[1]
You can embed PNG/JPEG to SVG, by php. Look there.
<?php
$file = __DIR__ . $path2image;
$path = pathinfo($file);
$ext = mb_strtolower($path['extension']);
if (in_array($ext, array('jpeg', 'jpg', 'gif', 'png', 'webp'))) {
$size = getimagesize($file);
$img = 'data:' . $size['mime'] . ';base64,' . base64_encode(file_get_contents($file));
}
?>
<img src="<?php echo $img; ?>">
If type == svg
$img = 'data:image/svg+xml;base64,' . base64_encode(file_get_contents($file));
Solution 2:[2]
For this conversion you need to install ImageMagick and potrace. potrace can convert pbm/pgm/ppm/bmp to svg. So at first we need to convert png/jpg to pbm/pgm/ppm/bmp using imagemagick. Than we will convert it to svg using potrace via command line. raster to svg
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Mouri |
