ImageNew toBase64 encoding issue with loss of quality in ColdFusion - coldfusion

I have been having problems with toBase64() for awhile. I am hoping someone can tell me why CF toBase64() seems to lost something i.e. in my example it reduces the quality of an image.
I have a solution (see last code example below), but I hate not understanding why and would love to solve this is CF.
If anyone would be so kind to run the code below, you would then see that after toBase64 conversion the image quality is bad. Nothing major, but it does not look as good after the encoding. If you have never noticed, then try it, you will see what I mean.
Does anyone know why, or how to solve this in CF?
<!--- EXAMPLE 1 --->
<!--- GET IMAGE - --->
<cfset image = ImageNew("test.png")>
<!--- BEFORE GOOD--->
<cfimage action="writeToBrowser" source="#image#" >
<cfset image = toBinary(toBase64(image)) />
<!--- AFTER --->
<cfimage action="writeToBrowser" source="#image#" >
<!--- Example 2 --->
<cfset image = ImageNew("test.png")>
<cfset FileWrite(expandPath('./converted.image'),toBinary(toBase64(image))) />
<!--- without any cfimage processing, the outputted file is a JPEG --->
My solution was to use a java add-on and everything seemed ok but for reasons I won't go into here not something I can do live.
image = createObject("java","it.sauronsoftware.base64.Base64").encode(image);
toBinary(image );
Sample image output of code above can be found here: http://i56.tinypic.com/29fwiq.png
First is before toBase64 second is after, you can see the image has lost a bit of quality after toBase64 function on the second output.
Update: As pointed out by Peter, the issue seems to be with the automatic output/conversion code within the ImageObject to provide the binary output for the toBase64 function to encode.
Update I have submitted this as a bug in CF 9.0.1, please vote for bug 3177303
https://bugbase.adobe.com/index.cfm?event=bug&id=3177303

use toBase64(imageGetBlob(myImg))
see: http://blog.dkferguson.com/index.cfm/2010/4/27/All-your-base64-are-not-equal

I see no one has mentioned the imageWriteBase64() function that has been in ColdFusion since version 8. I am not sure why.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-796b.html
I have been using it this week for the first time and it seems to be going great. I have not noticed any problems with quality loss.
<cfdirectory action="list" directory="#expandPath('./images')#" name="imageDir" type="file" />
<cfloop query="imageDir">
<cfset ext = listLast(imageDir.name, ".") />
<cfset name = imageDir.name />
<cfset imagePath = imageDir.directory & "/" & name />
<cfset imageFile = imageNew(imagePath) />
<cfset imageWriteBase64(imageFile,"#expandPath('./base64')#/#name#.txt",ext, true) />
</cfloop>

Related

ColdFusion FilegetMimeType giving inconsistent results

I need to use the FilegetMimeType() function, and I tested it in a program called demomimetype.cfm.
<cfoutput>
<cfset reploc = '#cookie.moxlog#'>
<cfset extrepoc = ExpandPath('#reploc#')>
<cfset typegif = FilegetMimeType('#extrepoc#/brightpink1.gif')>
typegif brightpink1.gif #typegif#<br>
</cfoutput>
This program returns the result that typegif is "image/gif" which is exactly correct. When I add the lines
<cfheader name = "Content-Disposition" value="attachment;#extrepoc#/logo106">
<cfcontent type="#typegif#" file="#extrepoc#/brightpink1.gif">
I get this result:
This is what I was hoping for, although demomimetype.cfm is not what I chose to open. However, this screen does open brightpink1.gif, so I'm okay with it.
Then I tried to integrate this into my actual system using this program, which results from a submit in an earlier program:
<cfoutput>
<cfset col = form.thecol>
<cfset colarray = getPageContext().getRequest().getParameterValues('xcol')>
<cfset origval = getPageContext().getRequest().getParameterValues('#col#')>
<cfset attname = origval[1]>
<cfset extreploc = ExpandPath("#cookie.moxlog#")>
<cfset typeatt = FilegetMimeType('#extreploc#/#attname#')>
</cfoutput>
The variable "attname" will be "brightpink1.gif. When I output typeatt I get "application/octet-stream", which is not correct. And when I add the lines
<cfheader name = "Content-Disposition" value="attachment;#extreploc#/#attname#">
<cfcontent type="#typeatt#" file="#extreploc#/#attname#">
I get the wrong result -- not surprising because the mimetype is wrong:
Obviously the demo is working correctly, and the real application is not. I have tried making the filename in the demo to be a variable, and the demo goes on working. I have tried making the filename in the application specific, and it still does not work.
I don't want to use the "strict" parameter because some of the files do not have extensions, even though they are of a specific type like .jpg.
There must be some reason why the code in the demo works and the nearly identical code in the application does not. But I cannot see it. Can anyone figure out what is going on here?

