PHP读取Excel图片对象,并保存替换为相对路径

下面由PHP教程栏目给大家介绍PHP读取Excel图片对象,并保存替换为相对路径方法,希望对需要的朋友有所帮助!

PHP利用PhpSpreadsheet 和 xlswriter 读取Excel图片对象,保存替换为相对路径

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/11 0011
 * Time: 8:59
 */

namespace App\\Services;

use PhpOffice\\PhpSpreadsheet\\Cell\\Coordinate;
use PhpOffice\\PhpSpreadsheet\\Exception;
use PhpOffice\\PhpSpreadsheet\\IOFactory;
use PhpOffice\\PhpSpreadsheet\\Spreadsheet;
use PhpOffice\\PhpSpreadsheet\\Worksheet\\Drawing;
use Vtiful\\Kernel\\Excel;

/**
 * 读取Excel图片并保存其路径
 * Class ExcelImagePathServer
 * @package App\\Services
 */
class ExcelImagePathServer
{
    /**
     * @var string
     */
    protected $relative_path = '/images';

    /**
     * @var Spreadsheet
     */
    protected $spreadsheet;

    /**
     * @var Excel
     */
    protected $xls_writer;

    /**
     * @var Excel
     */
    protected $sheet_writer;

    /**
     * @var string
     */
    protected $image_path;

    /**
     * ExcelImagePathServer constructor.
     * @param string $excel_file
     * @throws \\PhpOffice\\PhpSpreadsheet\\Reader\\Exception
     */
    public function __construct($excel_file)
    {
        $reader = IOFactory::createReader('Xlsx');
        $this->spreadsheet = $reader->load($excel_file);

        $config = ['path' => dirname($excel_file)];
        $this->xls_writer = new Excel($config);

        $this->image_path = dirname($excel_file) . $this->relative_path;
        if (!is_dir($this->image_path)) {
            mkdir($this->image_path, 0755);
        }
    }

    /**
     * @throws Exception
     */
    public function handle()
    {
        $write_filename = date('YmdHis') . '.xlsx';
        $sheetCount = $this->spreadsheet->getSheetCount();
        for ($i = 0; $i < $sheetCount; $i++) {
            $worksheet = $this->spreadsheet->getSheet($i);
            $data = $worksheet->toArray();
            $sheetNames = $this->spreadsheet->getSheetNames();
            var_dump($sheetCount, $sheetNames);
            // 读取并修改
            foreach ($worksheet->getDrawingCollection() as $drawing) {
                /**@var $drawing Drawing* */
                list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
                $image_filename = "/{$i}-" . $drawing->getCoordinates();
                $image_suffix = $this->saveImage($drawing, $image_filename);
                $image_name = ltrim($this->relative_path, '/') . "{$image_filename}.{$image_suffix}";
                var_dump($image_name);
                $startColumn = $this->ABC2decimal($startColumn);

                $data[$startRow - 1][$startColumn] = $image_name;
            }

            // 写入文件
            if ($i == 0) {
                $this->sheet_writer = $this->xls_writer->fileName($write_filename, $sheetNames[$i])->data($data);
            } else {
                // 向文件中追加工作表
                $this->sheet_writer->addSheet($sheetNames[$i])->data($data);
            }
        }
        // 最后的最后,输出文件
        $filePath = $this->sheet_writer->output();
        var_dump($filePath);
    }

    /**
     * 保存图片
     *
     * @param Drawing $drawing
     * @param $image_filename
     * @return string
     * @throws Exception
     */
    protected function saveImage(Drawing $drawing, $image_filename)
    {
        $image_filename .= '.' . $drawing->getExtension();
        switch ($drawing->getExtension()) {
            case 'jpg':
            case 'jpeg':
                $source = imagecreatefromjpeg($drawing->getPath());
                imagejpeg($source, $this->image_path . $image_filename);
                break;
            case 'gif':
                $source = imagecreatefromgif($drawing->getPath());
                imagegif($source, $this->image_path . $image_filename);
                break;
            case 'png':
                $source = imagecreatefrompng($drawing->getPath());
                imagepng($source, $this->image_path . $image_filename);
                break;
            default:
                throw new Exception('image format error!');
        }

        return $drawing->getExtension();
    }

    /**
     * 坐标转换
     *
     * @param $abc
     * @return float|int
     */
    protected 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;
    }
}

关于PHP读取Excel图片对象,并保存替换为相对路径的文章就分享到这,如果对你有帮助欢迎继续关注我们哦

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/44688.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
php学习php学习订阅用户
上一篇 2022年6月27日 00:30
下一篇 2022年6月27日 00:30

相关推荐

  • PHP实现数据库容器化负载均衡的方法。

    随着互联网应用越来越广泛,数据库的负载均衡问题越来越受到关注。在传统的单机架构下,单个数据库的瓶颈会导致系统的响应速度下降或甚至崩溃,因此容器化负载均衡已成为解决数据库性能问题的主要方案之一。本文将…

    2023年5月21日
    00
  • 循环结构---FOR循环

    语法: For(变量初始化;条件表达式;变量的变化){ 循环体; } 案例:1-99 注意:变量初始化可以是多个,用逗号隔开。 注意:条件判断可以是多个,每个用逗号隔开。 图解:

    2018年2月24日 PHP自学教程
    0434
  • 值得一看!高级PHP工程师必备的编码技巧及思维

    好的开发者通常以代码质量来定义。在软件行业,写好的代码意味着在在测试,更新,扩展或者修复漏洞中省钱。本文,我将向你展示一些现实生活中技巧和想法的例子,来帮助你清理你的逻辑代码,重构它,让它变得更健壮…

    2022年6月18日 PHP自学教程
    0114
  • PHP直播功能开发教程:项目实战。

    随着社交媒体的流行和直播行业的兴起,越来越多的企业和机构需要开发自己的直播功能。作为一种流行的后端语言,PHP已经成为目前许多直播项目的开发首选。本文将结合项目实战,为大家分享PHP直播功能开发的技巧和经…

    2023年5月28日
    01
  • PHP8中的数组函数:array_chunk()的高效应用方法。

    随着PHP8的发布,数组函数的效率得到了大幅度提升。其中一个非常有用的数组函数是array_chunk(),它可以将一个数组按照指定大小分割成多个子数组。在本文中,我们将探讨如何高效地使用array_chunk()。简单的使用方…

    2023年5月21日
    00
  • PHP与区块链的集成

    随着区块链技术的发展和应用场景的扩大,越来越多的开发者开始尝试将区块链与传统的编程语言集成,以期能够更好地实现区块链的各种功能和应用。在众多的编程语言中,PHP作为一种常用的开发语言,也在不断地尝试着与…

    2023年5月19日
    033
  • PHP实现数据库集群的方法。

    随着Web应用程序和电子商务网站的不断发展,数据存储和处理的需求也日益增加。为了应对这一挑战,数据库集群成为了一种越来越受欢迎的解决方案。然而,由于各种原因,许多企业选择使用PHP作为开发语言来实现其数据…

    2023年5月21日
    01
  • 微信小程序中PHP实现实时定位。

    随着移动互联网的发展,很多应用都要求实现实时定位功能。而微信小程序是目前最受欢迎的移动端应用之一,因此,如何在微信小程序中实现实时定位功能成为了开发者们关注的焦点。本文将介绍如何利用PHP语言在微信小程…

    2023年6月3日
    04

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息