php 图片转svg
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);
imagejpeg($i, __DIR__ . '/temp.jpg');
$i = imagecreatefromjpeg(__DIR__ . '/temp.jpg');
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);
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 {
$path[] = $x . " " . $y;
echo '◆';
}
} else {
echo '◇';
}
}
echo '<br/>';
}
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);
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;
while ($c) {
$temp = $spot2;
$spot2 = explode(' ', $path[$c]);
$self_0 = $spot2[0] - $temp[0];
$self_1 = $spot2[1] - $temp[1];
if ($self) {
if ($self[0] != $self_0 || $self[1] != $self_1) {
} else {
array_pop($line);
}
}
$self[0] = $self_0;
$self[1] = $self_1;
$line[] = $path[$c];
$self = $path[$c];
unset($path[$c]);
$c = 0;
!$c and $c = array_search(($spot2[0] - 1) . ' ' . ($spot2[1] - 1), $path);
!$c and $c = array_search(($spot2[0] - 1) . ' ' . $spot2[1], $path);
!$c and $c = array_search(($spot2[0] - 1) . ' ' . ($spot2[1] + 1), $path);
!$c and $c = array_search($spot2[0] . ' ' . ($spot2[1] + 1), $path);
!$c and $c = array_search($spot2[0] . ' ' . ($spot2[1] - 1), $path);
!$c and $c = array_search(($spot2[0] + 1) . ' ' . ($spot2[1] + 1), $path);
!$c and $c = array_search(($spot2[0] + 1) . ' ' . $spot2[1], $path);
!$c and $c = array_search(($spot2[0] + 1) . ' ' . ($spot2[1] - 1), $path);
}
$is_bg = false;
if (count($line) > 10) {
$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"/>';
}
reset($spot);
$spot = current($path);
};
echo '</g> </svg>';
header('Content-Type: image/svg+xml');
exit;
}
|