Commit 00a952d0 authored by 王源's avatar 王源
Browse files

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

parent 20fc68ee
Loading
Loading
Loading
Loading
+69 −8
Original line number Diff line number Diff line
@@ -46,11 +46,12 @@ class Drawer
     */
    public function downloadWebImage($url, $path = null)
    {
        $url = $this->parseUrl($url);
        // excel画图中下载图片时对图片名做urlencode处理,防止中文名不能正常画图片的bug
        $filename = urlencode(trim(pathinfo($url, PATHINFO_FILENAME)));
        $filename = trim(pathinfo($url, PATHINFO_FILENAME));
        $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION));
        $filename = "$filename.$ext";
        $url = $this->parseUrl($url);

        $path = $this->rootPath . '/download/images/' . ($path ?: $this->savePath);
        if (!is_dir($path)) {
            // 判断路径是否存在,不存在,则创建
@@ -65,8 +66,7 @@ class Drawer
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
            $file = curl_exec($ch);
            curl_close($ch);

            $resource = fopen($path . '/' . $filename, 'a');
            $resource = fopen($filePath, 'a');
            fwrite($resource, $file);
            fclose($resource);
        }
@@ -75,9 +75,12 @@ class Drawer

    private function parseUrl($url)
    {
        if (!preg_match('/%[0-9A-Z]{2}/', $url)) {
            $url = rawurlencode($url);
            $url = str_replace("%3A", ":", $url);
        return str_replace("%2F", "/", $url);
            $url = str_replace("%2F", "/", $url);
        }
        return $url;
    }

    /**
@@ -151,6 +154,64 @@ class Drawer
        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 图片路径
@@ -167,7 +228,7 @@ class Drawer
                $path = $this->addBoard($path, $boardPx);
            }
            $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) {
            put_log($e->getMessage());
        }