<commit_before><?php

class CM_File_Image extends CM_File {
	const QUALITY_JPEG = 95;

	/**
	 * @var int
	 */
	private $_imageType;

	/**
	 * @var resource
	 */
	private $_resource;

	public function __construct($file) {
		parent::__construct($file);

		$this->_getResource(); // Make sure resource can be created
	}

	public function __destruct() {
		imagedestroy($this->_resource);
	}

	/**
	 * @param int		 $type	Image type. Use IMAGETYPE_* consts
	 * @param string|null $newPath Where the resulting image should be written
	 */
	public function convert($type, $newPath = null) {
		if ($this->_getImageType() == $type) {
			// Copy image if no conversion necessary
			if (isset($newPath)) {
				$this->copy($newPath);
			}
			return;
		}

		$this->_writeResource($this->_resource, $newPath, $type);
		@chmod($newPath, 0777);
	}

	/**
	 * @param int	$widthMax   New width
	 * @param int	$heightMax  New height
	 * @param bool   $square     True if result image should be a square
	 * @param string $pathNew    OPTIONAL If set, image is stored a new location
	 */
	public function resize($widthMax, $heightMax, $square = false, $pathNew = null) {
		$width = $this->getWidth();
		$height = $this->getHeight();

		if (($width == $widthMax) && ($height == $heightMax)) {
			if (isset($pathNew)) {
				$this->copy($pathNew);
			}
			return;
		}

		$resource = $this->_getResource();
		if ($square) {
			$resource = $this->_getResourceSquare();
			$width = $height = ($width < $height ? $width : $height);
		}

		if (($width > $widthMax) || ($height > $heightMax)) {
			if ($height / $heightMax > $width / $widthMax) {
				$scale_coef = $heightMax / $height;
			} else {
				$scale_coef = $widthMax / $width;
			}
			$heightNew = $height * $scale_coef;
			$widthNew = $width * $scale_coef;
		} else {
			// Don't blow image up
			$heightNew = $height;
			$widthNew = $width;
		}

		$resourceNew = imagecreatetruecolor($widthNew, $heightNew);
		$result = imagecopyresampled($resourceNew, $resource, 0, 0, 0, 0, $widthNew, $heightNew, $width, $height);
		if (!$result) {
			throw new CM_Exception_Invalid('Cannot resample image');
		}
		$this->_writeResource($resourceNew, $pathNew);
		@chmod($pathNew, 0777);
	}

	/**
	 * @param int		 $angle   Angle to rotate the image
	 * @param string|null $pathNew OPTIONAL New path for the rotate image
	 * @throws CM_Exception_Invalid If something goes wrong during rotation or conversion
	 */
	public function rotate($angle, $pathNew = null) {
		$resource = imagerotate($this->_resource, $angle + 180, 0);
		if (!$resource) {
			throw new CM_Exception_Invalid('Cannot rotate image');
		}
		$this->_writeResource($resource, $pathNew);
	}

	/**
	 * @return int
	 */
	public function getWidth() {
		$width = imagesx($this->_resource);
		if (false === $width) {
			throw new CM_Exception_Invalid('Cannot detect image width');
		}
		return (int) $width;
	}

	/**
	 * @return int
	 */
	public function getHeight() {
		$height = imagesy($this->_resource);
		if (false === $height) {
			throw new CM_Exception_Invalid('Cannot detect image height');
		}
		return (int) $height;
	}

	/**
	 * @return resource
	 * @throws CM_Exception_Invalid
	 */
	private function _getResourceSquare() {
		$width = $this->getWidth();
		$height = $this->getHeight();
		if ($width == $height) {
			return $this->_getResource();
		}

		if ($width > $height) {
			$offsetX = ($width - $height) / 2;
			$offsetY = 0;
			$size = $height;
		} else {
			$offsetX = 0;
			$offsetY = ($height - $width) / 2;
			$size = $width;
		}

		$resource = imagecreatetruecolor($size, $size);
		$result = imagecopyresampled($resource, $this->_getResource(), 0, 0, $offsetX, $offsetY, $width, $height, $width, $height);
		if (!$result) {
			throw new CM_Exception_Invalid('Cannot resample image');
		}
		return $resource;
	}

	/**
	 * @return int
	 * @throws CM_Exception_Invalid
	 */
	private function _getImageType() {
		if (!isset($this->_imageType)) {
			$imageType = exif_imagetype($this->getPath());
			if (false === $imageType) {
				throw new CM_Exception_Invalid('Cannot detect image type of `' . $this->getPath() . '`.');
			}
			$this->_imageType = $imageType;
		}
		return $this->_imageType;
	}

