469,271 Members | 997 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

how to embed image(png) to Excel using php

I use below to output data to a excel file using PHP,
Expand|Select|Wrap|Line Numbers
  1. echo "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\">";
  2.         echo "<head>";
  3.         echo "<meta http-equiv=Content-Type content=\"text/html; charset=us-ascii\"> <meta name=ProgId content=Excel.Sheet><meta name=Generator content=\"Microsoft Excel 11\">";
  4.         echo "</head>";
  5.         echo "<body>";
however, I can't make it embed an image

Expand|Select|Wrap|Line Numbers
  1. <img src="data:image/png;base64,'. $_POST['DATA_BIN'] .'">
It shows blank box with text : "the linked image cannot be displayed. the file may have been moved, renamed, or deleted. verify that the link points to the correct file and location"
Apr 12 '10 #1
10 22985
8,651 Expert Mod 8TB
the HTML img element does not contain image data. it holds a reference to the image, as you can clearly see in the error message.
Apr 12 '10 #2
Are you sure you are creating an Excel spreadsheet? Looks more like you are creating an HTML file similar to one created by Excel.

To create an Excell file, you need a whole different approach.

I often output a CSV file, which Excel can open, but I have never tried to include images. There is also a PEAR thing called something like Excel Spreadsheet Writer which is pretty powerful - it may be what you need.
Apr 13 '10 #3
Yes it does create an xls file, but as I said before, it cannot embed images from base64 encoded png files. I tried to use different approach : change the base64 encoded png file to a png file and save it in the server then I use <img> tag to display the images. But my problem now, when I delete the images from server, the image in the xls is also disappear because it's only a link I guess. Any suggestions?
Apr 13 '10 #4
8,651 Expert Mod 8TB
if Excel would support SVG, you could include that (because XHTML supports inline XML)
Apr 13 '10 #5
code green
1,726 Expert 1GB
Mind if I join in? I asked the same question last year http://bytes.com/topic/php/answers/8...ls-excel-sheet

Sorry I never did implement the suggestions.

XLS is written in XHTML. It was possible to view the 'code behind' in Excel prior to Office 2007.
You can write simple HTML code as a string, write the string to a file, then save file as .xls and the file becomes a XLS spreadsheet with formatting, color etc.

But linking to CSS and images is only possible if full URL is supplied
Apr 13 '10 #6
Thank you guys for all your suggestions. I think I will try Spreadsheet_Excel_Writer from pear because I really need the images to be embedded in my xls file. However, when I tried it, it showed blank page. Does it need some special configuration?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. require_once("Spreadsheet/Excel/Writer.php");
  4.     $workbook = new Spreadsheet_Excel_Writer();
  5.     $workbook->send("file.xls");
  6.     $worksheet =& $workbook->addWorksheet("worksheet");
  7.     $worksheet->setLandscape();    
  8.     $worksheet->setFooter("footer");
  9.     $worksheet->centerHorizontally(1);
  10.     $worksheet->setMargins(0.5); // dalam inch
  11.     $worksheet->setPaper(9); // A4
  12.     $worksheet->hideGridLines();
  13.     $worksheet->setColumn(0, 0, 20);
  14.     $worksheet->setColumn(0, 1, 2);
  15.     $worksheet->setColumn(0, 2, 20);
  17.     $format1 =& $workbook->addFormat(array("Size" => 10, "Bold" => 1, "Align" => "center"));
  18.     $format1->setVAlign("vcenter");
  19.     $format2 =& $workbook->addFormat(array("Size" => 8, "Align" => "right"));
  20.     $format2->setVAlign("vcenter");
  21.     $format3 =& $workbook->addFormat(array("Size" => 8, "Align" => "left"));
  22.     $format3->setVAlign("vcenter");
  24.     $worksheet->write(1, 5, "write1", $format1);
  25.     $worksheet->write(2, 5, ("HAHA"), $format1);
  26.     $worksheet->write(3, 5, "Period : ", $format1);
  27.     $worksheet->write(5, 0, "mmm", $format3);
  28.     $workbook->close();
  29.     exit;
  30. ?>
Apr 13 '10 #7
I found the reason it didn't show anything. The include_path in php.ini should be
Expand|Select|Wrap|Line Numbers
  1. include_path = ".;C:\xampp\php\pear\;"
whereas mine was
Expand|Select|Wrap|Line Numbers
  1. include_path = ".;C:\program files\xampp\php\pear\;"
Hope it saves somebody's time

Thank you for all your helps guys.
Apr 13 '10 #8
code green
1,726 Expert 1GB
But the image is not actually 'embedded' in the XLS file, as is possible in a PDF file.
<src img is just linking to a live file. If this spreadsheet is sent as an attachment on an email or moved to another server this link is lost.
Unless a full www. address is given, firewalls permitting.

So maybe the image needs saving locally with the xls file, as when you save a web page.
Then whenever it is moved, one must ensure the pictures go with it!
Apr 13 '10 #9
I finally did it using pear. Using this library, the xls file is no longer an XML file, it's a binary file and I think it is embedded because I didn't use <img src...> anymore. Instead, I use :
Expand|Select|Wrap|Line Numbers
  1. $worksheet->insertBitmap
However, the image files have to be a bmp format so we have to convert the format first. I use JPEXS' bmp library to convert formats.
Apr 13 '10 #10
code green
1,726 Expert 1GB
I must take a look at that PEAR class and find what it does exactly.
Apr 14 '10 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Pawel | last post: by
1 post views Thread by Prasad More | last post: by
4 posts views Thread by Phil | last post: by
3 posts views Thread by Senthil | last post: by
2 posts views Thread by Joe | last post: by
2 posts views Thread by user | last post: by
11 posts views Thread by shapper | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.