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设置虚拟主机的方法是什么意思。

    PHP虚拟主机是一种虚拟的服务器,可以在同一台物理主机上托管多个域名,并根据不同的域名提供不同的服务。在PHP中设置虚拟主机的方法有很多种,其中一种方法是在Apache PHP虚拟主机配置步骤中进行配置 。 什么是虚…

    2024年7月14日
    05
  • 循环结构---FOR循环

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

    2018年2月24日 PHP自学教程
    0438
  • php基本语法形式及变量规则

    基本语法形式 区分大小写 主要是指变量名区分大小写 常量通常也区分,但常量也可以自己设定为不区分(不推荐) 但函数名不区分 而系统中使用的关键字也不区分大小写,比如if, else, for 语句结束符,用分号 一个p…

    2017年11月1日
    0199
  • 重蔚php学习第二十七天——php可变变量,匿名函数

    运算符:赋值运算符,算术运算符,错误抑制符,比较运算符,逻辑运算符,三元运算符,位运算符,连接运算符,自操作运算符(在项目计算当中,非常不建议使用多个自操作一起运算) 源码反码补码 整数的原码反码和补…

    2017年5月5日 PHP自学教程
    0380
  • PHP与MySQL查询优化。

    随着互联网和信息技术的发展,Web应用程序成为了企业和个人必备的一项技术。在Web应用程序中,PHP与MySQL是相当重要的技术,PHP提供了丰富的功能和特性,而MySQL则是用于存储和管理数据。然而,当数据量增加时,查…

    2023年5月21日
    00
  • PHP实现异步的三种方式

    三种实现方式通用的异步执行文件 exec.phpsleep(8); $data = "--- type " . date("Y-m-d H:i:s") . " ---\\\\n"; file_put_contents("../log.txt", $data, FILE_APPEND);p…

    2022年6月27日
    0208
  • php运行原理和环境的搭建。

    php语言运行原理 客户端技术:                                                服务器端技术: html                                    web服务器软件:            服务器端语言:        数据库: css       …

    2020年11月13日 PHP自学教程
    0354
  • 详解php版阿里云OSS图片上传类

    本文实例讲述了php版阿里云OSS图片上传类。分享给大家供大家参考,具体如下:【

    2022年6月17日
    0206

联系我们

QQ:951076433

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