user1087110
user1087110

Reputation: 3763

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

?>

Upvotes: 2

Views: 24870

Answers (3)

Mouri
Mouri

Reputation: 130

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

Upvotes: 0

Mevrit
Mevrit

Reputation: 41

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

Upvotes: 4

Aliceiw
Aliceiw

Reputation: 418

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...

Upvotes: -4

Related Questions