src/Db/ImageAggrDbAgent.php
public function cropToDb(ARef $aRef, ImageSettingInterface $imageSetting, CropSettingInterface $cropSetting, $attrs = [], $init = false)
    {
        $owner_name = $aRef->getType()->getName();
        $image_name = $imageSetting->getName();
        $crop_name = $cropSetting->getName();
        $owner_id = $aRef->getId();

        $collection = Crop::where('entity_name', '=', $owner_name)
            ->where('entity_id', '=', $owner_id)
            ->where('image_name', '=', $image_name)
            ->where('name', '=', $crop_name)
            ->get();

        $cache_index = 0;

        $crop = null;

        //Удалим лишние записи по этому ключу, если есть (на всякий случай, т. к. записи в БД уникальны по авто инк. id)
        foreach($collection as $curr_crop)
        {
            if($crop === null)
            {
                $cache_index = $curr_crop->cache_index;
                $crop = $curr_crop;

                if($init)
                {
                    break;
                }
            }
            else
            {
                $crop->delete();
            }
        }

        $db = !$init;

        if($crop === null)
        {
            $crop = new Crop;
            $crop->alt = '';
            $crop->link = '';
            $crop->width = 400;
            $crop->height = 400;
            $crop->x = 0;
            $crop->y = 0;
            $db = true;
        }

        $cache_index++;

        if($db)
        {
            if(!array_key_exists('alt', $attrs))
            {
                $attrs['alt'] = $crop->alt;
            }

            if(!array_key_exists('link', $attrs))
            {
                $attrs['link'] = $crop->link;
            }

            if(!array_key_exists('width', $attrs))
            {
                $attrs['width'] = $crop->width;
            }

            if(!array_key_exists('height', $attrs))
            {
                $attrs['height'] = $crop->height;
            }

            if(!array_key_exists('x', $attrs))
            {
                $attrs['x'] = $crop->x;
            }

            if(!array_key_exists('y', $attrs))
            {
                $attrs['y'] = $crop->y;
            }

            $crop->name        = $crop_name;
            $crop->entity_name = $owner_name;
            $crop->entity_id   = $owner_id;
            $crop->image_name  = $image_name;
            $crop->alt         = $attrs['alt'];
            $crop->link        = $attrs['link'];
            $crop->man_name    = $cropSetting->getMan()->getName();
            $crop->target_name = $cropSetting->getTarget()->getName();
            $crop->cache_index = $cache_index;

            $crop->x = $attrs['x'];
            $crop->y = $attrs['y'];

            //Кроп всегда задан постоянными размерами
            $crop->width  = $attrs['width'];
            $crop->height = $attrs['height'];

            $crop->save();
        }
    }
public function cropToDb(ARef $aRef, ImageSettingInterface $imageSetting, CropSettingInterface $cropSetting, $attrs = [], $init = false)
    {
        $owner_name = $aRef->getType()->getName();
        $image_name = $imageSetting->getName();
        $crop_name = $cropSetting->getName();
        $owner_id = $aRef->getId();

        $collection = Crop::where('entity_name', '=', $owner_name)
            ->where('entity_id', '=', $owner_id)
            ->where('image_name', '=', $image_name)
            ->where('name', '=', $crop_name)
            ->get();

        $cache_index = 0;

        $crop = null;

        //Удалим лишние записи по этому ключу, если есть (на всякий случай, т. к. записи в БД уникальны по авто инк. id)
        foreach($collection as $curr_crop)
        {
            if($crop === null)
            {
                $cache_index = $curr_crop->cache_index;
                $crop = $curr_crop;

                if($init)
                {
                    break;
                }
            }
            else
            {
                $crop->delete();
            }
        }

        $db = !$init;

        if($crop === null)
        {
            $crop = new Crop;
            $crop->alt = '';
            $crop->link = '';
            $crop->width = 400;
            $crop->height = 400;
            $crop->x = 0;
            $crop->y = 0;
            $db = true;
        }

        $cache_index++;

        if($db)
        {
            if(!array_key_exists('alt', $attrs))
            {
                $attrs['alt'] = $crop->alt;
            }

            if(!array_key_exists('link', $attrs))
            {
                $attrs['link'] = $crop->link;
            }

            if(!array_key_exists('width', $attrs))
            {
                $attrs['width'] = $crop->width;
            }

            if(!array_key_exists('height', $attrs))
            {
                $attrs['height'] = $crop->height;
            }

            if(!array_key_exists('x', $attrs))
            {
                $attrs['x'] = $crop->x;
            }

            if(!array_key_exists('y', $attrs))
            {
                $attrs['y'] = $crop->y;
            }

            $crop->name        = $crop_name;
            $crop->entity_name = $owner_name;
            $crop->entity_id   = $owner_id;
            $crop->image_name  = $image_name;
            $crop->alt         = $attrs['alt'];
            $crop->link        = $attrs['link'];
            $crop->man_name    = $cropSetting->getMan()->getName();
            $crop->target_name = $cropSetting->getTarget()->getName();
            $crop->cache_index = $cache_index;

            $crop->x = $attrs['x'];
            $crop->y = $attrs['y'];

            //Кроп всегда задан постоянными размерами
            $crop->width  = $attrs['width'];
            $crop->height = $attrs['height'];

            $crop->save();
        }
    }
@param \Interpro\Core\Contracts\Ref\ARef $aRef
@param \Interpro\ImageAggr\Contracts\Settings\ImageSetting $imageSetting
@param \Interpro\ImageAggr\Contracts\Settings\CropSetting $cropSetting
@param array $attrs

@return void