<commit_before><?php

namespace Mindy\Orm\Fields;

use Exception;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use Mindy\Orm\Traits\ImageProcess;
use Mindy\Storage\Files\File;
use Mindy\Storage\FileSystemStorage;
use Mindy\Storage\Interfaces\IExternalStorage;
use Mindy\Storage\MimiBoxStorage;

/**
 * Class ImageField
 * @package Mindy\Orm
 */
class ImageField extends FileField
{
    use ImageProcess;

    /**
     * Array with image sizes
     * key 'original' is reserved!
     * example:
     * [
     *      'thumb' => [
     *          300,200,
     *          'method' => 'adaptiveResize'
     *      ]
     * ]
     *
     * There are 3 methods resize(THUMBNAIL_INSET), adaptiveResize(THUMBNAIL_OUTBOUND),
     * adaptiveResizeFromTop(THUMBNAIL_OUTBOUND from top)
     *
     * @var array
     */
    public $sizes = [];
    /**
     * Force resize images
     * @var bool
     */
    public $force = false;
    /**
     * Imagine default options
     * @var array
     */
    public $options = [
        'resolution-units' => ImageInterface::RESOLUTION_PIXELSPERINCH,
        'resolution-x' => 72,
        'resolution-y' => 72,
        'jpeg_quality' => 100,
        'quality' => 100,
        'png_compression_level' => 0
    ];
    /**
     * @var array|null
     *
     * File MUST be described relative to "www" directory!
     *
     * example
     * [
     *  'file' => 'static/images/watermark.png',
     *  'position' => [200,100]
     * ]
     *
     * OR
     *
     * [
     *  'file' => 'static/images/watermark.png',
     *  'position' => 'top'
     * ]
     *
     * position can be array [x,y] coordinates or
     * string with one of available position
     * top, top-left, top-right, bottom, bottom-left, bottom-right, left, right, center, repeat
     */
    public $watermark = null;
    /**
     * All supported image types
     * @var array|null
     */
    public $types = ['jpg', 'jpeg', 'png', 'gif'];
    /**
     * Default resize method
     * @var string
     */
    public $defaultResize = 'adaptiveResizeFromTop';
    /**
     * @var bool
     */
    public $storeOriginal = true;
    /**
     * Recreate file if missing
     * @var bool
     */
    public $checkMissing = false;

    public function setFile(File $file, $name = null)
    {
        $name = $name ? $name : $file->name;
        if ($this->MD5Name) {
            $ext = pathinfo($name, PATHINFO_EXTENSION);
            $name = md5(str_replace("." . $ext, "", $name)) . '.' . $ext;
        }

        if ($name) {
            $this->value = $this->makeFilePath($name);
            $fileContent = $file->getContent();

            if ($this->getStorage() instanceof FileSystemStorage) {
                try {
                    $image = $this->getImagine()->load($fileContent);
                } catch (Exception $e) {
                    $image = null;
                }
                if ($image) {
                    $fileContent = $this->processSource($image);
                    if ($this->storeOriginal) {
                        $this->value = $this->getStorage()->save($this->value, $fileContent);
                    }
                } else {
                    $this->value = null;
                }
            } elseif ($this->getStorage() instanceof IExternalStorage) {
                $this->value = $this->getStorage()->save($this->value, $fileContent);
            }
        }

        return $this->value;
    }

    public function deleteOld()
    {
        if ($this->getOldValue()) {
            $this->getStorage()->delete($this->getOldValue());
            foreach (array_keys($this->sizes) as $prefix) {
                $this->getStorage()->delete($this->sizeStoragePath($prefix, $this->getOldValue()));
            }
        }
    }

