HaLo2FrEeEk Posted September 11, 2008 Share Posted September 11, 2008 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? Quote Link to comment Share on other sites More sharing options...
matthew798 Posted September 11, 2008 Share Posted September 11, 2008 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) Quote Link to comment Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 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 Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted September 11, 2008 Author Share Posted September 11, 2008 Shoot, maybe I won't do this then, I'm already creating a lot of overhead on the server by having a dynamically generated animated gif done by php...I might come back to this later, but for now, thank you for the starter help. Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 11, 2008 Share Posted September 11, 2008 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 Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted September 12, 2008 Author Share Posted September 12, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted September 12, 2008 Share Posted September 12, 2008 I think it would be better to create an external program in C/C++ instead of PHP. That would be much faster and easier on the CPU. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 12, 2008 Share Posted September 12, 2008 You'd create much more overhead by processing the image than transferring it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.