<commit_before><?php
/**
 * phlexible
 *
 * @copyright 2007-2013 brainbits GmbH (http://www.brainbits.net)
 * @license   proprietary
 */

namespace Phlexible\Component\MediaTemplate\Applier;

use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Palette\CMYK;
use Imagine\Image\Palette\Grayscale;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;
use Phlexible\Component\MediaManager\Volume\ExtendedFileInterface;
use Phlexible\Component\MediaTemplate\Model\ImageTemplate;
use Psr\Log\LoggerInterface;

/**
 * Image cache worker
 *
 * @author <NAME> <<EMAIL>>
 */
class ImageTemplateApplier
{
    /**
     * @var ImagineInterface
     */
    private $imagine;

    /**
     * @param ImagineInterface $imagine
     */
    public function __construct(ImagineInterface $imagine)
    {
        $this->imagine = $imagine;
    }

    /**
     * @param string $filename
     *
     * @return bool
     */
    public function isAvailable($filename)
    {
        return true;
    }

    /**
     * @return LoggerInterface
     */
    public function getLogger()
    {
        return null;
    }

    /**
     * @param ImageTemplate $template
     *
     * @return string
     */
    public function getExtension(ImageTemplate $template)
    {
        if ($template->getParameter('format')) {
            return $template->getParameter('format');
        } else {
            return 'jpg';
        }
    }

    /**
     * @param ImageTemplate $template
     *
     * @return string
     */
    public function getMimetype(ImageTemplate $template)
    {
        if ($template->getParameter('format')) {
            return 'image/' . $template->getParameter('format');
        } else {
            return 'image/jpg';
        }
    }

