Imagick::cropImage()函数是PHP中的内置函数,用于提取图像区域。
用法:
int Imagick::cropImage( $width, $height, $x, $y )
参数:该函数接受上面提到的四个参数,并在下面进行描述。
$width:此参数用于指定裁剪图像的宽度。
$height:此参数用于指定裁剪图像的高度。
$x:此参数用于指定左上角裁剪图像的X坐标。
$y:此参数用于指定左上角裁剪图像的Y坐标。
返回值:成功时此函数返回True。
以下示例程序旨在说明PHP中的Imagick::cropImage()函数:
原始图片:
示例1:
// require_once('path/vendor/autoload.php');
// Create an imagick object
$image = new Imagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');
// Imagick function to crop Image
$image->cropImage(390, 100, 0, 0);
header("Content-Type: image/jpg");
// Display the image
echo $image->getImageBlob();
?>
输出:
原始图片:
示例2:
$string = "Computer Science portal for Geeks!";
// creating new image of above String
// and add color and background
$im = new Imagick();
$draw = new ImagickDraw();
// Fill the color in image
$draw->setFillColor(new ImagickPixel('green'));
// Set the text font size
$draw->setFontSize(50);
$metrix = $im->queryFontMetrics($draw, $string);
$draw->annotation(0, 40, $string);
$im->newImage($metrix['textWidth'], $metrix['textHeight'],
new ImagickPixel('white'));
// Draw the image
$im->drawImage($draw);
// Set the image format
$im->setImageFormat('png');
// Imagick function to crop Image
$im->cropImage(420, 120, 0, 0);
header("Content-Type: image/jpg");
// Display the image
echo $im->getImageBlob();
?>
输出: