PHP如何读取Excel图片对象并保存替换为相对路径
这篇文章将为大家详细讲解有关PHP如何读取Excel图片对象并保存替换为相对路径,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联专注于镇宁网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供镇宁营销型网站建设,镇宁网站制作、镇宁网页设计、镇宁网站官网定制、微信小程序定制开发服务,打造镇宁网络公司原创品牌,更为您提供镇宁网站排名全网营销落地服务。
PHP利用PhpSpreadsheet 和 xlswriter 读取Excel图片对象,保存替换为相对路径
spreadsheet = $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); } } /** * @throws Exception */ public function handle() { $write_filename = date('YmdHis') . '.xlsx'; $sheetCount = $this->spreadsheet->getSheetCount(); for ($i = 0; $i < $sheetCount; $i++) { $worksheet = $this->spreadsheet->getSheet($i); $data = $worksheet->toArray(); $sheetNames = $this->spreadsheet->getSheetNames(); var_dump($sheetCount, $sheetNames); // 读取并修改 foreach ($worksheet->getDrawingCollection() as $drawing) { /**@var $drawing Drawing* */ list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates()); $image_filename = "/{$i}-" . $drawing->getCoordinates(); $image_suffix = $this->saveImage($drawing, $image_filename); $image_name = ltrim($this->relative_path, '/') . "{$image_filename}.{$image_suffix}"; var_dump($image_name); $startColumn = $this->ABC2decimal($startColumn); $data[$startRow - 1][$startColumn] = $image_name; } // 写入文件 if ($i == 0) { $this->sheet_writer = $this->xls_writer->fileName($write_filename, $sheetNames[$i])->data($data); } else { // 向文件中追加工作表 $this->sheet_writer->addSheet($sheetNames[$i])->data($data); } } // 最后的最后,输出文件 $filePath = $this->sheet_writer->output(); var_dump($filePath); } /** * 保存图片 * * @param Drawing $drawing * @param $image_filename * @return string * @throws Exception */ protected function saveImage(Drawing $drawing, $image_filename) { $image_filename .= '.' . $drawing->getExtension(); switch ($drawing->getExtension()) { case 'jpg': case 'jpeg': $source = imagecreatefromjpeg($drawing->getPath()); imagejpeg($source, $this->image_path . $image_filename); break; case 'gif': $source = imagecreatefromgif($drawing->getPath()); imagegif($source, $this->image_path . $image_filename); break; case 'png': $source = imagecreatefrompng($drawing->getPath()); imagepng($source, $this->image_path . $image_filename); break; default: throw new Exception('image format error!'); } return $drawing->getExtension(); } /** * 坐标转换 * * @param $abc * @return float|int */ protected 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图片对象并保存替换为相对路径”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
网站标题:PHP如何读取Excel图片对象并保存替换为相对路径
文章地址:http://pwwzsj.com/article/gsojjg.html