Grafika\Gd\Editor::_smartCrop PHP Method

_smartCrop() private method

Crop based on entropy.
private _smartCrop ( Image $oldImage, $cropW, $cropH ) : array
$oldImage Image
$cropW
$cropH
return array
    private function _smartCrop($oldImage, $cropW, $cropH)
    {
        $image = clone $oldImage;
        $this->resizeFit($image, 30, 30);
        $origW = $oldImage->getWidth();
        $origH = $oldImage->getHeight();
        $resizeW = $image->getWidth();
        $resizeH = $image->getHeight();
        $smallCropW = round($resizeW / $origW * $cropW);
        $smallCropH = round($resizeH / $origH * $cropH);
        $step = 1;
        for ($y = 0; $y < $resizeH - $smallCropH; $y += $step) {
            for ($x = 0; $x < $resizeW - $smallCropW; $x += $step) {
                $hist[$x . '-' . $y] = $this->_entropy($image->histogram(array(array($x, $y), array($smallCropW, $smallCropH))));
            }
            if ($resizeW - $smallCropW <= 0) {
                $hist['0-' . $y] = $this->_entropy($image->histogram(array(array(0, 0), array($smallCropW, $smallCropH))));
            }
        }
        if ($resizeH - $smallCropH <= 0) {
            $hist['0-0'] = $this->_entropy($image->histogram(array(array(0, 0), array($smallCropW, $smallCropH))));
        }
        asort($hist);
        end($hist);
        $pos = key($hist);
        // last key
        list($x, $y) = explode('-', $pos);
        $x = round($x * ($origW / $resizeW));
        $y = round($y * ($origH / $resizeH));
        return array($x, $y);
    }