我不知道为什么你不能为四边滤波器 (IMG_CROP_SIDES) 设置阈值,所以这里是如何使用 IMG_CROP_THRESHOLD 滤波器手动执行此操作。
$threshold = .5;
$im = imagecreatefromjpeg('somefile.jpg');
$width = imagesx($im);
$height = imagesy($im);
$arr = [
[0,0],
[$width-1,0],
[0,$height-1],
[$width-1,$height-1],
];
$red = 0;
$green = 0;
$blue = 0;
// 从所有四个角获取颜色
foreach( $arr as $arr2 ) {
$thisColor = imagecolorat($im, $arr2[0], $arr2[1]);
$rgb = imagecolorsforindex($im, $thisColor);
$red += round(round(($rgb['red'] / 0x33)) * 0x33);
$green += round(round(($rgb['green'] / 0x33)) * 0x33);
$blue += round(round(($rgb['blue'] / 0x33)) * 0x33);
}
// 并对其求平均值
$red /= 4;
$green /= 4;
$blue /= 4;
$newColor = imagecolorallocate($im, $red, $green, $blue);
$cropped = imagecropauto($im, IMG_CROP_THRESHOLD, $threshold, $newColor);
imagejpg($cropped, 'somefile.cropped.jpg');
imagedestroy($im);
imagedestroy($cropped);