Save images from xls to HD

Topics: Developer Forum, User Forum
Sep 6, 2008 at 9:27 AM

i have to read a large xls file with some images in it. Than i have to save the images somewhere.

It only seems to work with *.xlx files, is that right? Or is it also posible with Reader5? If not is there a way to convert xls to xlsx?

I tried somithing like

$file = "myexcel.xlsx"

$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load("data/uploads/".$file);

I know, I can do this (whatever?):


or get the number of images like that:


But i don't have a clue how to save each of the images to disk/database or something.

Maybe someone can give me a detailed hint please? A small example would be great.


Sep 6, 2008 at 1:40 PM
Unfortunately you're right... This is not currently possible using the Excel5 reader.

Can you please send a sample file of what you are trying to read to erik at phpexcel dot net so we have a reference.
Sep 7, 2008 at 5:12 AM
A work item has been created for this. Discussion can continue here:

Sep 8, 2008 at 7:00 AM
It sounds like it is possible with reader2007?

If that is true, is der a small example for xlsx files?

Sep 8, 2008 at 1:55 PM
Here is the method for reading images from Excel spreadsheets. If you rather want everything saved as PNG you can modify the script so it uses imagepng for all cases .gif, .jpeg, .png

$reader = new PHPExcel_Reader_Excel2007();
$PHPExcel = $reader->load('test.xlsx');
$worksheet = $PHPExcel->getActiveSheet();

