cto*_*ife 4 php imagemagick imagick
我正在使用php Imagick :: cropImage,我遇到了一些麻烦.
假设我有这个图像:

我想用这个裁剪区裁剪图像:

这是我正在使用的PHP代码:
$width = 200;
$height = 200;
$x = -100;
$y = -50;
$image = new Imagick();
$image->readImage($path_to_image);
$image->cropImage( $width, $height, $x, $y );
$image->writeImage($path_to_image);
$image->clear();
$image->destroy();
Run Code Online (Sandbox Code Playgroud)
结果是50px x 150px图像(这不是我想要的):

我想要的是一个200px x 200px图像,其中填充其余部分(检查图案说明透明像素):

如何填充这些空像素?
裁剪后使用Imagick :: extentImage将图像增长到预期的图像大小.无论是设置背景颜色还是根据需要进行填充,都可以轻松填充"空"像素.
$width = 100;
$height = 100;
$x = -50;
$y = -25;
$image = new Imagick();
$image->readImage('rose:');
$image->cropImage( $width, $height, $x, $y );
$image->extentImage( $width, $height, $x, $y );
Run Code Online (Sandbox Code Playgroud)

用背景填充空像素
$image = new Imagick();
$image->readImage('rose:');
$image->setImageBackgroundColor('orange');
$image->cropImage( $width, $height, $x, $y );
$image->extentImage( $width, $height, $x, $y );
Run Code Online (Sandbox Code Playgroud)

或者ImagickDraw
$image = new Imagick();
$image->readImage('rose:');
$image->cropImage( $width, $height, $x, $y );
$image->extentImage( $width, $height, $x, $y );
$draw = new ImagickDraw();
$draw->setFillColor('lime');
$draw->color(0, 0, Imagick::PAINT_FLOODFILL);
$image->drawImage($draw);
Run Code Online (Sandbox Code Playgroud)

要设置透明空像素,请在背景颜色之前设置遮罩
$image->setImageMatte(true);
$image->setImageBackgroundColor('transparent');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1286 次 |
| 最近记录: |