<commit_before><?php namespace Skvn\Crud\Traits;

use Skvn\Crud\Form\Field;

/**
 * Class ModelInlineImgTrait
 * Provides process inline images functionality
 * @package Skvn\Crud
 * @author <NAME> <<EMAIL>>
 */
trait ModelInlineImgTrait {


    /**
     * @var array columns that should be processed
     */
    protected $inlimgCols = [];
    /**
     * @var int Max image width
     */
    protected $maxWidth = 2000;



    protected function appendInlineImgConfig()
    {
        $cols = [];
        if (!empty($this->config['fields']))
        {
            foreach ($this->config['fields'] as $name => $field)
            {
                if (!empty($field['type']) && in_array($field['type'], [Field :: TEXTAREA]) && !empty($field['editor']))
                {
                    $cols[] = $name;
                }
            }
        }
        $this->inlimgCols = $cols;
    }


    /**
     * Laravel model boot
     */
    public static function bootModelInlineImgTrait()
    {
        static::registerPostconstruct(function($instance){
            $instance->appendInlineImgConfig();
        });
        static::saving(function($instance) {
            if ($instance->eventsDisabled)
            {
                return true;
            }
            foreach ($instance->inlimgCols as $attr)
            {
                $instance->setAttribute($attr, $instance->processInlineImgs($instance->getAttribute($attr)));
            }
        });
    }


    /**
     * Proces text for inline images
     * @param $text
     * @return mixed
     */
    public  function processInlineImgs($text)
    {
        if (preg_match_all('#(<img\s(?>(?!src=)[^>])*?src=")(data:image/(gif|png|jpeg);base64,([\w=+/]++))("[^>].*>)#siUm', $text, $matches, PREG_SET_ORDER))
        {

            foreach ($matches as $m)
            {
                if (!empty($m[4]))
                {
                    if (preg_match("#width:(.*);#siU",$m[0], $wm))
                    {

                        $width = trim($wm[1]);
                    }
                    $src = $m[2];
                    $base_64 = $m[4];
                    $img = \Image :: make(base64_decode($base_64));
                    $originalWidth = $img->width();
                    if (strpos($width,'%') !== false)
                    {
                        $newWidth = $originalWidth/100*intval(trim(str_replace('%','',$width)));
                    }
                    else
                    {
                        $newWidth = (int)trim(str_replace('px','',$width));
                    }
                    $resizeWidth = $this->maxWidth;
                    if ($newWidth>$this->maxWidth)
                    {
                        $resizeWidth = $newWidth;
                    }
                    $img->resize($resizeWidth, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                    $ext = $m[3];
                    if ($ext == 'jpeg')
                    {
                        $ext = 'jpg';
                    }
                    $filename = $this->generateInlineImgFilename($ext);
                    $this->app['files']->makeDirectory(dirname($this->getInlineImgPath($filename)), 0755, true, true);
                    $img->save($this->getInlineImgPath($filename));

                    $text = str_replace($src, $this->getInlineImgUrl($filename), $text);
                }
            }
        }
        return $text;
    }

    protected function getInlineImgFilename($filename)
    {
        return $this->getFilesConfig($filename, "inline_path") . DIRECTORY_SEPARATOR . $filename;
        //return ($this->getFilesConfig($filename, "inline_path") ?: ('images/' . $this->table . '/' . $this->id)) . DIRECTORY_SEPARATOR . $filename;
    }

    protected function getInlineImgUrl($filename)
    {
        return $this->getFilesConfig($filename, "inline_url") . "/" .  $this->getInlineImgFilename($filename);
//        if ($url = $this->getFilesConfig($filename, "inline_url"))
//        {
//        }
//        return '/' . $this->getInlineImgFilename($filename);
    }

    protected function getInlineImgPath($filename)
    {
        return $this->getFilesConfig($filename, "inline_root") . DIRECTORY_SEPARATOR . $this->getInlineImgFilename($filename);
//        if ($root = )
//        {
//            return $root . DIRECTORY_SEPARATOR . $this->getInlineImgFilename($filename);
//        }
//        return public_path($this->getInlineImgFilename($filename));
    }

    protected function generateInlineImgFilename($ext)
    {
        return str_replace(".", "-", uniqid($this->getFilesConfig('inline', 'prefix') ?: 'img', true)).'.'.$ext;
    }


} <commit_msg>-- constant removed<commit_after><?php namespace Skvn\Crud\Traits;

use Skvn\Crud\Form\Field;

/**
 * Class ModelInlineImgTrait
 * Provides process inline images functionality
 * @package Skvn\Crud
 * @author <NAME> <<EMAIL>>
 */
trait ModelInlineImgTrait {


