ImagickDemos
ImagickDemos copied to clipboard
relationship between the PHP Imagick class and the magick command
Hello to the round,
can someone explain me in more detail the relationship between the PHP Imagick class and the magick command of the command line tools? (I am using PHP 8.1.6 with ImageMagick 7.1.0.18).
I want to use the commands from the example here: https://imagemagick.org/script/convex-hull.php
...translate into PHP code. In general there should be an equavalent call to the class Imagick - or not?
$imagick = new \Imagick(); $imagick->convexHull() ??
but this is not the case.
Is there any way to translate the Examples from https://imagemagick.org/script/ into PHP code?
Many greetings. Theo
PS: see my post here: https://github.com/ImageMagick/ImageMagick/discussions/5198
So. Imagick uses the ImageMagick library underneath through the MagickWand API. ImageMagick also has command line tools, which mostly expose the same functionality, however.....
ImageMagick has different expectations for users. The command line tools will normally try to guess the right behaviour, whereas with the API it will do exactly as told, which can be annoying.
Also, not all of the functionality available in the command line tools of ImageMagick are exposed 'neatly' for re-use through an API.
Is there any way to translate the Examples from https://imagemagick.org/script/ into PHP code?
It's reasonably fine to just run those scripts through exec or one of the other ways of running stuff on the command line.
In general there should be an equavalent call to the class Imagick - or not?
Usually, but not always.
But convex hulls should be possible through the morphology method
That code is meant to give a convex hull output similar to the example on ImageMagick.
However it doesn't appear to be doing the right thing. I'll need to investigate if I'm just misremembering, or it's borken.
Yeah, the output of the link above is meant to be:
But apparently the output is wrong when building against the current version of Imagick. I'll investigate tomorrow.
Oh. The code works.....but the server is timing out, and so keeps displaying the previous image on the page. Which is a different problem, but that is some code that convex hulls stuff.
Thank you very much for your quick reply!
Probably I have the same timeout problem here. I get blank or the original images when deskewing and cropping photos. That's my overall goal. Either with convexHull or with deskew() + trim(). it's all the same - whichever is better.
I start as follows:
$fname = "6947"; # you can see 6947 below this code
$src = "files/test/$fname.jpg";
$imagick = new \Imagick();
$imagick->readImage(TL_ROOT . '/' . $src);
$html .= $this->ImgToHtml($src, $imagick, "Original");
$imagick->despeckleImage();
This is my source from the scanner:
It should be deskewed in the first step with the following code - this does not work on any server:
$src = "files/test/$fname.deskew.jpg";
#$imagick->setImageBackgroundColor(new ImagickPixel("rgb(254,254,254)"));
$result = $imagick->deskewImage(0);
$imagick->writeImage(TL_ROOT . '/' . $src);
$html .= $this->ImgToHtml($src, $imagick, "deskew: 0");
The result is:
You can see here, deskew is not working (with any parameter from zero to 100)
In the next step i try to trim/autocrop the image with the following code.
$src = "files/test/$fname.trim1.jpg";
#$imagick->setImageBackgroundColor(new ImagickPixel("rgb(254,254,254)"));
$fuzz = 60000;
$result = $imagick->trimImage($fuzz);
$imagick->writeImage(TL_ROOT . '/' . $src);
$html .= $this->ImgToHtml($src, $imagick, "trim1 fuzz: $fuzz");
This is the result:
The fuzz parameter is not well documented. I can't find any detailed information on how it works exactly.
For the previous image, trimImage() happens to work with the value 60000, but this is not true for all my images (3500), and there seems to be no documentation or algorithm that I can use to calculate the fuzz parameter so that it performs an exact trim on arbitrary images.
At the moment it seems that deskew and trim/autocrop do not work with IMagick 7.1.0.18? Is this true? Or am I making a serious mistake here?
Best regards Theo
ping