maatwebsite/excel使用:导出——插图 | laravel china 社区-大发黄金版app下载
通过使用 withdrawings ,你可以向工作表中添加一个或多个插图。
创建插图实例
首先,你需要实例化一个新的 \phpoffice\phpspreadsheet\worksheet\drawing,并为其属性分配有意义的值。
$drawing = new \phpoffice\phpspreadsheet\worksheet\drawing();
$drawing->setname('logo');
$drawing->setdescription('这是我的标志');
$drawing->setpath(public_path('/img/logo.jpg'));
$drawing->setheight(90);你可以在  中查看 drawing 的所有可用属性。
添加单个插图
当你实例化了插图后,可以在导出类中添加 withdrawings ,并在 drawings 方法中返回插图实例。
namespace app\exports;
use maatwebsite\excel\concerns\withdrawings;
use phpoffice\phpspreadsheet\worksheet\drawing;
class invoicesexport implements withdrawings
{
    public function drawings()
    {
        $drawing = new drawing();
        $drawing->setname('logo');
        $drawing->setdescription('这是我的标志');
        $drawing->setpath(public_path('/img/logo.jpg'));
        $drawing->setheight(90);
        $drawing->setcoordinates('b3');
        return $drawing;
    }
}添加多个插图
你可以通过在 drawings 方法中返回一个数组来向工作表中添加多个插图。
namespace app\exports;
use maatwebsite\excel\concerns\withdrawings;
use phpoffice\phpspreadsheet\worksheet\drawing;
class invoicesexport implements withdrawings
{
    public function drawings()
    {
        $drawing = new drawing();
        $drawing->setname('logo');
        $drawing->setdescription('这是我的标志');
        $drawing->setpath(public_path('/img/logo.jpg'));
        $drawing->setheight(50);
        $drawing->setcoordinates('b3');
        $drawing2 = new drawing();
        $drawing2->setname('其他图片');
        $drawing2->setdescription('这是第二张图片');
        $drawing2->setpath(public_path('/img/other.jpg'));
        $drawing2->setheight(120);
        $drawing2->setcoordinates('g2');
        return [$drawing, $drawing2];
    }
}添加远程图片的插图
你可以通过实例化一个新的 \phpoffice\phpspreadsheet\worksheet\memorydrawing,并从外部 url 获取二进制图像数据,然后将其赋值给 setimageresource。在 drawings 方法中返回插图实例。
namespace app\exports;
use maatwebsite\excel\concerns\withdrawings;
use phpoffice\phpspreadsheet\worksheet\memorydrawing;
class invoicesexport implements withdrawings
{
    public function drawings()
    {
        if (!$imageresource = @imagecreatefromstring(file_get_contents('http://example.jpg'))) {
            throw new \exception('无法将图片 url 转换为图像资源。');
        }
        $drawing = new memorydrawing();
        $drawing->setname('logo');
        $drawing->setdescription('这是我的标志');
        $drawing->setimageresource($imageresource);
        $drawing->setheight(90);
        $drawing->setcoordinates('b3');
        return $drawing;
    }
}本作品采用《cc 协议》,转载必须注明作者和本文链接
