PHP Get image from Excel file

Hi, I have an Excel spreadsheet (xlsx) that contain a graphic chart/image.  Is there a way to capture or get that image in PHP.  If so, how?
lgduongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
You would probably need to write a VBA macro to do the export using the export method of the Chart object and then invoke this from PHP through some Office COM intermediary library - sort of like trying to move a piano with a bicycle.

It might be a better idea to look at why you would want to do this and try to find another way.
0
selvolCommented:
Sure, it can be done and is pretty simple, Depends on your understanding of PHP.

I am short on time. And may return later to possibly give a full PHP code.
But here is my general solution in short....
Using PHP {
First rename the xlsx file to

YOURNAME.zip
Then extract the newly named file to a new folder
then
file_get_contents
of the image located in the
YOUR_NEW_FOLDER\xl\media\IMAGE_NAME.EXTENSION
}
AND YOU should have your image.......

Selvol
0
lgduongAuthor Commented:
Selvol - when you get a chance, I would like to get the full PHP code if possible.  I'm unfamiliar with the file_get_contents function.  Thanks.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

selvolCommented:
Do you have an  example of the xlsx.  It would help due to the different version of Xlsx and what not.

I'll be back in an hour or so...

Selvol
0
lgduongAuthor Commented:
Selvol,

Please see attached.
sample.xlsx
0
Julian HansenCommented:
@Selvol,

That will work for embedded images but charts are rendered on the fly by Excel and would first need to be rendered out to an image.

They are not inherently created as images - which makes this solution not really an option.
0
lgduongAuthor Commented:
Selvol,

That is what I was afraid.  Just in case I might need it later on.  How would I have to extract if I did render the charts as jpeg.  See attached.
sample.xlsx
0
Eddie ShipmanAll-around developerCommented:
I would checkout the PHPExcel library as it has a lot of functionality that may be useful to you.
http://phpexcel.codeplex.com/

"Extracting images with Excel file (xlsx) using PHP": http://phpexcel.codeplex.com/workitem/18189

Please be aware, though, that this library is fairly memory intensive and could, depending upon the size of your workbook, cause you to bump your PHP memory limit.
0
Julian HansenCommented:
(note the name)

Building on Sevol's idea here is how you extract files from a .xlsx and check for jpg versions
<?php
//
// WHERE DO YOU WANT TO SAVE THE IMAGES TO
//
define('INCOMINGFOLDER','images/');

//
// FILENAME TO PROCESS - THIS COULD BE PASSED IN AS A URL PARAMETER OR COMMAND LINE PARAMETER
//

$filename = "sample.xlsx";
// 
// OPEN ZIP FILE
//
$zip = zip_open($filename);
if ($zip) {

//
// LOOP THROUGH ENTRIES
//
    while ($zip_entry = zip_read($zip)) {
    
        //
        // GET FILENAME 
        //
        $zipentry = zip_entry_name($zip_entry);
        
        //
        // BREAK INTO PARTS TO GET NAME AND EXTENSION
        //
        $pi = pathinfo($zipentry);
        
        //
        // IF .JPG FOUND THEN ...
        //
        if ($pi['extension'] == 'jpg') {
            // 
            // WRITE THE FILE OUT. BUILD THE FILENAME FROM SCRATCH BECAUSE
            // THE FILENAME FROM THE ZIP MIGHT CONTAIN PATH INFORMATION
            echo "Found one: " . $zipentry . "<br/>";
            $path = INCOMINGFOLDER . $pi['filename'] . '.' . $pi['extension']; 
            if ($fd = @fopen($path, "w+")) {
                fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
                fclose($fd);
            }
            else {
                echo "Failed to open $path<br/>";
            }
        }
    }
}
echo "Done";
?>

Open in new window

Julian
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
selvolCommented:
Thank you, julianH.

I was a little busy.

Selvol
0
Julian HansenCommented:
@selvol - no problem - I had some code that I use for processing zip's so just adapted and posted - it was your idea though.
0
RobOwner (Aidellio)Commented:
How complex are the charts?  The ones in your sample don't look too complex so you could try extracting the chart data from the spreasheet and render the chart using PHP.  For example using this chart library (and there are many more)
http://pchart.sourceforge.net/
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.