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 应用程序安全性的具体步骤。我关注的是 PHP 配置本身,所以我们不会讨论 SQL 注入、HTTPS 或其他与 PHP 无关的问题。我将使用我的docker-ent…

    2022年6月21日
    0118
  • PHP中如何进行前端框架和后端框架的集成?

    随着Web应用程序开发的日益复杂和需要的交互性越来越高,使用前端框架和后端框架已经变得非常普遍。在此过程中,集成前端框架和后端框架也成为必不可少的步骤,以确保应用程序的顺畅运行和高效性能。本文将重点介绍…

    2023年5月17日
    00
  • PHP常用函数大全-(1)php数组处理常用的函数

    (1)php数组处理常用的函数 array_change_key_case — 返回字符串键名全为小写或大写的数组 array_chunk — 将一个数组分割成多个 array_combine — 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值…

    2015年12月7日
    0315
  • PHP入门指南:PHP和XML。

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

    2023年5月30日
    00
  • PHP与传统SQL数据库的对比。

    PHP是一种开源脚本语言,广泛用于Web开发。SQL(Structured Query Language)则是一种用于访问和管理关系数据库的标准语言。在Web开发中,PHP与SQL是两个常用的工具。本文将比较PHP与传统SQL数据库之间的关系,探讨…

    2023年5月21日
    03
  • PHP函数的封装性

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

    2018年4月5日 PHP自学教程
    0208
  • php如何使用Symfony框架。

    PHP是现在互联网开发的主流技术之一,而Symfony框架则是一种基于PHP的Web应用程序框架,它可以帮助开发人员构建高质量、可维护的Web应用程序。下面我们就来讲一下如何使用Symfony框架。一、Symfony框架简介Symfony…

    2023年6月3日
    01
  • PHP 判断用户的设备是否是移动设备(附代码)

    我们做网站的时候会有做到移动端和pc端,但是为了给用户良好的体验,我们会将pc端的用户和移动端的用户进行区分。下面就通过php判断用户的设备是否是移动设备 代码如下:  

    2018年3月7日
    0387

联系我们

QQ:951076433

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