<commit_before><?php

namespace pendalf89\imageresizer;

use Imagine\Image\ManipulatorInterface;
use Yii;
use yii\base\Component;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use yii\imagine\Image;

/**
 * Class ImageResizer
 * Class for creation thumbs from original image.
 *
 * @package pendalf89\imageresizer
 */
class ImageResizer extends Component
{
	/**
	 * @var array image sizes
	 * For example:
	 *  [
	 *        ['width' => 300, 'height' => 200, 'suffix' => 'md'],
	 *        ['width' => 300, 'height' => 100, 'suffix' => 'sm'],
	 *        ['width' => 200, 'height' => 50],
	 *    ]
	 * 
	 * If 'suffix' not set, than width and height be used for suffix name.
	 */
	public $sizes;

	/**
	 * @var string directory with images
	 */
	public $dir;

	/**
	 * @var bool handle directory recursively
	 */
	public $recursively = true;

	/**
	 * @var bool enable rewrite thumbs if its already exists
	 */
	public $enableRewrite = true;

	/**
	 * @var bool enable to delete all images, which sizes not in $this->sizes array.
	 * If true, all thumbs for image will be deleted before resize.
	 */
	public $deleteNonActualSizes = false;

	/**
	 * @var array|string the driver to use. This can be either a single driver name or an array of driver names.
	 * If the latter, the first available driver will be used.
	 *
	 * For more information see yii\imagine\BaseImage
	 */
	public $driver = [Image::DRIVER_GMAGICK, Image::DRIVER_IMAGICK, Image::DRIVER_GD2];

	/**
	 * @var string image creation mode.
	 * For more information see Imagine\Image\ManipulatorInterface
	 * Available values: inset, outset
	 */
	public $mode = ManipulatorInterface::THUMBNAIL_INSET;

	/**
	 * @var string background alpha (transparency) to use when creating thumbnails in `ImageInterface::THUMBNAIL_INSET`
	 * mode with both width and height specified. Default is solid.
	 */
	public $thumbnailBackgroundAlpha = 0;

	/**
	 * Create work directory, if it not exists.
	 */
	public function init()
	{
		$dir = $this->getDirectory();
		if (!is_null($dir) && !is_dir($dir)) {
			$this->createDirectory();
		}
	}

	/**
	 * Resize and save thumbnails from all images.
	 */
	public function resizeAll()
	{
		$filenames = $this->collectFilenames();

		foreach ($filenames as $filename) {
			if ($this->isOriginal($filename)) {
				$this->resize($filename);
			}
		}
	}

	/**
	 * Resize image and save thumbnails.
	 * If rewrite disabled and thumbnail already exists, than skip.
	 *
	 * @param string $filename image filename
	 */
	public function resize($filename)
	{
		Image::$driver = $this->driver;

		if ($this->deleteNonActualSizes) {
			$this->deleteFiles($this->getThumbs($filename));
		}

		foreach ($this->sizes as $size) {
			$newFilename = $this->addSuffix($filename, $this->getSuffixBySize($size));

			if (!$this->enableRewrite && file_exists($newFilename)) {
				continue;
			}

			Image::$thumbnailBackgroundAlpha = 0;
			Image::thumbnail($filename, $size['width'], $size['height'], $this->mode)->save($newFilename);
		}
	}

	/**
	 * Search all thumbs by original image filename
	 *<commit_msg>Fix $thumbnailBackgroundAlpha param<commit_after><?php

namespace pendalf89\imageresizer;

use Imagine\Image\ManipulatorInterface;
use Yii;
use yii\base\Component;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use yii\imagine\Image;

/**
 * Class ImageResizer
 * Class for creation thumbs from original image.
 *
 * @package pendalf89\imageresizer
 */
class ImageResizer extends Component
{
	/**
	 * @var array image sizes
	 * For example:
	 *  [
	 *        ['width' => 300, 'height' => 200, 'suffix' => 'md'],
	 *        ['width' => 300, 'height' => 100, 'suffix' => 'sm'],
	 *        ['width' => 200, 'height' => 50],
	 *    ]
	 *
	 * If 'suffix' not set, than width and height be used for suffix name.
	 */
	public $sizes;

	/**
	 * @var string directory with images
	 */
	public $dir;

	/**
	 * @var bool handle directory recursively
	 */
	public $recursively = true;

	/**
	 * @var bool enable rewrite thumbs if its already exists
	 */
	public $enableRewrite = true;

	/**
	 * @var bool enable to delete all images, which sizes not in $this->sizes array.
	 * If true, all thumbs for image will be deleted before resize.
	 */
	public $deleteNonActualSizes = false;

	/**
	 * @var array|string the driver to use. This can be either a single driver name or an array of driver names.
	 * If the latter, the first available driver will be used.
	 *
	 * For more information see yii\imagine\BaseImage
	 */
	public $driver = [Image::DRIVER_GMAGICK, Image::DRIVER_IMAGICK, Image::DRIVER_GD2];

	/**
	 * @var string image creation mode.
	 * For more information see Imagine\Image\ManipulatorInterface
	 * Available values: inset, outset
	 */
	public $mode = ManipulatorInterface::THUMBNAIL_INSET;

	/**
	 * @var string background alpha (transparency) to use when creating thumbnails in `ImageInterface::THUMBNAIL_INSET`
	 * mode with both width and height specified. Default is solid.
	 */
	public $thumbnailBackgroundAlpha = 0;

	/**
	 * Create work directory, if it not exists.
	 */
	public function init()
	{
		$dir = $this->getDirectory();
		if (!is_null($dir) && !is_dir($dir)) {
			$this->createDirectory();
		}
	}

	/**
	 * Resize and save thumbnails from all images.
	 */
	public function resizeAll()
	{
		$filenames = $this->collectFilenames();

		foreach ($filenames as $filename) {
			if ($this->isOriginal($filename)) {
				$this->resize($filename);
			}
		}
	}

	/**
	 * Resize image and save thumbnails.
	 * If rewrite disabled and thumbnail already exists, than skip.
	 *
	 * @param string $filename image filename
	 */
	public function resize($filename)
	{
		Image::$driver = $this->driver;

		if ($this->deleteNonActualSizes) {
			$this->deleteFiles($this->getThumbs($filename));
		}

		foreach ($this->sizes as $size) {
			$newFilename = $this->addSuffix($filename, $this->getSuffixBySize($size));

			if (!$this->enableRewrite && file_exists($newFilename)) {
				continue;
			}

			Image::$thumbnailBackgroundAlpha = $this->thumbnailBackgroundAlpha;
			Image::thumbnail($filename, $size['width'], $size['height'], $this->mode)->save($newFilename);
		}
	}

	/**
	 * Search all thumbs by original image filename
	 *
{'fraction_non_alphanumeric': 0.13656034620932841, 'fraction_numerical': 0.007533258535021638, 'mean_word_length': 3.5983787767133384, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 52, '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': '1017983', 'n_tokens_mistral': 2155, 'n_tokens_neox': 1892, 'n_words': 580}