3

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.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Roman
  • 1,118
  • 3
  • 15
  • 37
  • 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. – Roman Jan 16 '14 at 15:30
  • Define "doesn't seem to be working" - it errors, it incorrectly identifies different images as being identical, or it incorrectly identifies identical images as being different? – duncan Jan 16 '14 at 15:35
  • 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. – Roman Jan 16 '14 at 15:40
  • 1
    Where is the variable `qFile.name` coming from in your `` statement? In the code that you have posted you are looping over the `qFileOriginal` query but not the `qFile` query. So your _newImage_ is never changing but the _originalImage_ is. Is that the correct logic? You may need to share more information. And if you do, please [edit your question](http://stackoverflow.com/posts/21165612/edit) to add the information instead of in these comments. In order to debug I would output the filenames that are being compared. – Miguel-F Jan 16 '14 at 16:12

1 Answers1

1

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.