Extracting pictures/images within an Excel file (xls) using PHP

房东的猫 提交于 2020-02-21 12:33:23

问题


I have a spreadsheet that I would like to import using PHP. I can import the cell data using PHPExcel, but can't figure out how to use images from within the spreadsheet.

Is there a way of doing this and then using the images within PHP to save to the server etc?

Many thanks for the help! :)


Update:

@mark-baker - thank you so much for your help with this!

I have used the code below on a test XLS file with one JPG:

$objPHPExcel = PHPExcel_IOFactory::load("SH.xls");

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();
    }
}

I think I can then output JPEG headers and the contents of $imageContents to show the image.

How would I get the actual name of the image in the spreadsheet though eg "Picture1"? Is this possible with PHPExcel_Worksheet_MemoryDrawing?

I can't thank you enough!


回答1:


Somebody, I don't know if it's yourself, has asked a similar question on the PHPExcel board... that I haven't got round to answering yet.

$objPHPExcel->getActiveSheet()->getDrawingCollection()

will return an ArrayObject of all the image objects in the active worksheet.

These objects will be either PHPExcel_Worksheet_Drawing or PHPExcel_Worksheet_MemoryDrawing objects: you can identify which using is_a(). You can then use the methods appropriate to that class (as described in the API) either to read the image data from file (for PHPExcel_Worksheet_Drawing objects) or directly from the PHPExcel_Worksheet_MemoryDrawing object itself. The getName() and getDescription() methods can be used to retrieve the relevant values fro the image object.

Note that it's also possible to have image objects associated with print headers:

$objPHPExcel->getActiveSheet()->getHeaderFooter()->getImages()

can be used to retrieve images from the header/footer. This is an array of PHPExcel_Worksheet_HeaderFooterDrawing objects. All the PHPExcel_Worksheet_Drawing methods can be used to extract the image file from these objects.

EDIT

Based on your code in the modified question:

$drawing->getName();

should give you what you need




回答2:


You can do the following :

$im = {excel data} <br>
header("Content-type: image/jpeg"); // or whatever <Br>
$image = imagejpeg($im, NULL, 100);



回答3:


I have seen the example of Reading Images from a worksheet, It is working fine but I customized the same example for my project requirement like bellow.

require 'vendor/autoload.php';

$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load("./excel.xlsx");

$worksheet = $spreadsheet->getActiveSheet();
$worksheetArray = $worksheet->toArray();
array_shift($worksheetArray);

echo '<table style="width:100%"  border="1">';
echo '<tr align="center">';
echo '<td>Sno</td>';
echo '<td>Name</td>';
echo '<td>Image</td>';
echo '</tr>';

foreach ($worksheetArray as $key => $value) {

    $worksheet = $spreadsheet->getActiveSheet();
    $drawing = $worksheet->getDrawingCollection()[$key];

    $zipReader = fopen($drawing->getPath(), 'r');
    $imageContents = '';
    while (!feof($zipReader)) {
        $imageContents .= fread($zipReader, 1024);
    }
    fclose($zipReader);
    $extension = $drawing->getExtension();

    echo '<tr align="center">';
    echo '<td>' . $value[0] . '</td>';
    echo '<td>' . $value[1] . '</td>';
    echo '<td><img  height="150px" width="150px"   src="data:image/jpeg;base64,' . base64_encode($imageContents) . '"/></td>';
    echo '</tr>';
}

OutPut:

I have created the GitHub demo project "phpspreadsheet-Reading-Images-from-an-Excel-File" for future reference.

Github Link: https://github.com/rajaramtt/phpspreadsheet-Reading-Images-from-an-Excel-File



来源:https://stackoverflow.com/questions/6114024/extracting-pictures-images-within-an-excel-file-xls-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!