Commit 00a952d0 authored by 王源's avatar 王源 🎧

优化画图工具类,添加合并图片的方法

parent 20fc68ee
...@@ -46,11 +46,12 @@ class Drawer ...@@ -46,11 +46,12 @@ class Drawer
*/ */
public function downloadWebImage($url, $path = null) public function downloadWebImage($url, $path = null)
{ {
$url = $this->parseUrl($url);
// excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug // excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug
$filename = urlencode(trim(pathinfo($url, PATHINFO_FILENAME))); $filename = trim(pathinfo($url, PATHINFO_FILENAME));
$ext = strtolower(pathinfo($url, PATHINFO_EXTENSION)); $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
$filename = "$filename.$ext"; $filename = "$filename.$ext";
$url = $this->parseUrl($url);
$path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath); $path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath);
if (!is_dir($path)) { if (!is_dir($path)) {
// 判断路径是否存在,不存在,则创建 // 判断路径是否存在,不存在,则创建
...@@ -65,8 +66,7 @@ class Drawer ...@@ -65,8 +66,7 @@ class Drawer
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$file = curl_exec($ch); $file = curl_exec($ch);
curl_close($ch); curl_close($ch);
$resource = fopen($filePath, 'a');
$resource = fopen($path . '/' . $filename, 'a');
fwrite($resource, $file); fwrite($resource, $file);
fclose($resource); fclose($resource);
} }
...@@ -75,9 +75,12 @@ class Drawer ...@@ -75,9 +75,12 @@ class Drawer
private function parseUrl($url) private function parseUrl($url)
{ {
if (!preg_match('/%[0-9A-Z]{2}/', $url)) {
$url = rawurlencode($url); $url = rawurlencode($url);
$url = str_replace("%3A", ":", $url); $url = str_replace("%3A", ":", $url);
return str_replace("%2F", "/", $url); $url = str_replace("%2F", "/", $url);
}
return $url;
} }
/** /**
...@@ -151,6 +154,64 @@ class Drawer ...@@ -151,6 +154,64 @@ class Drawer
return $res ? $imgNewPath : false; return $res ? $imgNewPath : false;
} }
/**
* 拼图
* @param $imgPathList
* @return bool|string
*/
public function mergeImages($imgPathList)
{
$maxW = $maxH = 0;
$filenameList = [];
$imageList = [];
foreach ($imgPathList as $k => $path) {
$path = $this->parseImagePath($path);
$imgPathInfo = pathinfo($path);
$filename = $imgPathInfo['filename']; // 图片名称
$ext = $imgPathInfo['extension']; // 图片扩展名
[$w, $h] = getimagesize($path); // 图片大小
$imageList[$k] = [
'path' => $path,
'filename' => $filename,
'ext' => $ext,
'w' => $w,
'h' => $h,
];
$filenameList[] = $filename;
$maxW += $w;
if ($maxH < $h) {
$maxH = $h;
}
}
$filenameList = collect($filenameList)->unique()->sort()->toArray();
$filename = implode('_', $filenameList);
$savePath = $this->rootPath . '/download/merge/' . $this->savePath;
if (!is_dir($savePath)) {
// 判断路径是否存在,不存在,则创建
mkdir($savePath, 0777, true);
}
$imgNewPath = $savePath . '/' . $filename . '.png';
if (file_exists($imgNewPath)) {
return $imgNewPath;
}
// 黑色背景图片
$im = @imagecreatetruecolor($maxW, $maxH) or die ("Cannot Initialize new GD image stream");
// 为真彩色画布创建背景,再设置为透明
$color = imagecolorallocate($im, 0, 0, 0);
imagefill($im, 0, 0, $color);
imageColorTransparent($im, $color);
// 循环画图片
$dstX = 0;
foreach ($imageList as $item) {
$resource = imagecreatefromstring(file_get_contents($item['path']));
imagecopy($im, $resource, $dstX, 0, 0, 0, $item['w'], $item['h']);
$dstX += $item['w'];
}
$res = imagepng($im, $imgNewPath);
imagedestroy($im);
return $res ? $imgNewPath : false;
}
/** /**
* 画图片 * 画图片
* @param string $path 图片路径 * @param string $path 图片路径
...@@ -167,7 +228,7 @@ class Drawer ...@@ -167,7 +228,7 @@ class Drawer
$path = $this->addBoard($path, $boardPx); $path = $this->addBoard($path, $boardPx);
} }
$drawing = new Drawing(); $drawing = new Drawing();
$drawing->setPath($path)->setCoordinates($p)->setHeight($h)->setWorksheet($sheet); $drawing->setPath($path)->setCoordinates($p)->setHeight($h)->setOffsetX(1)->setOffsetY(1)->setWorksheet($sheet);
} catch (Exception $e) { } catch (Exception $e) {
put_log($e->getMessage()); put_log($e->getMessage());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment