Home  >  Article  >  Backend Development  >  How to read pictures in Excel with PHP (with code)

How to read pictures in Excel with PHP (with code)

angryTom
angryTomforward
2019-11-28 14:57:105385browse

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:

How to read pictures in Excel with PHP (with code)

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

How to read pictures in Excel with PHP (with code)

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 = &#39;./files/1.xlsx&#39;;
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch (\Exception $e) {
    die(&#39;加载文件发生错误:"&#39;.pathinfo($inputFileName,PATHINFO_BASENAME).&#39;": &#39;.$e->getMessage());
}

$sheet = $objPHPExcel->getSheet(0);
$data = $sheet->toArray(); //该方法读取不到图片,图片需单独处理
$imageFilePath = &#39;./uploads/imgs/&#39;; //图片本地存储的路径
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 &#39;jpg&#39;:
        case &#39;jpeg&#39;:
            $imageFileName .= &#39;.jpeg&#39;;
            $source = imagecreatefromjpeg($img->getPath());
            imagejpeg($source, $imageFilePath.$imageFileName);
            break;
        case &#39;gif&#39;:
            $imageFileName .= &#39;.gif&#39;;
            $source = imagecreatefromgif($img->getPath());
            imagejpeg($source, $imageFilePath.$imageFileName);
            break;
        case &#39;png&#39;:
            $imageFileName .= &#39;.png&#39;;
            $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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete