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

相关推荐

  • Redis在PHP应用中的数据库索引。

    Redis是一款常用的内存数据库,它被广泛使用在各种语言和应用中,其中包括PHP。 PHP是一种使用广泛的Web编程语言,开发者在使用PHP编写Web应用程序时往往需要使用外部数据存储以及快速访问这些数据。而Redis的快速…

    2023年5月21日
    02
  • PHP入门指南:PHP和Cobol。

    PHP和Cobol是两种非常不同的编程语言,但它们都有着自己的优势和适用范围。本文将深入探讨PHP和Cobol的区别和相似之处,并为初学者提供一份PHP入门指南。PHP是一种开源的脚本语言,通常用于Web开发。它可以嵌入HTML…

    2023年5月22日
    05
  • PHP与数据库监控的集成

    PHP作为Web开发中的常用语言,与数据库的结合使用已经成为常态。在开发过程中,我们不仅需要保证程序的正常运行,还需要对数据库进行监控,及时发现和解决问题。本文将介绍使用PHP进行数据库监控的相关知识和技巧,…

    2023年5月19日
    05
  • PHP实现代码复用的traits新特性的方法

    在阅读yii2源码的时候接触到了trait,就学习了一下,写下博客记录一下。自 PHP 5.4.0 起,PHP 实现了代码复用的一个方法,称为 traits。Traits 是一种为类似 PHP 的单继承语言而准备的代码复用机制。Trait 为了减少…

    2022年6月12日
    0128
  • php如何使用Symfony框架。

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

    2023年6月3日
    03
  • PHP微信开发:如何实现语音识别

    随着移动互联网的普及,微信作为一款社交软件,越来越多的人开始使用,并且微信开放平台也给开发者带来了众多的机会。近年来,随着人工智能技术的发展,语音识别技术逐渐成为了移动端开发的热门技术之一。在微信开…

    2023年5月18日
    09
  • 如何在PHP中实现票务网站。

    随着旅游业的发展和人们休闲需求的增加,票务网站成为越来越受欢迎的在线购票平台。在这篇文章中,我们将介绍如何使用PHP编写一个票务网站。网站需求分析在开发任何网站前,都需要进行需求分析。首先,需要确定网站…

    2023年5月30日
    011
  • PHP中级项目涉及的英语单词!

    B2C:business to customer 商家对客户 C2C:customer to customer 客户对客户 B2B:business to business 商家对商家 B2B2C:business to business to customer 商家对商家对客户 O2O:online to offline 线上线下…

    2018年4月30日
    0348

联系我们

QQ:951076433

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