php中图片转svg格式及svg转图片的实现方式

现在svg的普及随着html5的普及而推广开来,svg矢量相比较位图更加有优势,那么如何将传统的位图通过php转换成svg呢?还有就是svg图片又如何转换成传统的图片呢,今天我来给大家一一解答
一、图片转svg
php中没有直接的函数可以将图片转成svg,但是我们可以通过其他的办法,GD库是一种选择,原理就是通过gd库读取图片的每个像素点,然后生成一个rect组合起来,具体核心代码如下
<?php
$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];
// open image into a true color image
$orimage = imagecreatefrompng($filename);
$im = imagecreatetruecolor($width, $height);
imagecopyresampled($im, $orimage, 0, 0, 0, 0, $width, $height, $width, $height);
$svg = "";
// header
output_head();
// body
$colors = array();
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
// get pixel at (x,y)
$rgb = imagecolorat($im, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
// skip black pixels
if ($rgb == 0) continue;
...点击查看剩余70%
网友评论0