简体   繁体   中英

fill extra area with custom color while cropping with Cropper.js

I am using Cropper.js library to get crop coordinates and width and height an image at client side and Intervention in laravel 4 to actually corp it in server side with the corp data.

JavaScript function:

$('#image').cropper({
            aspectRatio: 16 / 9,
            crop: function (e) {
                // To send cop data to server
                x = e.x;
                y = e.y;
                width = e.width;
                height = e.height;
                rotate = e.rotate;
                scaleX = e.scaleX;
                scaleY = e.scaleY;
                $('#x').val(x);
                $('#y').val(y);
                $('#width').val(width);
                $('#height').val(height);
                $('#rotate').val(rotate);
                $('#scaleX').val(scaleX);
                $('#scaleY').val(scaleY);
            }
        });

PHP/Laravel Function:

$img->crop($width, $height, $x, $y);

When cropped area out of image, this extra area is auto filled with black color. I want it to be filled with white color instead.

使用图像区域进行图像裁剪

This generates this image

从上面的裁剪生成的图像

I want to change the black color to white.

You'll need a combination of commands in intervention:

  1. Create canvas at the correct size
  2. Fill it with a colour with fill()
  3. use insert() to place your image

So you'd something like this (not tested)

$destination = Image::canvas($width, $height);
$destination->fill('#ffffff');
$destination->insert($img, $x, $y);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM