温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

PHP如何读取Excel内的图片

发布时间:2021-05-11 10:49:21 阅读:1088 作者:小新 栏目:开发技术
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

小编给大家分享一下PHP如何读取Excel内的图片,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

php的框架有哪些

php的框架:1、Laravel,Laravel是一款免费并且开源的PHP应用框架。2、Phalcon,Phalcon是运行速度最快的一个PHP框架。3、Symfony,Symfony是一款为Web项目准备的PHP框架。4、Yii,Yii是一款快速、安全和专业的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的开源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能强大的PHP框架。

分别使用phpspreadsheet和PHPExcel扩展库来实现读取Excel内图片的功能:

PHP如何读取Excel内的图片 

PHPSpreadsheet

首先安装phpspreadsheet,由于线上服务器PHP版本是PHP5.6,所以需要安装兼容PHP5.6的版本,这里安装1.8.2版本

composer require phpoffice/phpspreadsheet=1.8.2

然后就可以在项目里使用了

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\IOFactory;
$imageFilePath './uploads/imgs/'//图片本地存储的路径
if (!file_exists($imageFilePath)) { //如果目录不存在则递归创建
 mkdir($imageFilePath0777true);
}
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(10009999);
  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;
}

可以看到,图片被读取并存到了本地服务器中

PHP如何读取Excel内的图片 

PHPExcel

PHPExcel实现从Excel文件里读取内容的方法和phpspreadsheet几乎一样,毕竟phpspreadsheet就是在PHPExcel基础上写的,不过PHPExcel由于已经被废弃了,所以建议优先使用phpspreadsheet,如果原来项目里一直使用了PHPExcel也可以继续使用PHPExcel的方法

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($imageFilePath0777true);
}
//处理图片
foreach ($sheet->getDrawingCollection() as $img) {
 list($startColumn$startRow) = PHPExcel_Cell::coordinateFromString($img->getCoordinates()); //获取图片所在行和列
 $imageFileName $img->getCoordinates() . mt_rand(10009999);
 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;
}

以上是“PHP如何读取Excel内的图片”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×