    /**
     * @param ImageTemplate         $template
     * @param ExtendedFileInterface $file
     * @param string                $filename
     * @param string                $outFilename
     *
     * @return ImageInterface
     */
    public function apply(ImageTemplate $template, ExtendedFileInterface $file, $filename, $outFilename)
    {
        $image = $this->imagine->open($filename);
        if ($image->layers()->count() > 1) {
            // workaround for multi-page pdfs
            // might be removed when https://github.com/avalanche123/Imagine/pull/451 is merged
            $tmpFilename = sys_get_temp_dir().'/'.basename($filename);
            $image->layers()->get(0)->save($tmpFilename.".jpg");
            $image = $this->imagine->open($tmpFilename.".jpg");
        }

        $options = array();

        if ($template->hasParameter('format', true)) {
            $options['format'] = $template->getParameter('format');
        }

        if ($template->hasParameter('quality', true)) {
            if (!empty($options['format']) && $options['format'] === 'png') {
                $options['png_compression_level'] = floor($template->getParameter('quality') / 10);
                $options['png_compression_filter'] = $template->getParameter('quality') % 10;
            } elseif (!empty($options['format']) && $options['format'] === 'jpg') {
                $options['jpeg_quality'] = $template->getParameter('quality');
            }
        }

        if ($template->hasParameter('tiffcompression', true)) {
            //$image->setTiffCompression($template->getParameter('tiffcompression'));
        }

        if ($template->hasParameter('for_web', true) && $template->getParameter('for_web')) {
            $image->strip();

            if ($template->hasParameter('colorspace')) {
                $colorspace = $template->getParameter('colorspace');
                if ($colorspace === 'grayscale') {
                    $image->usePalette(new Grayscale());
                } else {
                    $image->usePalette(new RGB());
                }
            }
        } elseif ($template->hasParameter('colorspace', true)) {
            $colorspace = $template->getParameter('colorspace');
            if ($colorspace === 'grayscale') {
                $image->usePalette(new Grayscale());
            } elseif ($colorspace === 'cmyk') {
                $image->usePalette(new CMYK());
            } else {
                $image->usePalette(new RGB());
            }
        }

        if ($template->hasParameter('scale', true)) {
            if ($template->getParameter('scale') === 'up') {
                // only scale up
            } elseif ($template->getParameter('scale') === 'down') {
                // only scale down
            }
        }

        $method = $template->getParameter('method');

        if ($method === 'width') {
            $templateSize = $image->getSize()->widen($template->getParameter('width'));
            $image->resize($templateSize);
        } elseif ($method === 'height') {
            $templateSize = $image->getSize()->heighten($template->getParameter('height'));
            $image->resize($templateSize);
        } elseif ($method === 'exact') {
            $templateSize = new Box($template->getParameter('width'), $template->getParameter('height'));
            $image->resize($templateSize);
        } elseif ($method === 'fit') {
            $templateSize = new Box($template->getParameter('width'), $template->getParameter('height'));
            $image = $image->thumbnail($templateSize, ImageInterface::THUMBNAIL_INSET);
        } elseif ($method === 'exactFit') {<commit_msg>BBPHX-49 fix grayscale conversion<commit_after><?php
/**
 * phlexible
 *
 * @copyright 2007-2013 brainbits GmbH (http://www.brainbits.net)
 * @license   proprietary
 */

namespace Phlexible\Component\MediaTemplate\Applier;

use Imagine\Image\Box;
use Imagine\Image\ImageInterface;
use Imagine\Image\ImagineInterface;
use Imagine\Image\Palette\CMYK;
use Imagine\Image\Palette\Grayscale;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;
use Phlexible\Component\MediaManager\Volume\ExtendedFileInterface;
use Phlexible\Component\MediaTemplate\Model\ImageTemplate;
use Psr\Log\LoggerInterface;

/**
 * Image cache worker
 *
 * @author <NAME> <<EMAIL>>
 */
class ImageTemplateApplier
{
    /**
     * @var ImagineInterface
     */
    private $imagine;

    /**
     * @param ImagineInterface $imagine
     */
    public function __construct(ImagineInterface $imagine)
    {
        $this->imagine = $imagine;
    }

    /**
     * @param string $filename
     *
     * @return bool
     */
    public function isAvailable($filename)
    {
        return true;
    }

    /**
     * @return LoggerInterface
     */
    public function getLogger()
    {
        return null;
    }

    /**
     * @param ImageTemplate $template
     *
     * @return string
     */
    public function getExtension(ImageTemplate $template)
    {
        if ($template->getParameter('format')) {
            return $template->getParameter('format');
        } else {
            return 'jpg';
        }
    }

    /**
     * @param ImageTemplate $template
     *
     * @return string
     */
    public function getMimetype(ImageTemplate $template)
    {
        if ($template->getParameter('format')) {
            return 'image/' . $template->getParameter('format');
        } else {
            return 'image/jpg';
        }
    }

    /**
     * @param ImageTemplate         $template
     * @param ExtendedFileInterface $file
     * @param string                $filename
     * @param string                $outFilename
     *
     * @return ImageInterface
     */
    public function apply(ImageTemplate $template, ExtendedFileInterface $file, $filename, $outFilename)
    {
        $image = $this->imagine->open($filename);
        if ($image->layers()->count() > 1) {
            // workaround for multi-page pdfs
            // might be removed when https://github.com/avalanche123/Imagine/pull/451 is merged
            $tmpFilename = sys_get_temp_dir().'/'.basename($filename);
            $image->layers()->get(0)->save($tmpFilename.".jpg");
            $image = $this->imagine->open($tmpFilename.".jpg");
        }

        $options = array();

        if ($template->hasParameter('format', true)) {
            $options['format'] = $template->getParameter('format');
        }

        if ($template->hasParameter('quality', true)) {
            if (!empty($options['format']) && $options['format'] === 'png') {
                $options['png_compression_level'] = floor($template->getParameter('quality') / 10);
                $options['png_compression_filter'] = $template->getParameter('quality') % 10;
            } elseif (!empty($options['format']) && $options['format'] === 'jpg') {
                $options['jpeg_quality'] = $template->getParameter('quality');
            }
        }

        if ($template->hasParameter('tiffcompression', true)) {
            //$image->setTiffCompression($template->getParameter('tiffcompression'));
        }

        if ($template->hasParameter('for_web', true) && $template->getParameter('for_web')) {
            $image->strip();

            if ($template->hasParameter('colorspace')) {
                $colorspace = $template->getParameter('colorspace');
                if ($colorspace === 'grayscale' || $colorspace === 'gray') {
                    $image->usePalette(new Grayscale());
                } else {
                    $image->usePalette(new RGB());
                }
            }
        } elseif ($template->hasParameter('colorspace', true)) {
            $colorspace = $template->getParameter('colorspace');
            if ($colorspace === 'grayscale' || $colorspace === 'gray') {
                $image->usePalette(new Grayscale());
            } elseif ($colorspace === 'cmyk') {
                $image->usePalette(new CMYK());
            } else {
                $image->usePalette(new RGB());
            }
        }

        if ($template->hasParameter('scale', true)) {
            if ($template->getParameter('scale') === 'up') {
                // only scale up
            } elseif ($template->getParameter('scale') === 'down') {
                // only scale down
            }
        }

        $method = $template->getParameter('method');

        if ($method === 'width') {
            $templateSize = $image->getSize()->widen($template->getParameter('width'));
            $image->resize($templateSize);
        } elseif ($method === 'height') {
            $templateSize = $image->getSize()->heighten($template->getParameter('height'));
            $image->resize($templateSize);
        } elseif ($method === 'exact') {
            $templateSize = new Box($template->getParameter('width'), $template->getParameter('height'));
            $image->resize($templateSize);
        } elseif ($method === 'fit') {
            $templateSize = new Box($template->getParameter('width'), $template->getParameter('height'));
            $image = $image->thumbnail($templateSize, ImageInterface::THUMBNAIL_INSET);
        } elseif ($method === 'exactFit') {
{'fraction_non_alphanumeric': 0.15878070973612374, 'fraction_numerical': 0.003821656050955414, 'mean_word_length': 2.220334016993847, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 117, 'https://': 2, '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': '5013446', 'n_tokens_mistral': 3038, 'n_tokens_neox': 2917, 'n_words': 527}