简体   繁体   English

使用 PHP 将 JPG/PNG 转换为 SVG 格式

[英]Convert JPG/PNG to SVG format using PHP

How do I convert JPG/PNG to SVG using PHP?如何使用 PHP 将 JPG/PNG 转换为 SVG?

I know that it will not be vectorised, but I need it in a SVG-format.我知道它不会被矢量化,但我需要它的 SVG 格式。

I dont want to use any other software than PHP.除了 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.您可以通过 php 将 PNG/JPEG 嵌入到 SVG 中。 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如果类型 == svg

$img = 'data:image/svg+xml;base64,' . base64_encode(file_get_contents($file));

For this conversion you need to install ImageMagick and potrace.对于此转换,您需要安装 ImageMagick 和 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. potrace 可以将 pbm/pgm/ppm/bmp 转换为 svg。所以首先我们需要使用 imagemagick 将 png/jpg 转换为 pbm/pgm/ppm/bmp。然后我们将通过命令行使用 potrace 将其转换为 svg。 raster to svg光栅到 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.据我所知,您实现这一目标的唯一机会是使用 imagick 库,但可用的格式取决于设置,因此如果您在共享服务器上,则不可能这样做。

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

and here : Convert SVG image to PNG with PHP在这里: 使用 PHP 将 SVG 图像转换为 PNG

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...您可以找到一个关于如何将 SVG 图像转换为 png 的示例,您需要做的是考虑到您的 imagick 库来创建和操作 svg 文件的可能性,使链接中的脚本适应...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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