利用php实现读取excel中的图片-php教程

资源魔 64 0

要完成读取excel中的图片,能够经过phpspreadsheet来完成。phpspreadsheet是一个纯php编写的库,并引入了定名空间、PSR标准等。

应用composer装置phpspreadsheet

composer require phpoffice/phpspreadsheet

GitHub下载:

https://github.com/PHPOffice/PhpSpreadsheet

(收费视频教程保举:php视频教程)

excel图片以下图:

7ead23da258f75fdf3cbfce2f8b7bd8.png

名目实例:

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;
}

后果如图:

3c6886e3cc2e015c0187f01ea9bbde7.png

相干文章教程保举:php教程

以上就是行使php完成读取excel中的图片的具体内容,更多请存眷资源魔其它相干文章!

标签: php 图片 php开发教程 php开发资料 php开发自学 读取 excel

抱歉,评论功能暂时关闭!