【问题标题】:Convert JPG/PNG to SVG format using PHP使用 PHP 将 JPG/PNG 转换为 SVG 格式
【发布时间】:2013-12-29 07:54:03
【问题描述】:

如何使用 PHP 将 JPG/PNG 转换为 SVG?

我知道它不会被矢量化,但我需要 SVG 格式的。

我不想使用 PHP 以外的任何其他软件。

类似这样的:

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

?>

【问题讨论】:

  • coreldraw 的CorelTrace 可以做到这一点,非常棒。
  • 你能解决这个问题吗?

标签: php svg png jpeg


【解决方案1】:

您可以通过 php 将 PNG/JPEG 嵌入到 SVG。看那里。

<?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; ?>">

如果类型 == svg

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

【讨论】:

  • 这很适合即时生成它们,但是有没有办法使用 svg 扩展名将其保存到服务器?
  • 这段代码不能把图片转换成svg,你能告诉我怎么做吗?
【解决方案2】:

据我所知,您实现这一目标的唯一机会是使用 imagick 库,但可用格式取决于设置,因此如果您在共享服务器上,则无法这样做。

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

这里: Convert SVG image to PNG with PHP

您可以找到有关如何将 SVG 图像转换为 png 的示例,您需要做的是考虑到您的 imagick 库的 oossibility 来创建和操作 svg 文件 调整链接中的脚本...

【讨论】:

  • 这个问题的答案如何,为什么被接受? OP 要求相反的方向 PNG -> SVG。
  • 关于其他方向的问题
猜你喜欢
  • 2012-04-09
  • 1970-01-01
  • 2010-10-19
  • 2015-01-31
  • 2011-09-30
  • 1970-01-01
  • 2011-03-01
  • 2013-03-07
  • 2011-06-16
相关资源
最近更新 更多