Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do with Imagick extension. Imagick is a native php extension to create and modify images using the ImageMagick API. </p> <p>Example Using Imagick::compareImages():</p> <p>Compare images and display the reconstructed image.</p> <pre><code>&lt;?php $image1 = new imagick("image1.png"); $image2 = new imagick("image2.png"); $result = $image1-&gt;compareImages($image2, Imagick::METRIC_MEANSQUAREERROR); $result[0]-&gt;setImageFormat("png"); header("Content-Type: image/png"); echo $result[0]; ?&gt; </code></pre> <p>OR </p> <p>A PHP shell script to tell you if two images are visually different by comparing them pixel by pixel. If there's a difference, the script creates a third image - black background with the different pixels in green. Here two GD images are created from the input files, also a third GD image with black background to store the diff. Loop through all the pixels and compare them one by one. If one is different, write a green pixel at the same location of the diff image. [Source : <a href="http://www.phpied.com/image-diff/" rel="nofollow">http://www.phpied.com/image-diff/</a> ]</p> <p>diff.php</p> <pre><code>&lt;?php /** * Shell script to tell if two images are identical. * If not, a third image is written - black background with the different pixels painted green * Code partially inspired by and borrowed from http://pear.php.net/Image_Text test cases */ // check if there's enough input if (empty($argv[1]) || empty($argv[2])) { echo 'gimme at least two image filenames, please.', "\n"; echo 'e.g. "php idiff.php img1.png img2.png"'; echo "\n", 'third filename is the image diff, optional, default is "diffy.png"'; exit(1); } // create images $i1 = @imagecreatefromstring(file_get_contents($argv[1])); $i2 = @imagecreatefromstring(file_get_contents($argv[2])); // check if we were given garbage if (!$i1) { echo $argv[1] . ' is not a valid image'; exit(1); } if (!$i2) { echo $argv[2] . ' is not a valid image'; exit(1); } // dimensions of the first image $sx1 = imagesx($i1); $sy1 = imagesy($i1); // compare dimensions if ($sx1 !== imagesx($i2) || $sy1 !== imagesy($i2)) { echo "The images are not even the same size"; exit(1); } // create a diff image $diffi = imagecreatetruecolor($sx1, $sy1); $green = imagecolorallocate($diffi, 0, 255, 0); imagefill($diffi, 0, 0, imagecolorallocate($diffi, 0, 0, 0)); // increment this counter when encountering a pixel diff $different_pixels = 0; // loop x and y for ($x = 0; $x &lt; $sx1; $x++) { for ($y = 0; $y &lt; $sy1; $y++) { $rgb1 = imagecolorat($i1, $x, $y); $pix1 = imagecolorsforindex($i1, $rgb1); $rgb2 = imagecolorat($i2, $x, $y); $pix2 = imagecolorsforindex($i2, $rgb2); if ($pix1 !== $pix2) { // different pixel // increment and paint in the diff image $different_pixels++; imagesetpixel($diffi, $x, $y, $green); } } } if (!$different_pixels) { echo "Image is the same"; exit(0); } else { if (empty($argv[3])) { $argv[3] = 'diffy.png'; // default result filename } imagepng($diffi, $argv[3]); $total = $sx1 * $sy1; echo "$different_pixels/$total different pixels, or ", number_format(100 * $different_pixels / $total, 2), '%'; exit(1); } ?&gt; </code></pre> <p>Usage (Command line):</p> <p>php diff.php img1.png img2.png result.png</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload