Home > Backend Development > PHP Tutorial > Use php to read pictures in excel

Use php to read pictures in excel

王林
Release: 2023-04-08 09:20:01
forward
7001 people have browsed it

Use php to read pictures in excel

To read pictures in excel, you can use phpspreadsheet. phpspreadsheet is a library written in pure PHP and introduces namespaces, PSR specifications, etc.

Install phpspreadsheet using composer

composer require phpoffice/phpspreadsheet
Copy after login

GitHub download:

https://github.com/PHPOffice/PhpSpreadsheet

(Free video tutorial recommendation: php video tutorial)

excel picture is as shown below:

Use php to read pictures in excel

Project example:

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;
}
Copy after login

The result is as shown in the figure:

Use php to read pictures in excel

Recommended related articles and tutorials: php tutorial

The above is the detailed content of Use php to read pictures in excel. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template