<commit_before><?php

namespace Imagine\GD\Command;

use Imagine\GD\Utils;

/**
 * GD resize command
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Resize implements \Imagine\Command
{
    const INFER_HEIGHT = 1;
    const INFER_WIDTH = 2;
    const AR_AROUND = 3;
    const AR_WITHIN = 4;

    /**
     * Resized image width
     *
     * @var int
     */
    protected $width;

    /**
     * Resized image height
     *
     * @var int
     */
    protected $height;

    /**
     * Resize mode
     *
     * @var int
     */
    protected $mode;

    /**
     * Constructs a resize operation.
     *
     * One of the following mode constants may optionally be specified:
     *
     *  - INFER_HEIGHT: Calculate the resize height based on the given width and
     *                  aspect ratio of the processed image.
     *  - INFER_WIDTH:  Calculate the resize width based on the given height and
     *                  aspect ratio of the processed image.
     *  - AR_AROUND:    Resize the image to the smallest size around the given
     *                  width and height, while still preserving aspect ratio.
     *  - AR_WITHIN:    Resize the image to the largest size within the given
     *                  width and height, while still preserving aspect ratio.
     *
     * @param int $width
     * @param int $height
     * @param int $mode
     * @throws \InvalidArgumentException
     */
    public function __construct($width, $height, $mode = null)
    {
        $this->width = (int) $width;
        $this->height = (int) $height;
        $this->mode = $mode;

        if (isset($mode) && !in_array($mode, array(self::INFER_HEIGHT, self::INFER_WIDTH, self::AR_AROUND, self::AR_WITHIN))) {
            throw new \InvalidArgumentException('Invalid mode parameter: ' . $mode);
        }
    }

    /**
     * Process the resize operation.
     *
     * @param \Imagine\Image $image
     * @throws \RuntimeException
     */
    public function process(\Imagine\Image $image)
    {
        if ($this->mode) {
            $this->adjustSize($image);
        }

        $srcImage = $image->getResource();
        $dstImage = Utils::createResource($this->width, $this->height, $image->getType());

        if (! imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $this->width, $this->height, $image->getWidth(), $image->getHeight())) {
            throw new \RuntimeException('Could not resize the image');
        }
        $image->setResource($dstImage);
    }

    /**
     * If either width or height is boolean true, calculate its integer value
     * from the other dimension and the image's aspect ratio.
     *
     * @param \Imagine\Image $image
     */
    private function adjustSize(\Imagine\Image $image)
    {
        switch ($this->mode) {
            case self::INFER_HEIGHT:
                $this->height = intval($this->width * $image->getHeight() / $image->getWidth());
                break;

            case self::INFER_WIDTH:
                $this->width = intval($this->height * $image->getWidth() / $image->getHeight());
                break;

            case self::AR_AROUND:
                list($this->width, $this->height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, true);
                break;

            case self::AR_WITHIN:
                list($this->width, $this->height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, false);
                break;
        }
    }
}
<commit_msg>Preserve Resize command state when processing several images<commit_after><?php

namespace Imagine\GD\Command;

use Imagine\GD\Utils;

/**
 * GD resize command
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
class Resize implements \Imagine\Command
{
    const INFER_HEIGHT = 1;
    const INFER_WIDTH = 2;
    const AR_AROUND = 3;
    const AR_WITHIN = 4;

    /**
     * Resized image width
     *
     * @var int
     */
    protected $width;

    /**
     * Resized image height
     *
     * @var int
     */
    protected $height;

    /**
     * Resize mode
     *
     * @var int
     */
    protected $mode;

    /**
     * Constructs a resize operation.
     *
     * One of the following mode constants may optionally be specified:
     *
     *  - INFER_HEIGHT: Calculate the resize height based on the given width and
     *                  aspect ratio of the processed image.
     *  - INFER_WIDTH:  Calculate the resize width based on the given height and
     *                  aspect ratio of the processed image.
     *  - AR_AROUND:    Resize the image to the smallest size around the given
     *                  width and height, while still preserving aspect ratio.
     *  - AR_WITHIN:    Resize the image to the largest size within the given
     *                  width and height, while still preserving aspect ratio.
     *
     * @param int $width
     * @param int $height
     * @param int $mode
     * @throws \InvalidArgumentException
     */
    public function __construct($width, $height, $mode = null)
    {
        $this->width = (int) $width;
        $this->height = (int) $height;
        $this->mode = $mode;

        if (isset($mode) && !in_array($mode, array(self::INFER_HEIGHT, self::INFER_WIDTH, self::AR_AROUND, self::AR_WITHIN))) {
            throw new \InvalidArgumentException('Invalid mode parameter: ' . $mode);
        }
    }

    /**
     * Process the resize operation.
     *
     * @param \Imagine\Image $image
     * @throws \RuntimeException
     */
    public function process(\Imagine\Image $image)
    {
        list($width, $height) = $this->adjustSize($image);

        $srcImage = $image->getResource();
        $dstImage = Utils::createResource($width, $height, $image->getType());

        if (! imagecopyresampled($dstImage, $srcImage, 0, 0, 0, 0, $width, $height, $image->getWidth(), $image->getHeight())) {
            throw new \RuntimeException('Could not resize the image');
        }
        $image->setResource($dstImage);
    }

    /**
     * If either width or height is boolean true, calculate its integer value
     * from the other dimension and the image's aspect ratio.
     *
     * @param \Imagine\Image $image
     * @return array width, height
     */
    private function adjustSize(\Imagine\Image $image)
    {
        switch ($this->mode) {
            case self::INFER_HEIGHT:
                $width  = $this->width;
                $height = intval($this->width * $image->getHeight() / $image->getWidth());
                break;

            case self::INFER_WIDTH:
                $width  = intval($this->height * $image->getWidth() / $image->getHeight());
                $height = $this->height;
                break;

            case self::AR_AROUND:
                list($width, $height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, true);
                break;

            case self::AR_WITHIN:
                list($width, $height) = Utils::getBoxForAspectRatio($image->getWidth() / $image->getHeight(), $this->width, $this->height, false);
                break;

            default:
                $width  = $this->width;
                $height = $this->height;
                break;
        }

        return array($width, $height);
    }
}

{'fraction_non_alphanumeric': 0.12755033103634644, 'fraction_numerical': 0.002161870017565194, 'mean_word_length': 2.021224489795918, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 78, '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': '2249342', 'n_tokens_mistral': 2193, 'n_tokens_neox': 2083, 'n_words': 582}