<commit_before><?php
/**
 * Created by PhpStorm.
 * User: gseidel
 * Date: 11.11.17
 * Time: 12:09
 */

namespace Enhavo\Bundle\MediaBundle\Filter\Filter;

use Enhavo\Bundle\MediaBundle\Content\ContentInterface;
use Enhavo\Bundle\MediaBundle\Filter\AbstractFilter;
use Enhavo\Bundle\MediaBundle\Model\FilterSetting;
use Imagine\Exception\RuntimeException;
use Imagine\Gd\Imagine;
use Imagine\Imagick\Imagine as ImagickImagine;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;

class ImageFilter extends AbstractFilter
{
    public function apply($file, FilterSetting $setting)
    {
        $content = $this->getContent($file);

        try {
            $format = $this->getFormat($content, $setting);
            if($format == 'gif' && class_exists('Imagick') && $this->isAnimatedGif($content->getContent())) {
                $imagine = new ImagickImagine();
                $image = $imagine->load($content->getContent());
                $image->layers()->coalesce();
                $resultImage = $image->copy();
                $resultImage = $this->resize($resultImage, $setting);
                $options = $this->getSaveOptions($format, $setting);
                $options['animated'] = true;
                $resultImage->save($content->getFilePath(), $options);
            } elseif($format == 'bmp' && class_exists('Imagick') ) {
                $imagine = new ImagickImagine();
                $image = $imagine->load($content->getContent());
                $resultImage = $image->copy();
                $resultImage = $this->resize($resultImage, $setting);
                $resultImage->save($content->getFilePath(), $this->getSaveOptions($format, $setting));
            } else {
                $imagine = new Imagine();
                $imagine = $imagine->load($content->getContent());
                $imagine = $this->resize($imagine, $setting);
                $imagine->save($content->getFilePath(), $this->getSaveOptions($format, $setting));
            }
        } catch (RuntimeException $e) {
            // if image cant be created we make an empty file
            file_put_contents($content->getFilePath(), '');
        }
    }

    /**
     * @param $format
     * @param FilterSetting $setting
     * @return array
     */<commit_msg>[MediaBundle] Default to using Imagick library if available in ImageFilter<commit_after><?php
/**
 * Created by PhpStorm.
 * User: gseidel
 * Date: 11.11.17
 * Time: 12:09
 */

namespace Enhavo\Bundle\MediaBundle\Filter\Filter;

use Enhavo\Bundle\MediaBundle\Content\ContentInterface;
use Enhavo\Bundle\MediaBundle\Filter\AbstractFilter;
use Enhavo\Bundle\MediaBundle\Model\FilterSetting;
use Imagine\Exception\RuntimeException;
use Imagine\Gd\Imagine;
use Imagine\Imagick\Imagine as ImagickImagine;
use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;

class ImageFilter extends AbstractFilter
{
    public function apply($file, FilterSetting $setting)
    {
        $content = $this->getContent($file);

        try {
            $format = $this->getFormat($content, $setting);
            if($format == 'gif' && class_exists('Imagick') && $this->isAnimatedGif($content->getContent())) {
                $imagine = new ImagickImagine();
                $image = $imagine->load($content->getContent());
                $image->layers()->coalesce();
                $resultImage = $image->copy();
                $resultImage = $this->resize($resultImage, $setting);
                $options = $this->getSaveOptions($format, $setting);
                $options['animated'] = true;
                $resultImage->save($content->getFilePath(), $options);
            } elseif($format == 'bmp' && class_exists('Imagick') ) {
                $imagine = new ImagickImagine();
                $image = $imagine->load($content->getContent());
                $resultImage = $image->copy();
                $resultImage = $this->resize($resultImage, $setting);
                $resultImage->save($content->getFilePath(), $this->getSaveOptions($format, $setting));
            } elseif(class_exists('Imagick') ) {
                $imagine = new ImagickImagine();
                $imagine = $imagine->load($content->getContent());
                $imagine = $this->resize($imagine, $setting);
                $imagine->save($content->getFilePath(), $this->getSaveOptions($format, $setting));
            } else {
                $imagine = new Imagine();
                $imagine = $imagine->load($content->getContent());
                $imagine = $this->resize($imagine, $setting);
                $imagine->save($content->getFilePath(), $this->getSaveOptions($format, $setting));
            }
        } catch (RuntimeException $e) {
            // if image cant be created we make an empty file
            file_put_contents($content->getFilePath(), '');
        }
    }

    /**
     * @param $format
     * @param FilterSetting $setting
     * @return array
     */
{'fraction_non_alphanumeric': 0.15099940629329112, 'fraction_numerical': 0.003958044725905403, 'mean_word_length': 2.5, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '5042426', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1375, 'n_words': 261}