How to compare two images in ColdFusion

615 views Asked by At

I am trying to compare images and find if they are same or not. Images can have same name but the actual image might be different. The code that I have so far.

<cfset dirToReadFrom = #ExpandPath( '../properties-feed/unzipped/' )# />

<cfdirectory
action="list"
directory="#dirToReadFrom#"
listinfo="name"
name="qFile"
sort="asc"
filter="*.jpg"
/>

<cfset images = ArrayNew(1)>
<cfoutput query="qFile">
    <cfset ArrayAppend(images, #qFile.name#)>
</cfoutput>

<cfset dirToCreate = #ExpandPath( './assets/images/resized/original/' )# />
<cfif not DirectoryExists(dirToCreate)>
    <cfdirectory action = "create" directory = "#dirToCreate#" />
    <cfoutput><p>Your directory has been created.</p></cfoutput>
</cfif>
<cfzip
action="unzip"
file="#ExpandPath( '../properties-feed/data.zip/' )#"
destination="#ExpandPath( './assets/images/resized/original/' )#"
overwrite="true"
/>

<cfset dirToReadFromOriginal = #ExpandPath( './assets/images/resized/original/' )# />

<cfdirectory
action="list"
directory="#dirToReadFromOriginal#"
listinfo="name"
name="qFileOriginal"
sort="asc"
filter="*.jpg"
/>
<cfset imagesLatest = ArrayNew(1)>
<cfoutput query="qFileOriginal">
    <cfset ArrayAppend(imagesLatest, #qFileOriginal.name#)>
</cfoutput>

<!--- Loop over your current images --->
<cfloop query="qFileOriginal">
    <!--- Check for a matching file name --->
    <cfquery name="fileExists" dbtype="query">
        SELECT
            COUNT(*) AS num_Rec
        FROM
            qfile
        WHERE
            name = <cfqueryparam cfsqltype="cf_sql_varchar" value="#qFileOriginal.name#" />
    </cfquery>

    <!--- do we have a matching file name? --->
    <cfif val(fileExists.num_rec)>
        <cfimage action="read" name="newImage" source="#dirToReadFrom##qFile.name#"/>
        <cfimage action="read" name="originalImage" source="#dirToReadFromOriginal##qFileOriginal.name#"/>

        <cfset newImageBlob = ImageGetBlob(newImage) />
        <cfset originalImageBlob = ImageGetBlob(originalImage) />       

        <!--- Compare --->  
        <cfif toString(newImageBlob) eq toString(originalImageBlob) >
            Images are same
            <cfelse>
            DIFFERENT
        </cfif>     

    </cfif>

</cfloop>

The code doesn't seem to be working. Can Anyone see what am I doing wrong?

Update 1 from comments

The result that I actually get is that the first images are same and the rest of images in files are different. But this is not correct as most of the images that I am comparing are same.

Update 2 from comments

It incorrectly identifies same images as being different. What I actually get is that the first two images are same and the rest is different. Which is not right as most of the images I have are same.

1

There are 1 answers

0
Njna Grimsdottir On

I've always just done this with BinaryEncode(), and then compare the resulting strings. You have to be careful though, as compression can make the files different even though they look (to the eye) exactly the same.