当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP读取Excel内的图片

PHP读取Excel内的图片

2019年11月20日  | 移动技术网IT编程  | 我要评论

宝鸡市卫星地图,烟台实木家具价格,农村劳动竟发生怪事 想起都后怕!

今天接到了一个从excel内读取图片的需求,在网上查找了一些资料,基本实现了自己的需求,不过由于查到的一些代码比较久远,里面一些库方法已经被移除不存在了,所以不能直接移植到自己的项目里,需要稍加改动一下。

这里介绍一下分别使用phpspreadsheet和phpexcel扩展库来实现读取excel内图片的功能:

phpspreadsheet

首先安装phpspreadsheet,由于线上服务器php版本是php5.6,所以需要安装兼容php5.6的版本,这里安装1.8.2版本

composer require phpoffice/phpspreadsheet=1.8.2

然后就可以在项目里使用了

use phpoffice\phpspreadsheet\cell\coordinate;
use phpoffice\phpspreadsheet\iofactory;

$imagefilepath = './uploads/imgs/'; //图片本地存储的路径
if (!file_exists($imagefilepath)) { //如果目录不存在则递归创建
    mkdir($imagefilepath, 0777, true);
}

try {
    $inputfilename = './files/1.xlsx';  //包含图片的excel文件
    $objread = iofactory::createreader('xlsx');
    $objspreadsheet = $objread->load($inputfilename);
    $objworksheet = $objspreadsheet->getsheet(0);
    $data = $objworksheet->toarray();

    foreach ($objworksheet->getdrawingcollection() as $drawing) {
        list($startcolumn, $startrow) = coordinate::coordinatefromstring($drawing->getcoordinates());
        $imagefilename = $drawing->getcoordinates() . mt_rand(1000, 9999);

        switch ($drawing->getextension()) {
            case 'jpg':
            case 'jpeg':
                $imagefilename .= '.jpg';
                $source = imagecreatefromjpeg($drawing->getpath());
                imagejpeg($source, $imagefilepath . $imagefilename);
                break;
            case 'gif':
                $imagefilename .= '.gif';
                $source = imagecreatefromgif($drawing->getpath());
                imagegif($source, $imagefilepath . $imagefilename);
                break;
            case 'png':
                $imagefilename .= '.png';
                $source = imagecreatefrompng($drawing->getpath());
                imagepng($source, $imagefilepath, $imagefilename);
                break;
        }
        $startcolumn = abc2decimal($startcolumn);
        $data[$startrow-1][$startcolumn] = $imagefilepath . $imagefilename;
    }
    dump($data);die();
} catch (\exception $e) {
    throw $e;
}

public function abc2decimal($abc)
{
    $ten = 0;
    $len = strlen($abc);
    for($i=1;$i<=$len;$i++){
        $char = substr($abc,0-$i,1);//反向获取单个字符

        $int = ord($char);
        $ten += ($int-65)*pow(26,$i-1);
    }
    return $ten;
}

可以看到,图片被读取并存到了本地服务器中

phpexcel

phpexcel实现从excel文件里读取内容的方法和phpspreadsheet几乎一样,毕竟phpspreadsheet就是在phpexcel基础上写的,不过phpexcel由于已经被废弃了,所以建议优先使用phpspreadsheet,如果原来项目里一直使用了phpexcel也可以继续使用phpexcel的方法

use phpexcel_iofactory;
use phpexcel_cell;

try {
    $inputfilename = './files/1.xlsx';
    $inputfiletype = phpexcel_iofactory::identify($inputfilename);
    $objreader = phpexcel_iofactory::createreader($inputfiletype);
    $objphpexcel = $objreader->load($inputfilename);
} catch (\exception $e) {
    die('加载文件发生错误:"'.pathinfo($inputfilename,pathinfo_basename).'": '.$e->getmessage());
}

$sheet = $objphpexcel->getsheet(0);
$data = $sheet->toarray(); //该方法读取不到图片,图片需单独处理
$imagefilepath = './uploads/imgs/'; //图片本地存储的路径
if (!file_exists($imagefilepath)) {
    mkdir($imagefilepath, 0777, true);
}

//处理图片
foreach ($sheet->getdrawingcollection() as $img) {
    list($startcolumn, $startrow) = phpexcel_cell::coordinatefromstring($img->getcoordinates()); //获取图片所在行和列
    $imagefilename = $img->getcoordinates() . mt_rand(1000, 9999);
    switch($img->getextension()) {
        case 'jpg':
        case 'jpeg':
            $imagefilename .= '.jpeg';
            $source = imagecreatefromjpeg($img->getpath());
            imagejpeg($source, $imagefilepath.$imagefilename);
            break;
        case 'gif':
            $imagefilename .= '.gif';
            $source = imagecreatefromgif($img->getpath());
            imagejpeg($source, $imagefilepath.$imagefilename);
            break;
        case 'png':
            $imagefilename .= '.png';
            $source = imagecreatefrompng($img->getpath());
            imagejpeg($source, $imagefilepath.$imagefilename);
            break;
    }
    $startcolumn = abc2decimal($startcolumn);
    $data[$startrow-1][$startcolumn] = $imagefilepath . $imagefilename;

}
var_dump($data);

public function abc2decimal($abc)
{
    $ten = 0;
    $len = strlen($abc);
    for($i=1;$i<=$len;$i++){
        $char = substr($abc,0-$i,1);//反向获取单个字符

        $int = ord($char);
        $ten += ($int-65)*pow(26,$i-1);
    }
    return $ten;
}

参考文章:

phpexcel数据导入(含图片)

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网