Question

I am using imagemagick to overlay one image over another with a blending option. However, the overlay can be bigger or smaller than the background image. I'd like to resize the overlay image on the fly so it matches the background, then merge them together. I know the background image dimensions ahead of time. I have the following that works.

exec("convert $img_in test_images/leak_1.jpg -compose softlight -gravity center -composite $img_out");

And I want to do something like this:

exec("convert $img_in (test_images/leak_1.jpg -resize {$w}x{$h} -compose softlight -gravity center -composite $img_out");
Was it helpful?

Solution

Resize as a sub-process. Use the area flag (^), or ignore aspect ratio (!) if needed (see examples)

$dim = "{$w}x{$h}^";
$job = "convert \( $img_in -resize {$dim} \) test_images/leak_1.jpg "
     . " -compose softlight -gravity center -composite $img_out";
$ok  = exec($job);

Although the background image is known, it might be wise to collect the dimensions at run time, and build out error handling.

$dim = exec("convert -ping -format '%wx%h^' test_images/leak_1.jpg info:-");
// Do error handle for overlay
$job = "convert \( $img_in -resize {$dim} \) test_images/leak_1.jpg "
     . " -compose softlight -gravity center -composite $img_out";
$ok  = exec($job);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top