简体   繁体   中英

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);

?>

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));

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

As i know your only chance to achieve that is to use imagick library but the format available depend on the settings so if you're on a shared server could not be possible do it.

http://php.net/manual/en/book.imagick.php

and here : Convert SVG image to PNG with PHP

you can find an example on how to convert a SVG image to png, what you will need to do is given the oossibility of your imagick library to create and manipulate svg file adapt the script in the link over...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM