Abdul Jabbar
Abdul Jabbar

Reputation: 5931

ImageMagick Trim Whitespace with offset

I am Trimming whitespace from my images using ImageMagick. Everything is working great and successfully trimming. But now I want to leave an offset of about 30px and cut extra whitespace from images. I mean to say my current result is

enter image description here

Now you can see that above image is completely trimmed but I want some offset like

enter image description here

I want to leave 30px on every side and trimming remaining whitespace. I am trimming with Fuzz with following code

$image = new Imagick('capfile.jpg');                                        
$image->trimImage(25000);

I don't want to add borders or crop. I just want to trim with offset of 30px to main image itself because many of my images also have some light background colors which is trimmed using Fuzz so adding borders is not an option.

Upvotes: 2

Views: 2622

Answers (2)

Eryk Wróbel
Eryk Wróbel

Reputation: 445

I know this kinda of silly but this worked for me and it is workaround. I couldn't get this so I figured out completely different approach.

1) Sometimes we are uploading photos that are visualisations like bedroom interior so we don't need to trim whitespace in photo because it is not existing. That is why we need to check if photo is bright or dark with get_avg_luminance function found in this thread https://stackoverflow.com/a/5959461/2760717

2) Now we have to write this

$src_image = new Imagick($src_file);
$dimmensions = $src_image->getImageGeometry();
$height = $dimmensions['height'];
$luminance = get_avg_luminance($src_file);

// If image is mostly bright then add white border with 5% of entire height
if ($luminance > 170) {
    $quantumInfo = $src_image->getQuantumRange();
    if ($src_image->trimImage($quantumInfo['quantumRangeLong']*0.1)) {
            // add white border with 5% of offset
            $src_image->borderImage('#ffffff', 0, ($height *0.05));
    }
}

It is adding border to image and work ok for me :)

Upvotes: 0

Mark Setchell
Mark Setchell

Reputation: 207425

You can get the trim-box that ImageMagick would trim to like this at the command line:

convert -fuzz 10% -format %@ cap.jpg info:
259x171+19+21

Then you can modify the width/height and offset as you wish before using the modified numbers to do a crop - i.e. subtract 30 from the x and y offset and add 60 to the width and height.

Edited by emcconville

How can I do this in php?

Here's a PHP alternative...

$img = new Imagick('/tmp/Zpuq9.jpg');
// Get Quantum to calculate 40%
$quantumInfo = $img->getQuantumRange();
// -fuzz 40%
$img->trimImage($quantumInfo['quantumRangeLong'] * 0.4);
// -format %@
$img->setOption('format','%@');
// info:
$img->setImageFilename('info:');

/*
 * For this example, let's use PHP memory protocol
 * to capture std out (from `info:').
 */
$fd = fopen('php://memory','rwb');
$img->writeImageFile($fd);
fseek($fd, 0);
$info = fread($fd, 1024);
fclose($fd);
var_dump($info);
//=> string(13) "258x170+19+22"

Upvotes: 4

Related Questions