How to read pictures in Excel with PHP (with code)
Today I received a request to read pictures from Excel. I searched for some information on the Internet and basically realized my needs. However, because some of the codes found are relatively old, there are Some library methods have been removed and no longer exist, so they cannot be directly transplanted to your own project and need to be slightly modified.
(Recommended learning: PHP video tutorial)
Here we introduce how to use phpspreadsheet and PHPExcel extension library to read pictures in Excel. Function:
PHPSpreadsheet
First install phpspreadsheet. Since the online server PHP version is PHP5.6, it needs to be installed to be compatible with PHP5 .6 version, install version 1.8.2 here
composer require phpoffice/phpspreadsheet=1.8.2
Then you can use it in the project
use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\IOFactory; $imageFilePath = './uploads/imgs/'; //图片本地存储的路径 if (!file_exists($imageFilePath)) { //如果目录不存在则递归创建 mkdir($imageFilePath, 0777, true); } try { $inputFileName = './files/1.xlsx'; //包含图片的Excel文件 $objRead = IOFactory::createReader('Xlsx'); $objSpreadsheet = $objRead->load($inputFileName); $objWorksheet = $objSpreadsheet->getSheet(0); $data = $objWorksheet->toArray(); foreach ($objWorksheet->getDrawingCollection() as $drawing) { list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates()); $imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999); switch ($drawing->getExtension()) { case 'jpg': case 'jpeg': $imageFileName .= '.jpg'; $source = imagecreatefromjpeg($drawing->getPath()); imagejpeg($source, $imageFilePath . $imageFileName); break; case 'gif': $imageFileName .= '.gif'; $source = imagecreatefromgif($drawing->getPath()); imagegif($source, $imageFilePath . $imageFileName); break; case 'png': $imageFileName .= '.png'; $source = imagecreatefrompng($drawing->getPath()); imagepng($source, $imageFilePath, $imageFileName); break; } $startColumn = ABC2decimal($startColumn); $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName; } dump($data);die(); } catch (\Exception $e) { throw $e; } public 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; }
You can see that the image is read and saved to the local server
PHPExcel
PHPExcel’s method of reading content from Excel files is almost the same as phpspreadsheet. After all, phpspreadsheet is written on the basis of PHPExcel. However, since PHPExcel has been abandoned, it is recommended to use phpspreadsheet first. If PHPExcel has been used in the original project, you can continue to use PHPExcel's method
use PHPExcel_IOFactory; use PHPExcel_Cell; try { $inputFileName = './files/1.xlsx'; $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); } catch (\Exception $e) { die('加载文件发生错误:"'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); } $sheet = $objPHPExcel->getSheet(0); $data = $sheet->toArray(); //该方法读取不到图片,图片需单独处理 $imageFilePath = './uploads/imgs/'; //图片本地存储的路径 if (!file_exists($imageFilePath)) { mkdir($imageFilePath, 0777, true); } //处理图片 foreach ($sheet->getDrawingCollection() as $img) { list($startColumn, $startRow) = PHPExcel_Cell::coordinateFromString($img->getCoordinates()); //获取图片所在行和列 $imageFileName = $img->getCoordinates() . mt_rand(1000, 9999); switch($img->getExtension()) { case 'jpg': case 'jpeg': $imageFileName .= '.jpeg'; $source = imagecreatefromjpeg($img->getPath()); imagejpeg($source, $imageFilePath.$imageFileName); break; case 'gif': $imageFileName .= '.gif'; $source = imagecreatefromgif($img->getPath()); imagejpeg($source, $imageFilePath.$imageFileName); break; case 'png': $imageFileName .= '.png'; $source = imagecreatefrompng($img->getPath()); imagejpeg($source, $imageFilePath.$imageFileName); break; } $startColumn = ABC2decimal($startColumn); $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName; } var_dump($data); public 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; }
This article comes from php Chinese website, php question Column, welcome to learn!
The above is the detailed content of How to read pictures in Excel with PHP (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
