Gmagick::cropimage()函数是PHP中的内置函数,用于提取图像区域。此功能裁剪图像的一部分。
用法:
public Gmagick::cropimage( $width , $height , $x , $y )
参数:该函数接受上面提到的四个参数,并在下面进行描述。
$width:此参数用于指定裁剪图像的宽度。
$height:此参数用于指定裁剪图像的高度。
$x:此参数用于指定左上角裁剪图像的X坐标。
$y:此参数用于指定左上角裁剪图像的Y坐标。
返回值:此函数返回裁剪的Gmagick对象。
以下示例程序旨在说明PHP中的Gmagick::cropimage()函数:
原始图片1:
示例1:
// require_once('path/vendor/autoload.php');
// Create an Gmagick object
$image = new Gmagick(
'https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png');
// Gmagick function to crop Image
$image->cropimage(390, 100, 0, 0);
header("Content-Type: image/jpg");
// Display the image
echo $image->getImageBlob();
?>
输出:
原始图片2:
示例2:
$string = "Computer Science portal for Geeks!";
// Creating new image of above String
// and add color and background color
$im = new Gmagick();
$draw = new GmagickDraw();
// Fill the color in image
$draw->setFillColor(new GmagickPixel('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 GmagickPixel('white'));
// Draw the image
$im->drawImage($draw);
// Set the image format
$im->setImageFormat('png');
// Gmagick function to crop Image
$im->cropimage(420, 120, 0, 0);
header("Content-Type: image/jpg");
// Display the image
echo $im->getImageBlob();
?>
输出: