Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHPExcel中检索图像_Php_Phpexcel - Fatal编程技术网

在PHPExcel中检索图像

在PHPExcel中检索图像,php,phpexcel,Php,Phpexcel,我想使用PHPExcel从excel文件到PHP读取/检索图像。 此代码用于从特定单元格中检索值 $objPHPExcel->getActiveSheet()->getCell('B5')->getValue(); 这仅检索单元格值,但我无法检索图像。有办法吗?作为第二个结果产生。它告诉你怎么做 要直接引用相关信息: $objPHPExcel->getActiveSheet()->getDrawingCollection() 将返回活动工作表中所有图像对象的Array


我想使用PHPExcel从excel文件到PHP读取/检索图像。 此代码用于从特定单元格中检索值

    $objPHPExcel->getActiveSheet()->getCell('B5')->getValue();
这仅检索单元格值,但我无法检索图像。有办法吗?

作为第二个结果产生。它告诉你怎么做

要直接引用相关信息:

$objPHPExcel->getActiveSheet()->getDrawingCollection()
将返回活动工作表中所有图像对象的ArrayObject

这些对象将是
PHPExcel\u工作表\u绘图
PHPExcel\u工作表\u内存绘图
对象:您可以使用以下方法来识别哪个对象。然后,您可以使用适用于该类的方法(如API中所述)从文件(用于
PHPExcel\u工作表\u绘图
对象)或直接从
PHPExcel\u工作表\u内存绘图
对象本身读取图像数据。可以使用
getName()
getDescription()
方法从图像对象检索相关值

请注意,还可以将图像对象与打印标题关联:

$objPHPExcel->getActiveSheet()->getHeaderFooter()->getImages()
可用于从页眉/页脚检索图像。这是一个包含
PHPExcel\u工作表\u HeaderFooterDrawing
对象的数组。所有的
PHPExcel\u工作表\u绘图
方法都可用于从这些对象提取图像文件


检查这个例子。我发现它很有用

$objPHPExcel = PHPExcel_IOFactory::load($_FILES['archivo']['tmp_name']);

$i = 0;
foreach ($objPHPExcel->getActiveSheet()->getDrawingCollection() as $drawing) {
    if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );

        $imageContents = ob_get_contents();
        ob_end_clean();
        $extension = 'png';
    } else {
        $zipReader = fopen($drawing->getPath(),'r');
        $imageContents = '';

        while (!feof($zipReader)) {
            $imageContents .= fread($zipReader,1024);
        }
        fclose($zipReader);
        $extension = $drawing->getExtension();
    }
    $myFileName = '00_Image_'.++$i.'.'.$extension;
    file_put_contents($myFileName,$imageContents);
}
来源:

类ExcelImport { /** *@var */ 受保护的$excel; /** *@var */ 受保护的$工作表; /** *@var数组 */ 受保护的$excel_数据=[]; /** *ExcelImport构造函数。 *@param Request$Request *@throws\PHPExcel\u异常 *@throws\PHPExcel\u Reader\u异常 */ 公共函数构造(请求$Request) { //从请求加载文件 $this->excel=PHPExcel_IOFactory::load($request->file('file')); //获取活动工作表 $this->work_sheet=$this->excel->getActiveSheet(); } /** *@return数组 */ 公共函数导入() { //遍历图形集合 foreach($this->work\u sheet->getDrawingCollection()作为$drawing){ //检查是否为图形实例 if($PHPExcel的绘图实例\U工作表\U绘图){ //创建具有扩展名的图像名称 $file_name=str_replace(“”,“,$drawing->getName()).”..$drawing->getExtension(); //从path获取图像内容并将其存储在Laravel存储器中 存储::put('public/'。$file\u name,file\u get\u contents($drawing->getPath()); //最初创建图像数组 $this->excel\u数据[]=[ “图像”=>$file\u名称 ]; } } //映射工作表中的其他数据 返回$this->rowData(); } /** *@return数组 */ 私有函数rowData() { $i=0; //逐行迭代 foreach($this->work\u sheet->getRowIterator(2)作为$row){ //逐行遍历单元格 foreach($row->getCellIterator()作为$cell){ //如果图像数据为空,请继续 //我们已经在数组中填充了它们 如果(为空($cell->getValue()){continue;} //将其他excel数据映射到数组中 $this->excel_data[$i]['name']=$cell->getValue(); } $i++; } //返回最终数据数组 返回$this->excel\u数据; } }
来源(我的博客):

实际上,图像覆盖在单元格上,而不是单元格中,因此不能使用getCell()->getValue()获取图像,只能获取单元格的实际内容。查看DaveRandom关于访问实际图像的回答。我想从一行中检索图像。我有一张多行多列的工作表,上面有图像。所以,我想在行的基础上迭代它们。请告诉我。谢谢。根据你链接的博客文章是由与你用户名同名的人撰写的,你似乎链接到了自己的网站/博客。如果你这样做,你必须披露这是你的网站。如果你不透露这是你自己的网站,它被认为是垃圾邮件。见:、和。 class ExcelImport { /** * @var */ protected $excel; /** * @var */ protected $work_sheet; /** * @var array */ protected $excel_data = []; /** * ExcelImport constructor. * @param Request $request * @throws \PHPExcel_Exception * @throws \PHPExcel_Reader_Exception */ public function __construct(Request $request) { //Load file from request $this->excel = PHPExcel_IOFactory::load($request->file('file')); //Get active sheet $this->work_sheet = $this->excel->getActiveSheet(); } /** * @return array */ public function import() { //Iterate through drawing collection foreach ($this->work_sheet->getDrawingCollection() as $drawing) { //check if it is instance of drawing if ($drawing instanceof PHPExcel_Worksheet_Drawing) { //creating image name with extension $file_name = str_replace(' ', '_', $drawing->getName()).'.'.$drawing->getExtension(); //Get image contents from path and store them in Laravel storage Storage::put('public/'.$file_name, file_get_contents($drawing->getPath())); //create images array initially $this->excel_data[] = [ 'image' => $file_name ]; } } //Map other data present in work sheet return $this->rowData(); } /** * @return array */ private function rowData() { $i = 0; //Iterate through row by row foreach ($this->work_sheet->getRowIterator(2) as $row) { //iterate through cell by cell of row foreach ($row->getCellIterator() as $cell) { //In case of image data that would be null continue //We have already populated them in array if(is_null($cell->getValue())){continue;} //Map other excel data into the array $this->excel_data[$i]['name'] = $cell->getValue(); } $i++; } //Return final data array return $this->excel_data; } }