您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
踩坑系列-php读取excel中的图片
发布时间:2022-05-27 23:01:43编辑:雪饮阅读()
本来打算用js的xlsx这个第三方插件去读取excel,但是客户发来的这个excel中还有图片,对于图片来说,这个excel有点无能为力,虽然有查到资料以node来实现,但这样就违背了初衷。我想纯前端实现。
最后既然都是用服务端实现,我何不直接使用我更喜欢的php来实现。但php这里也是有坑的,php这里读取excel中的图片不能和数据一起读,比如你想要获取完整excel的数据,则你需要读取一次所有基本数据,然后再重新读取,这第二次读取时候只读取图片
,然后读取图片时候拿到对应行后去和刚才读取的基本数据的对应行进行合并即可,那么我这里只实现了纯读取图片的,然后上传到阿里云oss上后返回所有已上传到阿里云oss的图片数组及对应excel的行给前端,则前端就可以通过对应行拿到对应图片了。:
★雪飲★ 2022/5/27 20:50:51
踩坑系列之php提取excel图片:
public function excel_extract_img(){
$file_excel= $_FILES["excel"];
$file=$file_excel["tmp_name"];
$imageFilePath=$_SERVER["DOCUMENT_ROOT"]."/assets/excel/Achievements";
$inputFileName = $file;
$objRead = IOFactory::createReader('Xlsx');
$objSpreadsheet = $objRead->load($inputFileName);
$objWorksheet = $objSpreadsheet->getSheet(0);
$data = $objWorksheet->toArray();
foreach ($objWorksheet->getDrawingCollection() as $key=>$drawing) {
list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
$imageFileName = $drawing->getCoordinates() . mt_rand(1000, 9999);
$flag=false;
$upload_file=$write_file=$imageFilePath."/".$key.".jpg";
$upload_key="achievements_import_".$key."_".date("Y_m_d_H_i_s",time()).".jpg";
$really_row=$startRow-1;
switch ($drawing->getExtension()) {
case 'jpg':
case 'jpeg':
$imageFileName .= '.jpg';
$source = imagecreatefromjpeg($drawing->getPath());
imagejpeg($source, $write_file);
$upload_res=uploadImg($upload_file,$upload_key);
$current_img["key"]=$really_row;
$current_img["val"]=$upload_res["url"];
$imgs[]=$current_img;
@unlink($upload_file);
break;
case 'gif':
$imageFileName .= '.jpg';
$source = imagecreatefromgif($drawing->getPath());
imagegif($source, $write_file);
$upload_res=uploadImg($upload_file,$upload_key);
$current_img["key"]=$really_row;
$current_img["val"]=$upload_res["url"];
$imgs[]=$current_img;
@unlink($upload_file);
break;
case 'png':
$imageFileName .= '.jpg';
$source = imagecreatefrompng($drawing->getPath());
imagepng($source, $write_file);
$upload_res=uploadImg($upload_file,$upload_key);
$current_img["key"]=$really_row;
$current_img["val"]=$upload_res["url"];
$imgs[]=$current_img;
@unlink($upload_file);
break;
}
$startColumn = $this->ABC2decimal($startColumn);
$data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName;
}
$this->success($imgs);
}
★雪飲★ 2022/5/27 20:51:07
//字母(26)进制转10进制
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;
}
★雪飲★ 2022/5/27 20:52:06
用到的接口:PhpOffice\PhpSpreadsheet\IOFactory
关键字词:php,excel,图片,读取
相关文章
- 踩坑系列-php的max_input_vars, php post 最大接收数
- php實現壓縮一個目錄支持多級目錄(解壓時可以鼠標右鍵
- Dockerfile的使用(基於php官方docker鏡像安裝擴展)
- docker-composer脚本編寫系列之nginx與php的結合
- webman-配置數據庫-pdo_pgsql擴展安裝(php7.3.4)
- webman-配置數據庫-psql基本使用及php7.3.4中pgsql擴
- 基于GuzzleHttp(thinkphp5实现原生获取獲取微信公衆平
- workerman16进制与2进制字符串的转换(局限有点狭隘,應該
- workerman系列之tp5(thinkphp5)日志结合workerman使用
- workerman常见问题-php的几种回调写法-类的静态方法作