Using cfimage to display a file that doesn't have an extension

curious one this.
I'm working on a process that generates PDF files, combining data from various sources. The last piece of this process I need to complete is merging in image files.
This is actually fairly straightforward but the problem I have is the image files aren't stored with file extensions. Locally, I can change the filename, but in production this isn't an option.
So because a filename looks like : B71637CB-A49C-0653-EF813918736BDEB7
This will not work:
<cfimage action="writeTobrowser" source="#FilePath#>
Same with
<img src="#FilePath#">.
So, any ideas on how I can work around this? Here's the code in context:
<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
<cfdocumentsection>
<cfimage action="writeTobrowser" source="#FilePath#.jpg">
</cfdocumentsection>
</cfdocument>
So here's what ended up working:
<cfdocument format="PDF" name="report" filename="#fileToDownloadimage#" overwrite="yes">
<cfdocumentsection>
<cfset fileObject = fileReadBinary('#FilePath#') />
<cfset imageObject = imageNew(fileObject) />
<cfimage action="writeTobrowser" source="#imageObject#">
</cfdocumentsection>
</cfdocument>
Alex's answer got me down the right path so I'm perfectly happy to leave the kudos in place, cos I wasn't getting anywhere near this!
If you need to embed the images into the PDF document, try HTML's inline image capabilities:
<cfset fileLocation = "/path/to/images/B71637CB-A49C-0653-EF813918736BDEB7">
<cfset imageContent = fileReadBinary(fileLocation)>
<cfset imageContentAsBase64 = toBase64(imageContent)>
<cfoutput>
<img src="data:image/jpeg;base64, #imageContentAsBase64#" />
</cfoutput>
You can try creating a cfm page that outputs your content using cfcontent as in:
<cfcontent type="image/jpg" file="path/#fielpath#">
Then you would you include THAT cfm page as the source for your image as in
<img src="myFancyImageOuputer.cfm?image=#filepath#">
This should work but it may require some trial and error. :)
Ray has some additional tips here:
http://www.raymondcamden.com/2007/09/14/Serving-up-CFIMages-via-Image-Tags-and-a-NonCF-Friday-contest

How to change the user image using cfldap?

