<commit_before><?php

namespace admin\storage;

use Yii;
use Exception;
use Imagine\Gd\Imagine;
use admin\models\StorageImage;
use admin\models\StorageFilter;
use luya\helpers\ArrayHelper;

class Image
{
    public function create($fileId, $filterId = 0)
    {
        // find image for file_id and filter_id
        $img = StorageImage::find()->where(['file_id' => $fileId, 'filter_id' => $filterId])->asArray()->one();

        // the image exists already, we return the already created image id
        if ($img) {
            return $img['id'];
        }

        $file = Yii::$app->storage->file->getPath($fileId);

        if (!$file) {
            return false;
        }

        $info = Yii::$app->storage->file->getInfo($fileId);

        try {
            $imagine = new Imagine();
            $image = $imagine->open($file);
        } catch (Exception $e) {
            return $e->getMessage();
        }

        $fileName = $filterId.'_'.$info->name_new_compound;

        if (empty($filterId)) {
            $save = $image->save(Yii::$app->storage->dir.$fileName);
        } else {
            $model = StorageFilter::find()->where(['id' => $filterId])->one();
            if (!$model) {
                throw new Exception("Could not find the provided filter id '$filterId'.");
            }
            $newimage = $model->applyFilter($image, $imagine);
            $save = $newimage->save(Yii::$app->storage->dir.$fileName);
        }

        if ($save) {
            $model = new StorageImage();
            $model->setAttributes([
                'file_id' => $fileId,
                'filter_id' => $filterId,
            ]);
            if ($model->save()) {
                return $model->id;
            }
        }

        return false;
    }

    /**
     * see if the filter for this image already has been applyd, yes return the image_source, otherwise apply
     * filter and return the new image_source.
     *
     * @param int    $imageId
     * @param string $filterIdentifier
     */
    public function filterApply($imageId, $filterIdentifier, $returnImageObject = false)
    {
        // resolve $filterIdentifier
        $filter = StorageFilter::find()->where(['identifier' => $filterIdentifier])->asArray()->one();
        if (!$filter) {
            throw new \Exception('could not find the filterIdentifier '.$filterIdentifier);
        }
        $filterId = $filter['id'];

        $image = $this->get($imageId);
        if (!$image) {
            return false;
        }

        $data = (new \yii\db\Query())->from('admin_storage_image')->where(['file_id' => $image->file_id, 'filter_id' => $filterId])->one();

        if ($data) {
            $imageId = $data['id'];
        } else {
            $imageId = $this->create($image->file_id, $filterId);
        }

        $imageObject = $this->get($imageId);

        if (!$imageObject) {
            throw new Exception("Image '$imageId' does not exist in database or storage system.");
        }

        if ($returnImageObject) {
            return $imageObject;
        }

        return $imageObject->source;
    }

    public function getSource($imageId)
    {
        return ($image = $this->get($imageId)) ? $image->source : '';
    }
<commit_msg>do not throw exception if image does not exists closes #465<commit_after><?php

namespace admin\storage;

use Yii;
use Exception;
use Imagine\Gd\Imagine;
use admin\models\StorageImage;
use admin\models\StorageFilter;
use luya\helpers\ArrayHelper;

class Image
{
    public function create($fileId, $filterId = 0)
    {
        // find image for file_id and filter_id
        $img = StorageImage::find()->where(['file_id' => $fileId, 'filter_id' => $filterId])->asArray()->one();

        // the image exists already, we return the already created image id
        if ($img) {
            return $img['id'];
        }

        $file = Yii::$app->storage->file->getPath($fileId);

        if (!$file) {
            return false;
        }

        $info = Yii::$app->storage->file->getInfo($fileId);

        try {
            $imagine = new Imagine();
            $image = $imagine->open($file);
        } catch (Exception $e) {
            return $e->getMessage();
        }

        $fileName = $filterId.'_'.$info->name_new_compound;

        if (empty($filterId)) {
            $save = $image->save(Yii::$app->storage->dir.$fileName);
        } else {
            $model = StorageFilter::find()->where(['id' => $filterId])->one();
            if (!$model) {
                throw new Exception("Could not find the provided filter id '$filterId'.");
            }
            $newimage = $model->applyFilter($image, $imagine);
            $save = $newimage->save(Yii::$app->storage->dir.$fileName);
        }

        if ($save) {
            $model = new StorageImage();
            $model->setAttributes([
                'file_id' => $fileId,
                'filter_id' => $filterId,
            ]);
            if ($model->save()) {
                return $model->id;
            }
        }

        return false;
    }

    /**
     * see if the filter for this image already has been applyd, yes return the image_source, otherwise apply
     * filter and return the new image_source.
     *
     * @param int    $imageId
     * @param string $filterIdentifier
     */
    public function filterApply($imageId, $filterIdentifier, $returnImageObject = false)
    {
        // resolve $filterIdentifier
        $filter = StorageFilter::find()->where(['identifier' => $filterIdentifier])->asArray()->one();
        if (!$filter) {
            throw new \Exception('could not find the filterIdentifier '.$filterIdentifier);
        }
        $filterId = $filter['id'];

        $image = $this->get($imageId);
        if (!$image) {
            return false;
        }

        $data = (new \yii\db\Query())->from('admin_storage_image')->where(['file_id' => $image->file_id, 'filter_id' => $filterId])->one();

        if ($data) {
            $imageId = $data['id'];
        } else {
            $imageId = $this->create($image->file_id, $filterId);
        }

        $imageObject = $this->get($imageId);

        if (!$imageObject) {
            return false;
        }

        if ($returnImageObject) {
            return $imageObject;
        }

        return $imageObject->source;
    }

    public function getSource($imageId)
    {
        return ($image = $this->get($imageId)) ? $image->source : '';
    }

{'fraction_non_alphanumeric': 0.15561736980373977, 'fraction_numerical': 0.0007726781023025807, 'mean_word_length': 1.985239852398524, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 95, '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': '4715894', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1834, 'n_words': 422}