// extract images from worksheet and save files:  0.jpeg, 1.jpeg, 2.png, ...
foreach ($worksheet->getDrawingCollection() as $i => $drawing) {
    $filename = $drawing->getPath();
    $imagesize = getimagesize($filename);

    switch ($imagesize[2]) {
    case 1:
        $image = imagecreatefromgif($filename);
        imagegif($image, "$i.gif");
    case 2:
        $image = imagecreatefromjpeg($filename);
        imagejpeg($image, "$i.jpeg");
    case 3:
        $image = imagecreatefrompng($filename);
        imagepng($image, "$i.png");
        continue 2;
Sep 23, 2008 at 6:13 AM
Hello again,

I tried to use this code but there is this warning

Warning: ZipArchive::getFromName() [function.ZipArchive-getFromName]: Invalid or unitialized Zip object in D:\wamp\bin\php\php5.2.5\PEAR\PHPExcel\Shared\ZipStreamWrapper.php on line 108

The question more important is, is it posible to get the linenumber of each image? Imagine an Excelfile with pictures, but not in every row. So I need the number of the row, to say to wich dataset the image belongs.

Thanks again :)
Sep 23, 2008 at 12:29 PM
Q1: I don't know what is causing the warning message. Can you upload the file somewhere (after removing sensitive data)?

Q2: Here is an example how to get coordinates all the images:

foreach ($worksheet->getDrawingCollection() as $i => $drawing) {
    $string = $drawing->getCoordinates();
    // e.g. string(2) "E9"
    $coordinate = PHPExcel_Cell::coordinateFromString($string);
    // e.g. array(2) { [0]=>  string(1) "E" [1]=>  string(1) "9" }
    $col = $coordinate[0]; // e.g. string(1) "E"
    $row = $coordinate[1]; // e.g. string(1) "9"
Sep 23, 2008 at 2:57 PM
Thx for your help, i got it working for Excel 2007.

The Problem with Q1 wars in ZipStreamWraper.php

As you can see, this is a function from your class. The thing I did, wars to comment "$url = @parse_url($path);" because it made $url['host']='data' from the path "zip://data/uploads/test1.xlsx#xl/media/image1.png".
One word is not a valid host I think ;) The Fix URL part is doing a fine job creating the real host. So it's working now :) I hope to see the 2005 version soon.

 public function stream_open($path, $mode, $options, &$opened_path) {
        // Check for mode
        if (substr($mode, 0, 1) != 'r') {
            throw new Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
        // Parse URL
        // $url = @parse_url($path);
        // Fix URL
        if (!is_array($url)) {
            $url['host'] = substr($path, strlen('zip://'));
            $url['path'] = '';
        if (strpos($url['host'], '#') !== false) {        
           if (!isset($url['fragment'])) {
                $url['fragment']    = substr($url['host'], strpos($url['host'], '#') + 1) . $url['path'];
                $url['host']        = substr($url['host'], 0, strpos($url['host'], '#'));

Sep 23, 2008 at 7:54 PM
Edited Sep 23, 2008 at 7:55 PM
Thank you for the report. Problem has been copied to the issue tracker for investigation:
Jan 1, 2009 at 10:00 PM
Fixed per work item #7538

Jan 7, 2009 at 12:57 PM
Thank you for the help. I tried to use the new version. It seems to work with the samples but it doesn't work with my *.xls files. I found some images in my file, which caused a stop in $PHPExcel = $reader->load('test.xls'); without any messages.
If I remove them, the sample script works. I will provide a sample File.

What do I have to do to save the imagefiles to disk.


$filename = $drawing->getPath();
$imagesize = getimagesize($filename);

switch ($imagesize[2]) {
    case 1:
        $image = imagecreatefromgif($filename);
        imagegif($image, "$i.gif");


doesn't seem to work.
Jan 7, 2009 at 6:35 PM
Yes, right now Excel5 reader just loads images as resources in memory. There is room for improvement.

Here is the loop for extracting all images from the workbook object. It should be working for both reading xls and xlsx files. You can just change the string 'images/' to wherever you want the images saved on the servers hard disk.

foreach ($worksheet->getDrawingCollection() as $drawing) {
    // $drawing->getCoordinates();

    // we have an ordinary drawing (case xlsx)
    if ($drawing instanceof PHPExcel_Worksheet_Drawing) {

        $filename = $drawing->getPath();

        copy($filename, 'images/' . $drawing->getIndexedFilename());

    // we have a memory drawing (case xls)
    } else if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {

        $image = $drawing->getImageResource();

        // save image to disk
        $renderingFunction = $drawing->getRenderingFunction();
        switch ($renderingFunction) {
        case PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG:
            imagejpeg($image, 'images/' . $drawing->getIndexedFilename());
        case PHPExcel_Worksheet_MemoryDrawing::RENDERING_GIF:
            imagegif($image, 'images/' . $drawing->getIndexedFilename());
        case PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG:
        case PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT:
            imagepng($image, 'images/' . $drawing->getIndexedFilename());

Please send me your sample file that is not working so I can investigate the bug.

Jan 8, 2009 at 5:18 PM
Fixed per work item #8860

Feb 6, 2009 at 12:01 PM
Edited Feb 6, 2009 at 12:09 PM
Hi, lads!
At first - great thanks for your really GREAT work!

I've got the same bug while reading excel5 fle - it can't recognize autofigures, created by excel with error:

Fatal error: Call to undefined method PHPExcel_Shared_Escher_DgContainer_SpgrContainer::setSpgrContainer() in Q:\home\\www\reader\Classes\PHPExcel\Reader\Excel5\Escher.php on line 473

The same error is for groups of images, even if they're jpegs, not autofigures.

To reproduce: just create excel5 file, insert two jpgs there, and group them.

I know - class for reading with excel 5 file format is not developed by you, but still I'm hoping for solution...:)
Any ideas?

Thanks in advance!
Feb 6, 2009 at 2:14 PM
Will check this and try to find solution!

Feb 9, 2009 at 3:11 AM
Work item created to deal with fatal error:

Please note that PHPExcel does unfortunately not support group shapes. At this stage we are not able to read group shapes with any of readers PHPExcel_Reader_Excel2007 and PHPExcel_Reader_Excel5.
Mar 11, 2009 at 12:57 PM
Hi Koyama,

Your article is a great one. Was able to read all the images in an excel sheet.
I have a question.

Can we get the cell from where the image is being copied.
The excel sheet has a key-value in Column A and a corresponding Image in column B

My requirement is to save the images with the name as the value from Column A.

thanks a lot in advance
Mar 11, 2009 at 6:14 PM
@santosh18: To get the coordinates of the cell where the image is anchored, try to look at the 7th post in this thread. It is available through


Oct 5, 2012 at 4:21 PM
Edited Oct 5, 2012 at 4:21 PM


Please can someone help me? I can not read pictures XLSX ... Below is my code and the result.



$reader = new PHPExcel_Reader_Excel2007();
		$PHPExcel = $reader->load('test.xlsx');
		$worksheet = $PHPExcel->getActiveSheet();




ArrayObject Object
    [storage:ArrayObject:private] => Array



Whats is Wrong?