问题
The php-function imagecropauto()
can automaticly crop an image.
and I guess its do some calculations and send the offset and size as paramters to the imagecrop()
function.
The size value I can get from the returned image, but is there someway to get the offset value?
回答1:
Here is an ugly solution, by swaping the color of the botom-right corner.
Is there a easier way?
function autocrop_range($image)
{
$org_size = array(imagesx($image), imagesy($image));
$croped = imagecropauto($image, IMG_CROP_SIDES);
if(!$croped)
{
return FALSE;
}
$croped_size = array(imagesx($croped), imagesy($croped));
if($org_size == $croped_size)
{
return FALSE;
}
imagedestroy($croped);
$copy = imagecrop($image, array('x' => 0, 'y' => 0, 'width' => $org_size[0], 'height' => $org_size[1]));
$corner_color = imagecolorat($copy, $org_size[0] - 1, $org_size[1] - 1);
$r = ($corner_color >> 16) & 0xFF;
$g = ($corner_color >> 8) & 0xFF;
$b = ($corner_color) & 0xFF;
$other_color = imagecolorallocate($copy, $r ^ 0x80, $r ^ 0x80, $r ^ 0x80);
imagesetpixel($copy, $org_size[0] - 1, $org_size[1] - 1, $other_color); // force botom to stay uncroped
imagesetpixel($copy, $org_size[0] - 1, $org_size[1] - 2, $other_color); // force right to stay uncroped
$corner_croped = imagecropauto($copy, IMG_CROP_SIDES);
imagedestroy($copy);
$corner_croped_size = array(imagesx($corner_croped), imagesy($corner_croped));
imagedestroy($corner_croped);
return array('x' => ($org_size[0] - $corner_croped_size[0]), 'y' => ($org_size[1] - $corner_croped_size[1]), 'width' => $croped_size[0], 'height' => $croped_size[1]);
}
来源:https://stackoverflow.com/questions/40584754/how-to-get-autocrops-offset-and-size