    /**
     * @param $source
     * @param bool $force
     * @param array|null $onlySizes - Resize and save only sizes, described in this array
     * @return
     */
    public function processSource($source, $force = false, $onlySizes = null)
    {
        $ext = pathinfo($this->value, PATHINFO_EXTENSION);
        foreach ($this->sizes as $prefix => $size) {
            if (is_array($onlySizes) && !in_array($prefix, $onlySizes)) {
                continue;
            }
            $width = isset($size[0]) ? $size[0] : null;
            $height = isset($size[1]) ? $size[1] : null;
            if (!$width || !$height) {
                list($width, $height) = $this->imageScale($source, $width, $height);
            }
            $method = isset($size['method']) ? $size['method'] : $this->defaultResize;
            $options = isset($size['options']) ? $size['options'] : $this->options;

            $watermark = isset($size['watermark']) ? $size['watermark'] : $this->watermark;
            if (($width || $height) && $method) {<commit_msg>Fix imageField resize method<commit_after><?php

namespace Mindy\Orm\Fields;

use Exception;
use Imagine\Image\ImageInterface;
use Imagine\Image\Point;
use Mindy\Orm\Traits\ImageProcess;
use Mindy\Storage\Files\File;
use Mindy\Storage\FileSystemStorage;
use Mindy\Storage\Interfaces\IExternalStorage;
use Mindy\Storage\MimiBoxStorage;

/**
 * Class ImageField
 * @package Mindy\Orm
 */
class ImageField extends FileField
{
    use ImageProcess;

    protected $availableResizeMethods = [
        'resize', 'adaptiveResize', 'adaptiveResizeFromTop'
    ];
    /**
     * Array with image sizes
     * key 'original' is reserved!
     * example:
     * [
     *      'thumb' => [
     *          300,200,
     *          'method' => 'adaptiveResize'
     *      ]
     * ]
     *
     * There are 3 methods resize(THUMBNAIL_INSET), adaptiveResize(THUMBNAIL_OUTBOUND),
     * adaptiveResizeFromTop(THUMBNAIL_OUTBOUND from top)
     *
     * @var array
     */
    public $sizes = [];
    /**
     * Force resize images
     * @var bool
     */
    public $force = false;
    /**
     * Imagine default options
     * @var array
     */
    public $options = [
        'resolution-units' => ImageInterface::RESOLUTION_PIXELSPERINCH,
        'resolution-x' => 72,
        'resolution-y' => 72,
        'jpeg_quality' => 100,
        'quality' => 100,
        'png_compression_level' => 0
    ];
    /**
     * @var array|null
     *
     * File MUST be described relative to "www" directory!
     *
     * example
     * [
     *  'file' => 'static/images/watermark.png',
     *  'position' => [200,100]
     * ]
     *
     * OR
     *
     * [
     *  'file' => 'static/images/watermark.png',
     *  'position' => 'top'
     * ]
     *
     * position can be array [x,y] coordinates or
     * string with one of available position
     * top, top-left, top-right, bottom, bottom-left, bottom-right, left, right, center, repeat
     */
    public $watermark = null;
    /**
     * All supported image types
     * @var array|null
     */
    public $types = ['jpg', 'jpeg', 'png', 'gif'];
    /**
     * Default resize method
     * @var string
     */
    public $defaultResize = 'adaptiveResizeFromTop';
    /**
     * @var bool
     */
    public $storeOriginal = true;
    /**
     * Recreate file if missing
     * @var bool
     */
    public $checkMissing = false;

    public function setFile(File $file, $name = null)
    {
        $name = $name ? $name : $file->name;
        if ($this->MD5Name) {
            $ext = pathinfo($name, PATHINFO_EXTENSION);
            $name = md5(str_replace("." . $ext, "", $name)) . '.' . $ext;
        }

        if ($name) {
            $this->value = $this->makeFilePath($name);
            $fileContent = $file->getContent();

            if ($this->getStorage() instanceof FileSystemStorage) {
                try {
                    $image = $this->getImagine()->load($fileContent);
                } catch (Exception $e) {
                    $image = null;
                }
                if ($image) {
                    $fileContent = $this->processSource($image);
                    if ($this->storeOriginal) {
                        $this->value = $this->getStorage()->save($this->value, $fileContent);
                    }
                } else {
                    $this->value = null;
                }
            } elseif ($this->getStorage() instanceof IExternalStorage) {
                $this->value = $this->getStorage()->save($this->value, $fileContent);
            }
        }

        return $this->value;
    }

    public function deleteOld()
    {
        if ($this->getOldValue()) {
            $this->getStorage()->delete($this->getOldValue());
            foreach (array_keys($this->sizes) as $prefix) {
                $this->getStorage()->delete($this->sizeStoragePath($prefix, $this->getOldValue()));
            }
        }
    }

    /**
     * @param $source
     * @param bool $force
     * @param array|null $onlySizes - Resize and save only sizes, described in this array
     * @return
     */
    public function processSource($source, $force = false, $onlySizes = null)
    {
        $ext = pathinfo($this->value, PATHINFO_EXTENSION);
        foreach ($this->sizes as $prefix => $size) {
            if (is_array($onlySizes) && !in_array($prefix, $onlySizes)) {
                continue;
            }
            $width = isset($size[0]) ? $size[0] : null;
            $height = isset($size[1]) ? $size[1] : null;
            if (!$width || !$height) {
                list($width, $height) = $this->imageScale($source, $width, $height);
            }
            $method = isset($size['method']) ? $size['method'] : $this->defaultResize;
            if (!in_array($method, $this->availableResizeMethods)) {
                throw new Exception('Unknown resize method: ' . $method);
            }
            $options = isset($size['options']) ? $size['options'] : $this->options;

            $watermark = isset($size['watermark']) ? $size['watermark'] : $this->watermark;
            if (($width || $height) && $method) {
{'fraction_non_alphanumeric': 0.15184403577529895, 'fraction_numerical': 0.006029544769369913, 'mean_word_length': 1.868838281925627, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 102, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3611540', 'n_tokens_mistral': 3047, 'n_tokens_neox': 2973, 'n_words': 623}