今天接到了一个从Excel内读取图片的需求,在网上查找了一些资料,基本实现了自己的需求,不过由于查到的一些代码比较久远,不能直接移植到自己的项目里,需要稍加改动一下。
这里介绍一下分别使用phpspreadsheet和PHPExcel扩展库来实现读取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($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;
}
可以看到,图片被读取并存到了本地服务器中
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($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;
}
总结
以上所述是小编给大家介绍的PHP读取Excel内的图片,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。

