1

I am currently on CF version 9.0.1 and I'm having trouble getting the SpreadsheetAddImage function to properly insert an image into the spreadsheet that I am generating. I'm not getting any error message, its just that the image does not display in the spreadsheet. I've also ruled out any issues with finding the image or returning it (tested by using cfimage writetobrowser attribute).

I also stripped out all of my data and formatting from the spreadsheet to rule out anything overwriting that cell's contents. Just to see if I could get a blank spreadsheet with an image.

See sample code below.

<cfset sObj = SpreadsheetNew()>
<!--- <cfimage source="pathtomyimage.jpg" name="image"> --->
<cfset image_var = ImageRead("pathtomyimage.png")>

<cfset SpreadsheetAddRow(sObj, "")>
<cfset SpreadsheetAddImage(sObj,image_var,"png","1,1,1,1")>

...

<cffile action="readbinary" file="#dest_loc#" variable="export_file">
<cffile action="delete" file="#dest_loc#">

<cfheader name="Content-Disposition" value="inline; filename=#file_name#.xls">
<cfcontent type="application/vnd.msexcel" variable="#toBinary(export_file)#">

I appreciate any feedback. Thanks.

Leigh
  • 28,765
  • 10
  • 55
  • 103

1 Answers1

4

I ran your code and found the image did not appear when the anchor dimensions were too small. (I also used SpreadSheetReadBinary instead of writing the spreadsheet to disk, but that should not make any difference). Try this example from the documentation. If the image is visible, that suggests a problem with the dimensions. To fix it you could either increase the anchor dimensions or use image functions to reduce the size of your image.

<cfchart format="png" name="image_var"> 
    <cfchartseries type="line"> 
        <cfchartdata item="Point1" value="-50"> 
        <cfchartdata item="Point2" value="-25"> 
        <cfchartdata item="Point3" value="1"> 
    </cfchartseries> 
</cfchart> 

<cfset sObj = SpreadsheetNew()>
<cfset SpreadsheetAddRow(sObj, "")>
<cfset SpreadsheetAddImage(sObj,image_var,"png","1,1,7,6")>
<cfheader name="Content-Disposition" value="inline; filename=testFile.xls">
<cfcontent type="application/vnd.msexcel" variable="#SpreadSheetReadBinary(sObj)#">
Leigh
  • 28,765
  • 10
  • 55
  • 103
  • That was it. Thank you! Follow up question though: I'm getting the image but the colors are rendering differently then they should. Could this be becuase the dimensions are still not enough? – user1489298 Jul 19 '13 at 13:01
  • Doesn't seem to be caused by the dimensions. Very strange. – user1489298 Jul 19 '13 at 13:15
  • Looks like it was something to do with the image file format. I had another copy of the same image saved as a jpg file instead of a png. The jpg image rendered correctly. Not sure why that is. Thanks again! – user1489298 Jul 19 '13 at 13:25
  • JPEGs and PNGs store colors differently and are interpreted differently when decoded, especially given JPEGs lossy compression. To see an example of this save the same base file in PNG, JPEG, and GIF, then create an HTML page that shows them side by side in IE. All will appear visibly different in tonality. – Nicholas Jul 19 '13 at 17:01