    /**
     * @var array columns that should be processed
     */
    protected $inlimgCols = [];
    /**
     * @var int Max image width
     */
    protected $maxWidth = 2000;



    protected function appendInlineImgConfig()
    {
        $cols = [];
        if (!empty($this->config['fields']))
        {
            foreach ($this->config['fields'] as $name => $field)
            {
                if (!empty($field['type']) && in_array($field['type'], ["textarea"]) && !empty($field['editor']))
                {
                    $cols[] = $name;
                }
            }
        }
        $this->inlimgCols = $cols;
    }


    /**
     * Laravel model boot
     */
    public static function bootModelInlineImgTrait()
    {
        static::registerPostconstruct(function($instance){
            $instance->appendInlineImgConfig();
        });
        static::saving(function($instance) {
            if ($instance->eventsDisabled)
            {
                return true;
            }
            foreach ($instance->inlimgCols as $attr)
            {
                $instance->setAttribute($attr, $instance->processInlineImgs($instance->getAttribute($attr)));
            }
        });
    }


    /**
     * Proces text for inline images
     * @param $text
     * @return mixed
     */
    public  function processInlineImgs($text)
    {
        if (preg_match_all('#(<img\s(?>(?!src=)[^>])*?src=")(data:image/(gif|png|jpeg);base64,([\w=+/]++))("[^>].*>)#siUm', $text, $matches, PREG_SET_ORDER))
        {

            foreach ($matches as $m)
            {
                if (!empty($m[4]))
                {
                    if (preg_match("#width:(.*);#siU",$m[0], $wm))
                    {

                        $width = trim($wm[1]);
                    }
                    $src = $m[2];
                    $base_64 = $m[4];
                    $img = \Image :: make(base64_decode($base_64));
                    $originalWidth = $img->width();
                    if (strpos($width,'%') !== false)
                    {
                        $newWidth = $originalWidth/100*intval(trim(str_replace('%','',$width)));
                    }
                    else
                    {
                        $newWidth = (int)trim(str_replace('px','',$width));
                    }
                    $resizeWidth = $this->maxWidth;
                    if ($newWidth>$this->maxWidth)
                    {
                        $resizeWidth = $newWidth;
                    }
                    $img->resize($resizeWidth, null, function ($constraint) {
                        $constraint->aspectRatio();
                    });
                    $ext = $m[3];
                    if ($ext == 'jpeg')
                    {
                        $ext = 'jpg';
                    }
                    $filename = $this->generateInlineImgFilename($ext);
                    $this->app['files']->makeDirectory(dirname($this->getInlineImgPath($filename)), 0755, true, true);
                    $img->save($this->getInlineImgPath($filename));

                    $text = str_replace($src, $this->getInlineImgUrl($filename), $text);
                }
            }
        }
        return $text;
    }

    protected function getInlineImgFilename($filename)
    {
        return $this->getFilesConfig($filename, "inline_path") . DIRECTORY_SEPARATOR . $filename;
        //return ($this->getFilesConfig($filename, "inline_path") ?: ('images/' . $this->table . '/' . $this->id)) . DIRECTORY_SEPARATOR . $filename;
    }

    protected function getInlineImgUrl($filename)
    {
        return $this->getFilesConfig($filename, "inline_url") . "/" .  $this->getInlineImgFilename($filename);
//        if ($url = $this->getFilesConfig($filename, "inline_url"))
//        {
//        }
//        return '/' . $this->getInlineImgFilename($filename);
    }

    protected function getInlineImgPath($filename)
    {
        return $this->getFilesConfig($filename, "inline_root") . DIRECTORY_SEPARATOR . $this->getInlineImgFilename($filename);
//        if ($root = )
//        {
//            return $root . DIRECTORY_SEPARATOR . $this->getInlineImgFilename($filename);
//        }
//        return public_path($this->getInlineImgFilename($filename));
    }

    protected function generateInlineImgFilename($ext)
    {
        return str_replace(".", "-", uniqid($this->getFilesConfig('inline', 'prefix') ?: 'img', true)).'.'.$ext;
    }


} 
{'fraction_non_alphanumeric': 0.15885389234172548, 'fraction_numerical': 0.005267038870746866, 'mean_word_length': 1.6032355360570332, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3610139', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2577, 'n_words': 415}