如何获得自动作物的偏移量和大小? [英] How to get autocrops offset and size?

查看:132
本文介绍了如何获得自动作物的偏移量和大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

php函数imagecropauto()可以自动裁剪图像.

The php-function imagecropauto() can automaticly crop an image.

我想它会做一些计算,并将偏移量和大小作为参数发送给imagecrop()函数.

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?

推荐答案

在这里,通过交换右下角的颜色,这是一个丑陋的解决方案.
有没有更简单的方法?

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]);
}

这篇关于如何获得自动作物的偏移量和大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