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和XML。

    PHP是一种流行的Web编程语言,已经被广泛应用于互联网和企业应用。PHP可以动态生成Web页面,提供功能强大的数据处理和交互。而XML是一种可扩展的标记语言,可以用来描绘复杂的数据结构和关系,是数据交换和存储的重…

    2023年5月30日
    00
  • PHP8.0中的数据验证库:Respect

    随着PHP语言的普及和应用范围的扩大,数据验证也变得越来越重要。数据验证是一个Web应用中的重要环节,负责验证和过滤用户提交的数据并确保其完整性和有效性。如果没有一个强大的数据验证系统来保证数据的安全性和…

    2023年5月19日
    09
  • PHP和Redis中的LUA脚本使用方法

    PHP和Redis中的LUA脚本使用方法LUA 是一种轻量级的脚本语言,旨在提供高效的嵌入式扩展功能。Redis 是一种开源的 NoSQL 数据库,提供高效的键值存储和缓存功能。在 Redis 中使用 LUA 脚本可以大大提高数据处理效率…

    2023年5月19日
    09
  • 教你php如何连html。

    在PHP中连接HTML,我们首先需要了解什么是PHP和HTML。 (图片来源网络,侵删) PHP是一种服务器端的脚本语言,主要用于Web开发,它可以嵌入到HTML中,通过服务器端处理后发送给客户端浏览器,然后由浏览器解释并显…

    2024年6月25日
    01
  • 新手应该对php有个全面的了解

    比如我们是做PHP工程师。那么我们要熟悉的点有: 1、编程语言:首先就是PHP,要熟悉PHP的面向对象,PHP每个版本的特性的不同等等,一些坑,还有就是熟读文档,当然最后还能熟悉其他语言,比如Python,Node等等 &nbs…

    2018年2月26日
    0266
  • 我的PHP学习第二十四天之PHP环境搭建

    什么是PHP? PHP是运行在服务器端的脚本语言,配合mysql和html实现动态网站。   脚本语言:编程语言有更加严格的规范。编程语言不能直接执行,需要编译后再执行。脚本文件可以直接被执行。 网站: 用户角度:…

    2016年5月24日 PHP自学教程
    01.1K
  • PHP微信开发:如何实现群发消息发送记录

    随着微信成为了人们生活中越来越重要的一个通讯工具,其敏捷的消息传递功能迅速受到广大企业和个人的青睐。对于企业而言,将微信发展为一个营销平台已经成为趋势,而微信开发的重要性也逐渐凸显。在其中,群发功能…

    2023年5月18日
    02
  • PHP函数的封装性

    使用一个表单,输入任意数字,使之可以在2,8,16进制到10进制或10进制到2,8,16进制之间转换,形式大致如下如下: 原始代码实现 Document 数: 十进制转二进制 二进制转十进制

    2018年4月5日 PHP自学教程
    0213

联系我们

QQ:951076433

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