By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,319 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,319 IT Pros & Developers. It's quick & easy.

read images/graphics in spreadsheet

P: n/a
hi

I have to read images from spreadsheet, is it possible to do with php?

Jul 14 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
C.
On 14 Jul, 16:00, wst...@hotmail.com wrote:
hi

I have to read images from spreadsheet, is it possible to do with php?

maybe. It kind of depends what format the spreadsheets are in.

C.

Jul 14 '07 #2

P: n/a
This class will extract the images from an Excel spreadsheet. It uses
the SaveAs method to convert it to HTML and image files:

<?

// Convert an Excel file to HTML using
// COM.
//
// $conv = new ExcelToHtmlConverter;
// $result = $conv->convert('file.xls', '.');
//
// The output directory will contain files.htm
// and file_files. The supporting files directory
// will contain any images in the spreadsheet.

class ExceltoHtmlConverter
{
// Export an Excel file to HTML

function convert($file, $destDir)
{
$excel = new COM('excel.sheet');
$wb = $excel->Application->Workbooks->Open($file);

$dir = dirname($file);
$bn = basename($file);
$dot = strrpos($bn, '.');
$rn = $dot === false ? $bn : substr($bn, 0, $dot);

$htmlPath = "$destDir/$rn.htm";
$this->removePath($htmlPath);
$filesPath = "$destDir/${rn}_files";
$this->removePath($filesPath);

$excel->Application->DisplayAlerts = false;
$res = $wb->SaveAs($htmlPath, 44);
$wb->Close();
unset($wb);
unset($excel);
if (!$res) {
throw new Exception("Failed to export $file to " .
"HTML.");
}
$images = $this->findImages($filesPath);
return array('html' =$htmlPath,
'files' =$filesPath, 'images' =$images);
}

// Find image files in the output directory

function findImages($path)
{
$images = array();
if (is_file($path) || is_link($path)) {
$ext = strval(substr(strrchr($path, '.'), 1));
$exts = array('png', 'gif', 'jpg', 'jpeg', 'jpe',
'tif', 'tiff', 'bmp');
if (in_array($ext, $exts)) {
$images[] = $path;
}
} else {
if (($dir = opendir($path)) === false) {
throw new Exception("Failed to open $path");
}
$e = null;
while (($file = readdir($dir)) !== false) {
if ($file == '.' || $file == '..') continue;
try {
$subs = $this->findImages("$path/$file");
$images = array_merge($images, $subs);
} catch (Exception $e) {
break;
}
}
closedir($dir);
if (!is_null($e)) {
throw $e;
}
}
return $images;
}

// Remove a file or directory tree

function removePath($path)
{
if (is_file($path) || is_link($path)) {
if (!unlink($path)) {
throw new Exception("Failed to unlink $path");
}
} elseif (file_exists($path)) {
if (($dir = opendir($path)) === false) {
throw new Exception("Failed to open $path");
}
$e = null;
while (($file = readdir($dir)) !== false) {
if ($file == '.' || $file == '..') continue;
try {
$this->removePath("$path/$file");
} catch (Exception $e) {
break;
}
}
closedir($dir);
if (!is_null($e)) {
throw $e;
}
if (!rmdir($path)) {
throw new Exception("Failed to remove $path");
}
}
}

// Test using file.xls in the current directory

function test()
{
$dir = dirname(__FILE__);
$path = "$dir/file.xls";
$conv = new ExcelToHtmlConverter;
$result = $conv->convert($path, $dir);

echo "Converted Excel file $path to HTML.\n";
echo "HTML file: {$result['html']}\n";
echo "Files directory: {$result['files']}\n";
echo "Images: " . join(', ', $result['images']);
}
}

error_reporting(E_ALL);
ExcelToHtmlConverter::test();

?>

Other options for Excel:

o Export to HTML using File -Save As from MS Excel.

o Export from OpenOffice.

o AJP Graphics Exporter - Add-in that exports images from Excel.
<http://www.andypope.info/vba/gex.htm>

o XL2GIF Macro - Code that exports images by accessing the Shapes
directly and using the Chart object's Export method.
<http://www.mvps.org/dmcritchie/excel/xl2gif.htm>

o Copy the Shape objects to the clipboard and manipulate it from
there.
<http://groups.google.com/group/micro...l.programming/
browse_frm/thread/de2b1b699fb89c01/fd5f054fa9d6de#fd5f054fa9d6de>
<http://groups.google.com/group/micro...xcel.charting/
browse_thread/thread/911fa46e1a10952e/
6bbac0228f383bf8#6bbac0228f383bf8>

o PHPExcel - PHP Excel 2007 classes.
<http://www.codeplex.com/PHPExcel>

Regards,

John Peters

On Jul 14, 10:00 am, wst...@hotmail.com wrote:
hi

I have to read images from spreadsheet, is it possible to do with php?

Jul 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.