how to embed image(png) to Excel using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhbrayeun
    New Member
    • Apr 2010
    • 6

    how to embed image(png) to Excel using php

    I use below to output data to a excel file using PHP,
    Code:
    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\">";
    		echo "<head>";
    		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\">";
    		echo "</head>";
    		echo "<body>";
    however, I can't make it embed an image

    Code:
    <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"
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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.

    Comment

    • NettSIte
      New Member
      • Jun 2009
      • 7

      #3
      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.

      Comment

      • bhbrayeun
        New Member
        • Apr 2010
        • 6

        #4
        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?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          if Excel would support SVG, you could include that (because XHTML supports inline XML)

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            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.

            NettSIte,
            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

            Comment

            • bhbrayeun
              New Member
              • Apr 2010
              • 6

              #7
              Thank you guys for all your suggestions. I think I will try Spreadsheet_Exc el_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?

              Code:
              <?php
              require_once("Spreadsheet/Excel/Writer.php");
               
              	$workbook = new Spreadsheet_Excel_Writer();
              	$workbook->send("file.xls");
              	$worksheet =& $workbook->addWorksheet("worksheet");
              	$worksheet->setLandscape();    
              	$worksheet->setFooter("footer");
              	$worksheet->centerHorizontally(1);
              	$worksheet->setMargins(0.5); // dalam inch
              	$worksheet->setPaper(9); // A4
              	$worksheet->hideGridLines();
              	$worksheet->setColumn(0, 0, 20);
              	$worksheet->setColumn(0, 1, 2);
              	$worksheet->setColumn(0, 2, 20);
              	
              	$format1 =& $workbook->addFormat(array("Size" => 10, "Bold" => 1, "Align" => "center"));
              	$format1->setVAlign("vcenter");
              	$format2 =& $workbook->addFormat(array("Size" => 8, "Align" => "right"));
              	$format2->setVAlign("vcenter");
              	$format3 =& $workbook->addFormat(array("Size" => 8, "Align" => "left"));
              	$format3->setVAlign("vcenter");
              
              	$worksheet->write(1, 5, "write1", $format1);
              	$worksheet->write(2, 5, ("HAHA"), $format1);
              	$worksheet->write(3, 5, "Period : ", $format1);
              	$worksheet->write(5, 0, "mmm", $format3);
              	$workbook->close();
              	exit;
              ?>
              thanks.

              Comment

              • bhbrayeun
                New Member
                • Apr 2010
                • 6

                #8
                I found the reason it didn't show anything. The include_path in php.ini should be
                Code:
                include_path = ".;C:\xampp\php\pear\;"
                whereas mine was
                Code:
                include_path = ".;C:\program files\xampp\php\pear\;"
                Hope it saves somebody's time

                Thank you for all your helps guys.

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  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!

                  Comment

                  • bhbrayeun
                    New Member
                    • Apr 2010
                    • 6

                    #10
                    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 :
                    Code:
                    $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.

                    Comment

                    • code green
                      Recognized Expert Top Contributor
                      • Mar 2007
                      • 1726

                      #11
                      I must take a look at that PEAR class and find what it does exactly.

                      Comment

                      Working...