PHP读取Excel图片对象,并保存替换为相对路径PHP读取擅长图片对象,并保存替换为相对路径,下面由教程栏目给大家介绍服务器端编程语言(专业超文本预处理器的缩写)读取擅长图片对象,并保存替换为相对路径方法,希望对需要的朋友有所帮助!
服务器端编程语言(专业超文本预处理器的缩写)利用phpspredadssheet和xlswriter读取擅长图片对象,保存替换为相对路径
?服务器端编程语言(专业超文本预处理器的缩写)
/**
*由PhpStorm创建。
*用户:管理员
*日期: 2021/1/11 0011
*时间: 8:59
*/
命名空间应用程序\服务;
使用PhPoffice \ phpspredsheet \ Cell \ Coordinate;
使用PhPoffice \ PhPSPredssheet \异常;
使用PhPoffice \ phpspredsheet \ IOFactory;
使用PhpOffice \ PhpSpreadsheet \电子表格;
使用PhpOffice \ PhpSpreadsheet \工作表\绘图;
使用Vtiful \内核\ Excel
/**
* 读取擅长图片并保存其路径
*类ExcelImagePathServer
* @包应用程序\服务
*/
类ExcelImagePathServer
{
/**
* @var字符串
*/
protected $ relative _ path=’/images ‘;
/**
* @var电子表格
*/
受保护的$电子表格;
/**
* @var Excel
*/
受保护的$ xls _ writer
/**
* @var Excel
*/
受保护的$ sheet _ writer
/**
* @var字符串
*/
受保护的$ image _ path
/**
* ExcelImagePathServer构造函数。
* @param string $excel_file
* @抛出\ PhpOFFiCE \ PhpPredssheet \ Reader \异常
*/
public function _ _ construct($ excel _ file)
{
$ reader=io factory : create reader(‘ Xlsx ‘);
$这个-电子表格=$ reader-load($ excel _ file);
$ config=[‘ path ‘=dirname($ excel _ file)];
$ this-xls _ writer=new Excel($ config);
$ this-image _ path=dirname($ excel _ file).$ this-relative _ path;
if(!is_dir($this-image_path)) {
mkdir($this-image_path,0755);
}
}
/**
* @引发异常
*/
公共函数句柄()
{
$ write _ filename=date(‘ YmdHis ‘). xlsx ‘;
$工作表数=$ this-电子表格-GetSheetCount();
for($ I=0;$ i $ sheetCount$i ) {
$工作表=$这个-电子表格-GetSheet($ I);
$ data=$工作表-to array();
$ sheet name=$ this-电子表格-GetSheetNames();
var_dump($sheetCount,$ sheet names
//读取并修改
foreach($工作表-GetDrawingCollection()(作为$drawing) {
/**@var $drawing Drawing* */
list($startColumn,$ start row)=坐标:3360从字符串开始协调($ drawing-GetCoords());
$image_filename=’/{$i}-‘ .$ drawing-GetCoords();
$ image _ suffix=$ this-保存图像($ drawing,$ image _ filename);
$ image _ name=ltrim($ this-relative _ path,’/’).{$image_filename} .{ $ image _ suffix } ‘;
var _ dump($ image _ name);
$ start column=$ this-ABC2d decimal($ start column);
$ data[$ StartRow-1][$ StartColumn]=$ image _ name;
}
//写入文件
if ($i==0) {
$ this-sheet _ writer=$ this-xls _ writer-fileName($ write _ fileName,$ sheet name[$ I])-data($ data);
} else {
//向文件中追加工作表
$ this-sheet _ writer-addSheet($ sheet name[$ I])-data($ data);
}
}
//最后的最后,输出文件
$ FilePath=$ this-sheet _ writer-output();
var _ dump($ FilePath);
}
/**
* 保存图片
*
* @参数绘图$绘图
* @param $image_filename
* @返回字符串
* @引发异常
*/
受保护的函数保存图像(绘图$drawing,$image_filename)
{
$image_filename .=’.’。$ drawing-GetExtension();
switch($ drawing-GetExtension()){
case ‘jpg’:
大小写jpeg’:
$ source=imagecreatefrom JPEG($ drawing-GetPath());
imagejpeg($source,$this-image_path .$ image _ filename);
打破;
case ‘gif’:
$ source=imagecreatefromgif($ drawing-GetPath());
imagegif($source,$this-image_path .$ image _ filename);
打破;
case ‘png’:
$ source=imagecreatefrompng($ drawing-GetPath());
imagepng($source,$this-image_path .$ image _ filename);
打破;
默认值:
引发新异常(‘图像格式错误!’);
}
return $ drawing-GetExtension();
}
/**
* 坐标转换
*
* @param $abc
* @return float|int
*/
受保护的功能ABC 2分类(ABC)
{
$ ten=0;
$ len=strlen($ ABC);
for($ I=1;$ i=$ len$i ) {
$char=substr($abc,0 – $i,1);//反向获取单个字符
$ int=order($ char);
$ten=($int – 65) * pow(26,$ I-1);
}
返还十美元;
}