Jump to content

Recommended Posts

HI, I have a need to compare 2 images using a php script and find the differences.  I've looked through the GD functions and can't find anything that might work, but I'm sure there is a way to do it.  I don't know how to check a pixel's color.  If I could get that, I could, in theory, just create an array for both images and compare the difference ratio, if it is above a certain threshold, then It's sufficiently different.  I am backing up images from another server that change each minute (though I'm only getting a new one every 10 minutes), and I want to cut down on unwanted overhead caused by similar files.  Can anyone help me?

Link to comment
https://forums.phpfreaks.com/topic/123722-compare-2-images-using-php/
Share on other sites

Jeeze... Your asking for ALOTTTTT

 

You realize (and i THINK this is how it works) that an image with only 500X500 resolution has 250000 pixels...

 

Thats ALOT of ifs lol. (im sure there is an easier way. But not much easier)

 

And, honestly. I don't think there is a way to do that in PHP that wouldn't amount to the same overhead... Because thats ALOT of checking.

 

But good luck and i'll do some digging for you to see if i can turn anything up. I am by no means a PHP expert but i am trying to give back to this community that has given me so much! (looks like money is the only way i'm useful HAH)

mathew's probably right....that's a lot of overhead

I don't think I've ever compared to images like that before.....it's got me curious I'll admit

$img = imagecreatefrompng("php.png");
$rgb = imagecolorat($img, 0, 0);
$r = ($rgb >> 16) & 0xFF;  //119 = 1110111
$g = ($rgb >>  & 0xFF; //123 = 1111011
$b = $rgb & 0xFF; //180 = 10110100
?>

That would get you the Red Green and Blue colors for the very beginning pixel.

of course...then you'd need a loop for EVERY pixel, I assume you know that

 

and probably another loop for each color, red green and blue.

 

 

and when you're comparing them you could use the XOR operator to do it

 

echo $r XOR $r2 .... etc

 

 

hope this helps and good luck

What is the threshhold? You would have to compare color variations, size, etc. There might be some third party tools that you could plug into.

 

ImageMagick can do a mathmatical comparison betwen two images and has a command line interface, so that might be a possibility. http://www.imagemagick.org/script/compare.php

I could use that if my server were running windows, but it's not, so I can't.  This will all be server side.  I'll just need the threshold be like, idk, 10% difference.  The script can work, and if it reaches a 10% threshold (10% of the total pixels, not of the pixels checked) of difference anytime while it's running, it stops and marks the image as sufficiently different from the comparison image.  This is basically being done because I'm archiving an image from another site that changes every minute, I transfer a new one every 10 minutes, but I want to not transfer over duplicate images.  Of course, every one will be different as far as the MD5 hash goes, because of differences in compression, lighting, etc.  I just want an image thatis more than 10% different than the previous one to be saved, and everything else to be discarded.  Since this is supposedly really CPU intensive, and because I'm on shared hosting, I think I'll let it rest for now, I might come back to it later though.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.