Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

relationship between the PHP Imagick class and the magick command #84

Open
cmette opened this issue Jun 7, 2022 · 5 comments
Open

relationship between the PHP Imagick class and the magick command #84

cmette opened this issue Jun 7, 2022 · 5 comments

Comments

@cmette
Copy link

cmette commented Jun 7, 2022

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: ImageMagick/ImageMagick#5198

@Danack
Copy link
Collaborator

Danack commented Jun 8, 2022

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.

@Danack
Copy link
Collaborator

Danack commented Jun 8, 2022

Yeah, the output of the link above is meant to be:

convexy

But apparently the output is wrong when building against the current version of Imagick. I'll investigate tomorrow.

@Danack
Copy link
Collaborator

Danack commented Jun 10, 2022

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.

@cmette
Copy link
Author

cmette commented Jun 10, 2022

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:

6947

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:
6947 deskew
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:
6947 trim1

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

@cmette
Copy link
Author

cmette commented Jun 21, 2022

ping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants