So I am trying to trim the borders of an image with imagemagick trimimage function (PHP), based on the background color. Below is a demonstration of what I am trying to do, and what the problem is.
When I upload this image (this image has thick white borders on the top and the right):
And run the following code:
$canvas->setBackgroundColor('#ffffff');
$quantumRange = $canvas->getQuantumRange();
$canvas->trimImage(0.3 * $quantumRange['quantumRangeLong']);
$canvas->setImagePage(0, 0, 0, 0);
The result is exactly what I expect: The white borders are being trimmed out.
Using the below image however (no outside borders):
and running the same code, the image gets trimmed from the inside(?) with some weird effects:
Now, form what I understood, trimimage should take the background color and trim it from the outside in, and only if the outer margins of the image (borders) do match this color.
So, either I understood trimimage wrong, or I am missing something in the implementation. Also, playing around with the fuzz value is not an option. At some point it will stop trimming the blue border, but at this point it will also NOT trim enough if there are outer borders since I need to have a value high enough to also remove light shadows or compression artifacts.
So the actual questions are: 1. Is there a way to crop the image only from the outside in when the outside border color matches the background color? Maybe I missed something. 2. Is there another method you could recommend?
EDIT: After @fmw42 answered below, I took a closer look at what exactly edge detection is and how it works. My assumption was that imagemagick looks at the borders of the image and cuts them based on the given background color , which is wrong. To better understand, check how edge detection works: https://en.wikipedia.org/wiki/Edge_detection
Thank you.
@mspir
wroteEDIT: After @fmw42 answered below, I took a closer look at what exactly edge detection is and how it works. My assumption was that imagemagick looks at the borders of the image and cuts them based on the given background color , which is wrong.
Why do you say it is wrong? It is correct for doing trimming. If you want automatic trimming based upon edge detection, see my scripts: smarttrim and smartcrop at my web site: fmwconcepts.com/imagemagick/index.php – fmw42