I'm able to get all the values that I want from cfldap.
But when I try to update the user image I don't know how to send the correct value for the binary image attribute.
I tried getting the image variable from the cffile upload
<cffile action="UPLOAD" filefield="file" destination="c:\inetpub\wwwroot\test" nameconflict="OVERWRITE" result="image" />
Also tried using cfimage with a static image -
<cfimage action="read" source="c:\inetpub\wwwroot\test\image.png" name="anotherImage">
Or even with
<cffile action="READBINARY" file="c:\inetpub\wwwroot\test\image.png" variable="BinaryImageContent">
But in any case, when I call
<cfldap action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#obj.image#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass">
The #results.dn# is the DN from the user that I get before (Everything ok on that)
I created the #obj.image# to be able to try all types of variables
Also tried these params:
<cfset obj.test1 = BinaryImageContent />
<cfdump var="#imageGetBlob(anotherImage)#" />
<cfdump var="#toString(obj.test1)#" />
By the way, the error that I get its
One or more of the required attributes may be missing or incorrect or
you do not have permissions to execute this operation on the server.
The problem is that I'm using the domain administrator account to update that
(THIS ERROR IS SOLVED - The network guys hadn't given me this permission... now I have it).
Now what I'm using is the following:
<cffile action="UPLOAD" filefield="file" destination="c:\inetpub\wwwroot\test" nameconflict="OVERWRITE" result="imagem" />
<cfset filename = "C:\inetpub\wwwroot\test\#imagem.serverFile#">
<cffile action="readbinary" file="#filename#" variable="img">
<cfset imgStr = BinaryEncode(img, "hex")>
<cfset imgStr2 = REReplace(imgStr, "..", "\\\0", "ALL")>
<cfldap
action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#imgStr2#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass"
>
but I get this binary code
Whats strange, is that before I had a binary code like -1-41 and now, nothing similar...
and when I try to show the pic
And this is one correct image....
EDIT: The original code sample below shows how it could work if ColdFusion wouldn't have a bug (or "very unfortunate design decision") in CFLDAP.
CFLDAP encodes the parameter values you pass to it before sending them to the server. This is nice because you don't have to worry about value encoding. But... it is also not helpful because it means you can't send encoded values yourself anymore, since CF invariably encodes them again.
Bottom line: As far as LDAP is concerned, encoding a file into a hex-string is correct, but CFLDAP mangles that string before sending it to the server. Combined with the fact that CFLDAP does not accept raw binary data this means that you can't use it to update binary attributes.
The comments contain a suggestion for a 3rd-party command line tool that can easily substitute CFLDAP for this task.
You need to send an encoded string to the server as the attribute value. The encoding scheme for binary data in LDAP queries has the form of attribute=\01\02\03\ab\af\cd.
Read your image into a byte array, encode that array into a hex string and prefix every encoded byte with a backslash.
<cffile action="readbinary" file="#filename#" variable="img">
<cfset imgStr = BinaryEncode(img, "hex")>
<cfset imgStr = REReplace(imgStr, "..", "\\\0", "ALL")>
<cfldap
action="modify"
DN="#results.dn#"
attributes="thumbnailPhoto=#imgStr#"
modifytype="replace"
server="myserver"
username="mydomain\myuser"
password="mypass"
>
Also don't forget what the documentation has to say about modifyType.

Excluding items from a list in coldfusion by type

Is there a way to exclude certain items by filetype in a list in Coldfusion?
Background: I just integrated a compression tool into an existing application and ran into the problem of the person's prior code would automatically grab the file from the upload destination on the server and push it to the Network Attached Storage. The aim now is to stop their NAS migration code from moving all files to the NAS, only those which are not PDF's. What I want to do is loop through their variable that stores the names of the files uploaded, and exclude the pdf's from the list then pass the list onto the NAS code, so all non pdf's are moved and all pdf's uploaded remain on the server. Working with their code is a challenge as no one commented or documented anything and I've been trying several approaches.
<cffile action="upload" destination= "c:\uploads\" result="myfiles" nameconflict="makeunique" >
<cfset fileSys = CreateObject('component','cfc.FileManagement')>
<cfif Len(get.realec_transactionid)>
<cfset internalOnly=1 >
</cfif>
**This line below is what I want to loop through and exclude file names
with pdf extensions **
<cfset uploadedfilenames='#myfiles.clientFile#' >
<CFSET a_insert_time = #TimeFormat(Now(), "HH:mm:ss")#>
<CFSET a_insert_date = #DateFormat(Now(), "mm-dd-yyyy")#>
**This line calls their method from another cfc that has all the file
migration methods.**
<cfset new_file_name = #fileSys.MoveFromUploads(uploadedfilenames)#>
**Once it moves the file to the NAS, it inserts the file info into the
DB table here**
<cfquery name="addFile" datasource="#request.dsn#">
INSERT INTO upload_many (title_id, fileDate, filetime, fileupload)
VALUES('#get.title_id#', '#dateTimeStamp#', '#a_insert_time#', '#new_file_name#')
</cfquery>
<cfelse>
<cffile action="upload" destination= #ExpandPath("./uploaded_files/zip.txt")# nameconflict="overwrite" >
</cfif>
Update 6/18
Trying the recommended code helps with the issue of sorting out filetypes when tested outside of the application, but anytime its integrated into the application to operate on the variable uploadedfilenames the rest of the application fails and the multi-file upload module just throws a status 500 error and no errors are reported in the CF logs. I've found that simply trying to run a cfloop on another variable not related to anything in the code still causes it to error.
As per my understanding, you want to filter-out file names with a specific file type/extension (ex: pdf) from the main list uploadedfilenames. This is one of the easiest ways:
<cfset lFileNames = "C:\myfiles\proj\icon-img-12.png,C:\myfiles\proj\sample-file.ppt,C:\myfiles\proj\fin-doc1.docx,C:\myfiles\proj\fin-doc2.pdf,C:\myfiles\proj\invoice-temp.docx,C:\myfiles\proj\invoice-final.pdf" />
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#lFileNames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,"#fileItem#") />
</cfif>
</cfloop>
Using only List Function provided by ColdFusion this is easily done, you can test and try the code here. I would recommend you to wrap this code around a function for easy handling. Another way to do it would be to use some complex regular expression on the list (if you're looking for a more general solution, outside the context of ColdFusion).
Now, applying the solution to your problem:
<cfset uploadedfilenames='#myfiles.clientFile#' >
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#uploadedfilenames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,fileItem) />
</cfif>
</cfloop>
<cfset uploadedfilenames = lResultList />
<!--- rest of your code continues --->
The result list lResultList is copied to the original variable uploadedfilenames.
I hope I'm not misunderstanding the question, but why don't you just wrap all of that in an if-statement that reads the full file name? Whether the files are coming one by one or through a delimited list, it should be easy to work around.
<cfif !listContains(ListName, '.pdf')>
OR
<cfif FileName does not contain '.pdf'>
then
all the code you posted

How can I strip this URL of everything before "http://"?

I'm doing some web scraping with ColdFusion and mostly everything is working fine. The only other issues I'm getting is that some URL's come through with text behind them that is now causing errors.
Not sure what's causing it, but it's probably my regex. Anyhow, there's a distinct pattern where text appears before the "http://". I'd like to simply remove everything before it.
Any chance you could help?
Take this string for example:
"I'M OBSESSED WITH MY BEAUTIFUL FRIEND" src="http://scs.viceland.com/feed/images/uk_970014338_300.jpg
I'd much appreciate your help as regex isn't something I've managed to make time for - hopefully I will some day!
Thanks.
EDIT:
Hi,
I thought it might be helpful to post my entire function, since it could be my initial REGEX that is causing the issue. Basically, the funcion takes one argument. In this case, it's the contents of a HTML file (via CFHTTP).
In some cases, every URL looks and works fine. If I try digg.com for example it works...but it dies on something like youtube.com. I guess this would be down to their specific HTML formatting. Either way, all I ever need is the value of the SRC attribute on image tags.
Here's what I have so far:
<cffunction name="extractImages" returntype="array" output="false" access="public" displayname="extractImages">
<cfargument name="fileContent" type="string" />
<cfset var local = {} />
<cfset local.images = [] />
<cfset local.imagePaths = [] />
<cfset local.temp = [] />
<cfset local.images = reMatchNoCase("<img([^>]*[^/]?)>", arguments.fileContent) />
<cfloop array="#local.images#" index="local.i">
<cfset local.temp = reMatchNoCase("(""|')(.*)(gif|jpg|jpeg|png)", local.i) />
<cfset local.path = local.temp />
<cfif not arrayIsEmpty(local.path)>
<cfset local.path = trim(replace(local.temp[1],"""","","all")) />
<cfset arrayAppend(local.imagePaths, local.path) />
</cfif>
<cfif isValid("url", local.path)>
<cftry>
<cfif fileExists(local.path)>
<cfset arrayAppend(local.imagePaths, local.path) />
</cfif>
<cfcatch type="any">
<cfset application.messagesObject.addMessage("error","We were not able to obtain all available images on this page.") />
</cfcatch>
</cftry>
</cfif>
</cfloop>
<cfset local.imagePaths = application.udfObject.removeArrayDuplicates(local.imagePaths) />
<cfreturn local.imagePaths />
</cffunction>
This function WORKS. However, on some sites, not so. It looks a bit over the top but much of it is just certain safeguards to make sure I get valid image paths.
Hope you can help.
Many thanks again.
Michael
Take a look at ReFind() or REFindNoCase() - http://cfquickdocs.com/cf9/#refindnocase
Here is a regex that will work.
<cfset string = 'IM OBSESSED WITH MY BEAUTIFUL FRIEND" src="http://scs.viceland.com/feed/images/uk_970014338_300.jpg' />
<cfdump var="#refindNoCase('https?://[-\w.]+(:\d+)?(/([\w/_.]*)?)?',string, 1, true)#">
You will see a structure returned with a POS and LEN keys. Use the first element in the POS array to see where the match starts, and the first element in the LEN array to see how long it is. You can then use these values in the Mid() function to grab just that matching URL.
I'm not familiar with ColdFusion, but it seems to me that you just need a regex that looks for http://, then any number of characters, then the end of the string.