0

yesterday I posted about a problem with recreating an Error Level Analysis in PHP with Imagmagick. In this question I found a solution with the command-line interaction and tried to translate it into Imagick and PHP.

The following code was proposed:

convert barn.jpg \( +clone -quality 95 \) -compose difference -composite -auto-level -gamma 1.5 barn_ela.png

In this example, the result should highlight the manipulated parts of the image. So I implemented the following code:

            $ELAImageMagick = new Imagick($targetDir . $OriginalImage);
            $OriginalImageMagick = new Imagick($targetDir . $OriginalImage);

            $ELAImageMagick->setImageCompression(Imagick::COMPRESSION_JPEG);
            $ELAImageMagick->setImageCompressionQuality($this->getRequest()->postVar('elaQuality'));

            $ELAImageMagick->compositeImage($OriginalImageMagick, Imagick::COMPOSITE_DIFFERENCE, 1,1);

            $ELAImageMagick->autoLevelImage();

            //Set gamma with a slider in the frontend
            $ELAImageMagick->gammaImage($this->getRequest()->postVar('elaSize'));

            //save ELA-image into Folder
            $ELAImageMagick->writeImage($targetDir . $ELAImage);

Unfortunatly the result does not come close to the desired optic of the result:

The original image (yellow bird has been added in photoshop)

The ELA-Result

Does anyone have an idea, what step I didn't catch quite right and how to solve it? I looked into the documentation and didn't really find any alternatives to this.

Thanks in advance!

AnBau
  • 3
  • 1
  • Does the Imagemagick code you show actually work on your image? The issue may be the code you are trying to use is not proper for your image. First prove it works in Imagemagick, then try to convert to Imagick – fmw42 Aug 10 '22 at 22:29
  • -quality only works when writing an output. When I try your code I get a totally black results since the difference image is zero. – fmw42 Aug 10 '22 at 22:35
  • ELA-Quality is defined by a Range in the HTML-Frontend, but I tried it with a static 95 as well. Same goes for gamma, there I used static value of 1.5 – AnBau Aug 10 '22 at 22:45
  • If you are using some spatially adaptive values, then trying to implement it with static values will not work the same – fmw42 Aug 10 '22 at 22:49

1 Answers1

0

You need to first prove that your code works in Imagemagick. But -quality only works for writing a JPG output. So you need to actually save the result of the -quality operation as a JPG and then use it in a second command. Here I just pipe from one convert to another. The first convert actually creates the compressed JPG but just passes it to the second command without writing to disk.

Input:

enter image description here

convert birds.jpg -quality 95 JPG:- | convert birds.jpg - -compose difference -composite -auto-level -gamma 1.5 birds_ela.png

The result I get is:

enter image description here

This is a JPG version of the PNG output since the PNG was too large to post.

However the results from the command line do not match the resulting image you show. So the command is not going to work in Imagick either.

ADDITION:

From what I can see there is no tampering other than possibly the orange bird in the middle. Most of the image is flat or has random noise. The small bird in the middle has a bit brighter texture and so may be an insert.

enter image description here

ADDITION 2

For enhancing edge artifacts, add -geometry +1+1 to my command above just before -compose difference.

convert birds.jpg -quality 95 JPG:- | convert birds.jpg - -geometry +1+1 -compose difference -composite -auto-level -gamma 2 -define jpeg:extent=2000kb birds_ela2.png

Again posting a JPG version for file size issues.

This looks similar to your posted result.

enter image description here

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • I saw that you had two steps, first -compose difference and then -composite. Could it be, that this part is missing in my code? – AnBau Aug 10 '22 at 22:51
  • I do not understand your question. you must have both. -compose is the setting and is set to difference. -composite actually does the difference. I have two input image in the second command. The first is the bird image. The seconds is the "-" which brings in the piped compressed image. So it is like your command but actually "writes" the compressed image so that it can be used. Does that answer your question? – fmw42 Aug 10 '22 at 23:00
  • Yes, that helps. But the big question for me is, wether it is possible to recreate this code in PHP. I now write the compressed image out and create a new Imagick with the compressed image. Then I use the compositeImage()-Function with Imagick::COMPOSITE_DIFFERENCE as you suggested in the other question. After that the code stayed the same, but I get totally different results. Am I missing something? – AnBau Aug 10 '22 at 23:08
  • Show your new code and result. I do not use Imagick. But your code so far looks to be correct in implementation. I do not know what other differences there are in Imagick from Imagemagick nor if you are using the same Imagemagick version within Imagick. – fmw42 Aug 10 '22 at 23:28
  • You have `$ELAImageMagick->compositeImage($OriginalImageMagick, Imagick::COMPOSITE_DIFFERENCE, 1,1); ` with last arguments 1,1. Those should be 0,0. You do not want an offset between the two images when you do the difference. You want to difference them at the same pixel locations in each image. Why are you using 1,1? – fmw42 Aug 10 '22 at 23:30
  • See my ADDITION in my answer. – fmw42 Aug 10 '22 at 23:41
  • See my ADDITION 2 in my answer. I thought you were looking for JPG differences. If you are looking for edge effects, then you do want the 1,1 offset. To implement that in Imagemagick add -geometry +1+1. – fmw42 Aug 10 '22 at 23:45
  • You were absolutely right! I had the offset to 1,1 because then I didn't get a black screen. But that was because I didn't save the image in a compressed form before. After I implemented your suggestion of saving the compressed image first, it worked with the offset(0,0) and I got the real ELA-Image just like you got. Thanks a lot for your help! – AnBau Aug 11 '22 at 09:16