php 图片转svg

php 图片转svg

  • 临时写的未做优化 还是可以将就着用
if (!function_exists('outputSvg')) {
    function outputSvg($url)
    {
        $info = getimagesize($url);
        if (!$info) return '<h1>这不是图片</h1>';
        $readImage = [
            IMAGETYPE_GIF  => 'imagecreatefromgif',
            IMAGETYPE_JPEG => 'imagecreatefromjpeg',
            IMAGETYPE_PNG  => 'imagecreatefrompng',
            IMAGETYPE_WBMP => 'imagecreatefromwbmp',
            IMAGETYPE_XBM  => 'imagecreatefromxbm',
            IMAGETYPE_BMP  => 'imagecreatefrombmp',
        ];

        if (!$readImage[$info[2]]) return '<h1>这不是图片</h1>';
        $i = $readImage[$info[2]]($url);
        $temp_file = __DIR__ . '/temp.jpg';
        imagejpeg($i, $temp_file);
        $i = imagecreatefromjpeg($temp_file);
        if (!$i) return '<h1>图片不存在</h1>';
        $x_l = $info[0];
        $y_l = $info[1];
        $path = [];
        $channels = imagecolorstotal($i);
        ob_start();
        echo '<style>body{line-height: 8px;}</style>';
        //过滤白色
        $mottled = 10000000;
        for ($x = 0; $x < $x_l; $x++) {
            for ($y = 0; $y < $y_l; $y++) {
                $rgb = imagecolorat($i, $x, $y);
                if ($rgb < $mottled) {
                    $c_1 = imagecolorat($i, $x - 1, $y);
                    $c_2 = imagecolorat($i, $x + 1, $y);
                    $c_3 = imagecolorat($i, $x, $y - 1);
                    $c_4 = imagecolorat($i, $x, $y + 1);
                    //echo '◆';
                    if ($c_1 < $mottled && $c_2 < $mottled && $c_3 < $mottled && $c_4 < $mottled) {
                        echo '◇';
                    } elseif ($c_1 > $mottled && $c_2 > $mottled && $c_3 > $mottled && $c_4 > $mottled) {
                        echo '◇';
                    } else {
                        echo '◆';
                        $path[] =  $x . " " . $y;
                    }
                } else {
                    echo '◇';
                }
            }
            echo '<br/>';
        }
        //exit;
        ob_clean();
        echo "<?xml version=\"1.0\" standalone=\"no\"?> <!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 20010904//EN\" \"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd\"> <svg version=\"1.0\" xmlns=\"http://www.w3.org/2000/svg\" width=\"80%\" height=\"80%\" viewBox=\"0 0 {$x_l} {$y_l}\" preserveAspectRatio=\"xMidYMid meet\"> <g fill=\"#000\" stroke=\"none\">";
        $spot = current($path);
        $block = 1;
        $lines = [];
        while ($spot) {
            unset($path[array_search($spot, $path)]);
            $spot1 = explode(' ', $spot);
            echo "<path d=\"M{$spot1[0]} {$spot1[1]}";
            $c = 0;
            !$c and $c = array_search(($spot1[0] - 1) . ' ' . ($spot1[1] - 1), $path);
            !$c and $c = array_search(($spot1[0] - 1) . ' ' . $spot1[1], $path);
            !$c and $c = array_search(($spot1[0] - 1) . ' ' . ($spot1[1] + 1), $path);
            !$c and $c = array_search($spot1[0] . ' ' . ($spot1[1] - 1), $path);
            !$c and $c = array_search($spot1[0] . ' ' . ($spot1[1] + 1), $path);
            !$c and $c = array_search(($spot1[0] + 1) . ' ' . ($spot1[1] - 1), $path);
            !$c and $c = array_search(($spot1[0] + 1) . ' ' . $spot1[1], $path);
            !$c and $c = array_search(($spot1[0] + 1) . ' ' . ($spot1[1] + 1), $path);
            $line = [];
            $self = [];
            $spot2 = $spot1;
            $lien_count = 0;
            while ($c) {
                $temp = $spot2;
                $spot2 = explode(' ', $path[$c]);
                $self_temp = [$spot2[0] - $temp[0], $spot2[1] - $temp[1]];
                //if ($block == 2 && $lien_count > 10) {
                //    ob_clean();
                //    dd($self_temp);
                //}
                if ($self && $self == $self_temp) {
                    array_pop($line);
                }

                $self = $self_temp;
                $line[] = $path[$c];

                $_c = $c;
                $c = 0;
                $d = [];
                if ($temp = array_search($spot2[0] . ' ' . ($spot2[1] + 1), $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }
                if ($temp = array_search(($spot2[0] + 1) . ' ' . $spot2[1], $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }
                if ($temp = array_search($spot2[0] . ' ' . ($spot2[1] - 1), $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }
                if ($temp = array_search(($spot2[0] - 1) . ' ' . $spot2[1], $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }
                if ($temp = array_search(($spot2[0] - 1) . ' ' . ($spot2[1] - 1), $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }
                if ($temp = array_search(($spot2[0] - 1) . ' ' . ($spot2[1] + 1), $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }
                if ($temp = array_search(($spot2[0] + 1) . ' ' . ($spot2[1] + 1), $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }

                if ($temp = array_search(($spot2[0] + 1) . ' ' . ($spot2[1] - 1), $path)) {
                    $c = $c ? $c : $temp;
                    $d[] = $temp;
                }
                if ($lien_count == 1 || count($d) <= 1) {
                    unset($path[$_c]);
                } else {
                    //break;
                    unset($path[$_c]);
                    //ob_clean();
                    //dd($path);
                }
                //if ($block == 2 && $lien_count > 80) {
                //    break;
                //}
                $lien_count++;
            }
            //读取当前点的颜色
            //是否为白色
            $is_bg = false;
            $lines = array_merge($lines, $line);

            if (count($line) > 1) {
                $bg = explode(' ', current($line));
                if (imagecolorat($i, $bg[0] + 1, $bg[1]) > $mottled) {
                    $is_bg = true;
                }
            }
            foreach ($line as $item) {
                echo " L$item";
            }
            if ($is_bg) {
                echo "Z\" fill=\"#fff\" />";
            } else {
                echo 'Z"/>';
            }
            //LZQ:待优化
            //if ($block == 10) {
            //    break;
            //}
            reset($spot);
            $spot = current($path);
            $block++;
        };
        echo '</g> </svg>';
        header('Content-Type: image/svg+xml');
        exit;

        ob_clean();
        echo '<style>body{line-height: 8px;}</style>';
        $x_l = 50;
        $y_l = 100;
        for ($x = 0; $x < $x_l; $x++) {
            for ($y = 0; $y < $y_l; $y++) {
                $temp = $x . " " . $y;

                if (in_array($temp, $lines)) {
                    echo '<span style="color:red">◆</span>';
                } else if (in_array($temp, $path)) {
                    echo '◆';
                } else {
                    echo '◇';
                }
            }
            echo '<br/>';
        }

        exit;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值