@@ -446,12 +446,25 @@ class PipelineWorker : public Nan::AsyncWorker {
446446 image = image.bandjoin (
447447 VImage::new_matrix (image.width (), image.height ()).new_from_image (255 * multiplier));
448448 }
449+
449450 // Embed
450- int left = static_cast <int >(round ((baton->width - image.width ()) / 2 ));
451- int top = static_cast <int >(round ((baton->height - image.height ()) / 2 ));
452- image = image.embed (left, top, baton->width , baton->height , VImage::option ()
451+
452+ // Calculate where to position the embeded image if gravity specified, else center.
453+ int left;
454+ int top;
455+
456+ left = static_cast <int >(round ((baton->width - image.width ()) / 2 ));
457+ top = static_cast <int >(round ((baton->height - image.height ()) / 2 ));
458+
459+ int width = std::max (image.width (), baton->width );
460+ int height = std::max (image.height (), baton->height );
461+ std::tie (left, top) = sharp::CalculateEmbedPosition (
462+ image.width (), image.height (), baton->width , baton->height , baton->embed );
463+
464+ image = image.embed (left, top, width, height, VImage::option ()
453465 ->set (" extend" , VIPS_EXTEND_BACKGROUND )
454466 ->set (" background" , background));
467+
455468 } else if (baton->canvas != Canvas::IGNORE_ASPECT ) {
456469 // Crop/max/min
457470 if (baton->crop < 9 ) {
@@ -1141,6 +1154,7 @@ NAN_METHOD(pipeline) {
11411154 // Resize options
11421155 baton->withoutEnlargement = AttrTo<bool >(options, " withoutEnlargement" );
11431156 baton->crop = AttrTo<int32_t >(options, " crop" );
1157+ baton->embed = AttrTo<int32_t >(options, " embed" );
11441158 baton->kernel = AttrAsStr (options, " kernel" );
11451159 baton->fastShrinkOnLoad = AttrTo<bool >(options, " fastShrinkOnLoad" );
11461160 // Join Channel Options
0 commit comments