	/**
	 * @return resource
	 * @throws CM_Exception_Invalid
	 */
	private function _getResource() {
		if (!isset($this->_resource)) {
			switch ($this->_getImageType()) {
				case IMAGETYPE_GIF:
					$resource = @imagecreatefromgif($this->getPath());
					break;
				case IMAGETYPE_JPEG:
					$resource = @imagecreatefromjpeg($this->getPath());
					break;<commit_msg>t90: set gd.jpeg_ignore_warning to 1<commit_after><?php

class CM_File_Image extends CM_File {
	const QUALITY_JPEG = 95;

	/**
	 * @var int
	 */
	private $_imageType;

	/**
	 * @var resource
	 */
	private $_resource;

	public function __construct($file) {
		parent::__construct($file);

		$this->_getResource(); // Make sure resource can be created
	}

	public function __destruct() {
		imagedestroy($this->_resource);
	}

	/**
	 * @param int		 $type	Image type. Use IMAGETYPE_* consts
	 * @param string|null $newPath Where the resulting image should be written
	 */
	public function convert($type, $newPath = null) {
		if ($this->_getImageType() == $type) {
			// Copy image if no conversion necessary
			if (isset($newPath)) {
				$this->copy($newPath);
			}
			return;
		}

		$this->_writeResource($this->_resource, $newPath, $type);
		@chmod($newPath, 0777);
	}

	/**
	 * @param int	$widthMax   New width
	 * @param int	$heightMax  New height
	 * @param bool   $square     True if result image should be a square
	 * @param string $pathNew    OPTIONAL If set, image is stored a new location
	 */
	public function resize($widthMax, $heightMax, $square = false, $pathNew = null) {
		$width = $this->getWidth();
		$height = $this->getHeight();

		if (($width == $widthMax) && ($height == $heightMax)) {
			if (isset($pathNew)) {
				$this->copy($pathNew);
			}
			return;
		}

		$resource = $this->_getResource();
		if ($square) {
			$resource = $this->_getResourceSquare();
			$width = $height = ($width < $height ? $width : $height);
		}

		if (($width > $widthMax) || ($height > $heightMax)) {
			if ($height / $heightMax > $width / $widthMax) {
				$scale_coef = $heightMax / $height;
			} else {
				$scale_coef = $widthMax / $width;
			}
			$heightNew = $height * $scale_coef;
			$widthNew = $width * $scale_coef;
		} else {
			// Don't blow image up
			$heightNew = $height;
			$widthNew = $width;
		}

		$resourceNew = imagecreatetruecolor($widthNew, $heightNew);
		$result = imagecopyresampled($resourceNew, $resource, 0, 0, 0, 0, $widthNew, $heightNew, $width, $height);
		if (!$result) {
			throw new CM_Exception_Invalid('Cannot resample image');
		}
		$this->_writeResource($resourceNew, $pathNew);
		@chmod($pathNew, 0777);
	}

	/**
	 * @param int		 $angle   Angle to rotate the image
	 * @param string|null $pathNew OPTIONAL New path for the rotate image
	 * @throws CM_Exception_Invalid If something goes wrong during rotation or conversion
	 */
	public function rotate($angle, $pathNew = null) {
		$resource = imagerotate($this->_resource, $angle + 180, 0);
		if (!$resource) {
			throw new CM_Exception_Invalid('Cannot rotate image');
		}
		$this->_writeResource($resource, $pathNew);
	}

	/**
	 * @return int
	 */
	public function getWidth() {
		$width = imagesx($this->_resource);
		if (false === $width) {
			throw new CM_Exception_Invalid('Cannot detect image width');
		}
		return (int) $width;
	}

	/**
	 * @return int
	 */
	public function getHeight() {
		$height = imagesy($this->_resource);
		if (false === $height) {
			throw new CM_Exception_Invalid('Cannot detect image height');
		}
		return (int) $height;
	}

	/**
	 * @return resource
	 * @throws CM_Exception_Invalid
	 */
	private function _getResourceSquare() {
		$width = $this->getWidth();
		$height = $this->getHeight();
		if ($width == $height) {
			return $this->_getResource();
		}

		if ($width > $height) {
			$offsetX = ($width - $height) / 2;
			$offsetY = 0;
			$size = $height;
		} else {
			$offsetX = 0;
			$offsetY = ($height - $width) / 2;
			$size = $width;
		}

		$resource = imagecreatetruecolor($size, $size);
		$result = imagecopyresampled($resource, $this->_getResource(), 0, 0, $offsetX, $offsetY, $width, $height, $width, $height);
		if (!$result) {
			throw new CM_Exception_Invalid('Cannot resample image');
		}
		return $resource;
	}

	/**
	 * @return int
	 * @throws CM_Exception_Invalid
	 */
	private function _getImageType() {
		if (!isset($this->_imageType)) {
			$imageType = exif_imagetype($this->getPath());
			if (false === $imageType) {
				throw new CM_Exception_Invalid('Cannot detect image type of `' . $this->getPath() . '`.');
			}
			$this->_imageType = $imageType;
		}
		return $this->_imageType;
	}

	/**
	 * @return resource
	 * @throws CM_Exception_Invalid
	 */
	private function _getResource() {
		ini_set('gd.jpeg_ignore_warning', 1);
		if (!isset($this->_resource)) {
			switch ($this->_getImageType()) {
				case IMAGETYPE_GIF:
					$resource = @imagecreatefromgif($this->getPath());
					break;
				case IMAGETYPE_JPEG:
					$resource = @imagecreatefromjpeg($this->getPath());
					break;
{'fraction_non_alphanumeric': 0.1620768477196038, 'fraction_numerical': 0.005660171982148688, 'mean_word_length': 3.559801488833747, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, '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': '4871585', 'n_tokens_mistral': 3514, 'n_tokens_neox': 3038, 'n_words': 777}