472,353 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 23913
8,658 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,658 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

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

Similar topics

by: Pawel | last post by:
Hello All I tried to embed Excel sheet into HTML page by following command: <iframe src="Tmp.XLS" width="100%" height="500"></iframe> and it...
by: bborden | last post by:
Novice Access programmer here. I would like to display an image using the Toolbox Image object by calling the images file name using: ...
by: Prasad More | last post by:
Hello, I am trying to write a text on Multi-page TIFF image using C# and .NET GDI+. I have written following code to do this. When I execute this...
by: Phil | last post by:
I've recently seen a couple of sites that have dynamically generated .png files on them and I'm wondering how to do it in c#, or if it's a server...
by: Senthil | last post by:
Hi all I'm new in ajax. How to upload a image using ajax..
by: Joe | last post by:
Hi I am able to embed Excel into a WebBrowser on my Dev machine but on a client machine an instance of Excel starts in it's own window outside of...
by: user | last post by:
Hi all... i am trying to print an image using GDI - but the only thing i get is a black rectangle. Does anybody know what is wrong, or how...
by: wingsganesh | last post by:
Hi Guys, I dont know how to send image in windows mobile 6. i captured image using CameraCaptureDialog class and displayed that image in...
by: shapper | last post by:
Hello, I am displaying an image on a few pages. The image size is 50 px height and 50 px width. In some pages I need the image to be 30x30 px in...
by: neovantage | last post by:
Hey geeks, i want to draw a jpeg image of pattern chess. I can draw solid color image using gd libraray and here is my code //$t_im having...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.