<commit_before><?php

	require_once LIB_PATH . '/base/Util.php';

	class php_image {

		/**
		 * Contains image resource
		 *
		 * @access private
		 * @var Resource
		 */
		private $image = ''; 
		
		/**
		 * Contains resized image resource
		 *
		 * @access private
		 * @var Resource
		 */
		private $image_resized = ''; 

		/**
		 * Contains mime_type
		 *
		 * @access private
		 * @var string
		 */		
		private $mime_type = '';

		/**
		 * Contains width
		 *
		 * @access private
		 * @var int
		 */		
		private $width;

		/**
		 * Contains height
		 *
		 * @access private
		 * @var int
		 */
		private $height;

		/**
		 * Constructor
		 *
		 * @access public
		 */
		public function __construct($file) {
			$this->load($file);						
		}

		/**
		 * Load image
		 *
		 * @access private
		 * @param string $file
		 */
		private function load($file) {
			if (is_object($file) AND get_class($file) == 'Picture') {
				$path = $file->get_path();
				$width = $file->width;
				$height = $file->height;
				$mime_type = $file->mime_type;
			} elseif (is_string($file) AND file_exists($file)) {
				$path = $file;
				list($width, $height) = $this->get_dimensions($path);
				$mime_type = $this->get_mime_type($path);
			} else {
				throw new Exception ('This is not an existing file or Picture object');
			}

			$this->width = $width;
			$this->height = $height;
			$this->mime_type = $mime_type;
			$this->image = $this->open($path);
		}

		/**
		 * Open image
		 *
		 * @access private
		 * @return Resource $img
		 */
		private function open($path) {
			switch ($this->mime_type) {
				case 'image/jpeg':
					$img = imagecreatefromjpeg($path);  
					break;
				case 'image/gif':
					$img = imagecreatefromgif($path);  
					break;
				case 'image/png':
					$img = imagecreatefrompng($path); 
					break;
				default:
					$img = false;
			}

			return $img;
		}

		/**
		 * Resize image
		 * 
		 * @access public
		 * @param int $new_width (px)
		 * @param int $new_height (px)
		 * @proportional bool $proportional
		 */
		public function output($destination = null, $quality = 100) {

			switch ($this->mime_type) {
				case 'image/jpeg':
					imagejpeg($this->image_resized, $destination, $quality);  
					break;
				case 'image/gif':
					imagegif($this->image_resized, $destination); 
					break;
				case 'image/png':
		            $scale_quality = round(($quality/100) * 9);  
					$invert_scale_quality = 9 - $scale_quality;  
					imagepng($this->image_resized, $destination, $invert_scale_quality);  
				default:
					break;
			}

			imagedestroy($this->image_resized);
			exit();
			
		}

		/**
		 * Calculate output dimensions
		 * 
		 * @access private
		 * @param int $width (px)
		 * @param int $height (px)
		 * @param int $new_width (px)
		 * @param int $new_height (px)
		 * @return array $output_dimensions
		 */
		private function get_output_dimensions($new_width = null, $new_height = null, $mode) {
			
			switch ($mode) {
				case 'exact':
					$output_width = $new_width;
					$output_height = $new_height;
					break;
				case 'auto':
					list($output_width, $output_height) = $this->calculate_size($new_width, $new_height);
					break;
				case 'crop':
					list($output_width, $output_height) = $this->calculate_crop_size($new_width, $new_height);
					break;
			}
			return array($output_width, $output_height);			
		}

		/**
		 * Calculate width using new height
		 *
		 * @access private
		 * @param int $new_height
		 * @return float $output_width
		 */
		private function calculate_size_by_height($new_height) {
			$ratio = $this->width / $this->height;  
		    return $new_height * $ratio;  		 
		}

		/**
		 * Calculate height using new width
		 *
		 * @access private
		 * @param int $new_width
		 * @return float $output_height
		 */
		private function calculate_size_by_width($new_width) {
			$ratio = $this->height / $this->width;  
		    return $new_width * $ratio;  		
		}

		/**
		 * Calculate output dimensions
		 *
		 * @access private
		 * @param int $new_width
		 * @param int $new_height
		 * @return array $output_dimensions
		 */
		private function calculate_size($new_width, $new_height) {
			if ($this->height < $this->width) {  
				$output_width = $new_width;  
				$output_height = $this->calculate_size_by_width($new_width);  
			} elseif ($this->height > $this->width) {  
				$output_width = $this->calculate_size_by_height($new_height);  
				$output_height = $new_height;  
			} else {
				if ($new_height < $new_width) {  
					$output_width = $new_width;  
					$output_height = $this->calculate_size_by_width($new_width);  
				} else if ($new_height > $new_width) {  
					$output_width = $this->calculate_size_by_height($new_height);  
					$output_height = $new_height;  
				} else {  
					$output_width = $new_width;  
					$output_height = $new_height;
				}
			}

			return array($output_width, $output_height);
		}

		/**
		 * Calculate output dimensions for cropping
		 *
		 * @access private
		 * @param int $new_width
		 * @param int $new_height
		 * @return array $output_dimensions
		 */
		private function calculate_crop_size($new_width, $new_height) {
			$height_ratio = $this->height / $new_height;  
			$width_ratio  = $this->width / $new_width;  
  
			if ($height_ratio < $width_ratio) {  
				$output_ratio = $height_ratio;  
			} else {  
				$output_ratio = $width_ratio;  
			}
			
			$output_height = $this->height / $output_ratio;  
			$output_width = $this->width  / $output_ratio;  

			return array($output_width, $output_height);
		}

		/**
		 * Get dimensions
		 * 
		 * @access public
		 * @param mixed $path
		 * @return array $dimensions
		 */
		public function get_dimensions($path = null) {
			if (!is_null($path)) {
				$this->path = $path;
			}
			
			return getimagesize($this->path);
		}

		/**
		 * Get mime types
		 *
		 * @access public
		 * @param mixed $path
		 * @return string $mime_type
		 */
		public function get_mime_type($path = null) {
			if (!is_null($path)) {
				$this->path = $path;
			}
			return Util::get_mime_type($this->path);
		}	

		/**
		 * Resize image
		 * 
		 * @access public
		 * @param int $new_width (px)
		 * @param int $new_height (px)
		 * @proportional bool $proportional
		 */
		public function resize($new_width = null, $new_height = null, $mode = 'auto') {
			
			if (is_null($new_width) AND is_null($new_height)) {
				throw new Exception('specifiy output dimensions');
			}

			list($output_width, $output_height) = $this->get_output_dimensions($new_width, $new_height, $mode);
			
			$this->image_resized = imagecreatetruecolor($output_width, $output_height);  						

			if ($this->mime_type == 'image/gif' OR $this->mime_type == 'image/png') {
				$transparent_index = imagecolortransparent($this->image);
				if ($transparent_index >= 0) {	// GIF
					imagepalettecopy($this->image, $this->image_resized);
					imagefill($this->image_resized, 0, 0, $transparent_index);
					imagecolortransparent($this->image_resized, $transparent_index);
					imagetruecolortopalette($this->image_resized, true, 256);
				} else { // PNG
					imagealphablending($this->image_resized, false);
					imagesavealpha($this->image_resized,true);
					$transparent = imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127);
					imagefilledrectangle($this->image_resized, 0, 0, $output_width, $output_height, $transparent);
				}
			}
						
			imagecopyresampled($this->image_resized, $this->image, 0, 0, 0, 0, $output_width, $output_height, $this->width, $this->height);  
			
			if ($mode == 'crop') {
				$this->crop($output_width, $output_height, $new_width, $new_height); 
			}
		}

		/**
		 * Crop image
		 * 
		 * @access private
		 * @param int $output_width (px)
		 * @param int $output_height (px)
		 * @param int $new_width (px)
		 * @param int $new_height (px)
		 */
		private function crop($output_width, $output_height, $new_width, $new_height) {
			$offset_x = ($output_width / 2) - ($new_width / 2);  
			$offset_y = ($output_height / 2) - ($new_height / 2);  
  
			$crop = $this->image_resized;  
		    $this->image_resized = imagecreatetruecolor($new_width, $new_height);  
			imagecopyresampled($this->image_resized, $crop , 0, 0, $offset_x, $offset_y, $output_width, $output_height , $output_width, $output_height);  
		}

	}<commit_msg>FIX: whitespace<commit_after><?php
/**
 * Email class
 *
 * Send emails
 *
 * @package %%PACKAGE%%
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @version $Id$
 */

require_once LIB_PATH . '/base/Util.php';

class php_image {

	/**
	 * Contains image resource
	 *
	 * @access private
	 * @var Resource
	 */
	private $image = '';

	/**
	 * Contains resized image resource
	 *
	 * @access private
	 * @var Resource
	 */
	private $image_resized = '';

	/**
	 * Contains mime_type
	 *
	 * @access private
	 * @var string
	 */
	private $mime_type = '';

	/**
	 * Contains width
	 *
	 * @access private
	 * @var int
	 */
	private $width;

	/**
	 * Contains height
	 *
	 * @access private
	 * @var int
	 */
	private $height;

	/**
	 * Constructor
	 *
	 * @access public
	 */
	public function __construct($file) {
		$this->load($file);
	}

	/**
	 * Load image
	 *
	 * @access private
	 * @param string $file
	 */
	private function load($file) {
		if (is_object($file) AND get_class($file) == 'Picture') {
			$path = $file->get_path();
			$width = $file->width;
			$height = $file->height;
			$mime_type = $file->mime_type;
		} elseif (is_string($file) AND file_exists($file)) {
			$path = $file;
			list($width, $height) = $this->get_dimensions($path);
			$mime_type = $this->get_mime_type($path);
		} else {
			throw new Exception ('This is not an existing file or Picture object');
		}

		$this->width = $width;
		$this->height = $height;
		$this->mime_type = $mime_type;
		$this->image = $this->open($path);
	}

	/**
	 * Open image
	 *
	 * @access private
	 * @return Resource $img
	 */
	private function open($path) {
		switch ($this->mime_type) {
			case 'image/jpeg':
				$img = imagecreatefromjpeg($path);
				break;
			case 'image/gif':
				$img = imagecreatefromgif($path);
				break;
			case 'image/png':
				$img = imagecreatefrompng($path);
				break;
			default:
				$img = false;
		}

		return $img;
	}

	/**
	 * Resize image
	 *
	 * @access public
	 * @param int $new_width (px)
	 * @param int $new_height (px)
	 * @proportional bool $proportional
	 */
	public function output($destination = null, $quality = 100) {

		switch ($this->mime_type) {
			case 'image/jpeg':
				imagejpeg($this->image_resized, $destination, $quality);
				break;
			case 'image/gif':
				imagegif($this->image_resized, $destination);
				break;
			case 'image/png':
				$scale_quality = round(($quality/100) * 9);
				$invert_scale_quality = 9 - $scale_quality;
				imagepng($this->image_resized, $destination, $invert_scale_quality);
			default:
				break;
		}

		imagedestroy($this->image_resized);
		exit();
	}

	/**
	 * Calculate output dimensions
	 *
	 * @access private
	 * @param int $width (px)
	 * @param int $height (px)
	 * @param int $new_width (px)
	 * @param int $new_height (px)
	 * @return array $output_dimensions
	 */
	private function get_output_dimensions($new_width = null, $new_height = null, $mode) {
		switch ($mode) {
			case 'exact':
				$output_width = $new_width;
				$output_height = $new_height;
				break;
			case 'auto':
				list($output_width, $output_height) = $this->calculate_size($new_width, $new_height);
				break;
			case 'crop':
				list($output_width, $output_height) = $this->calculate_crop_size($new_width, $new_height);
				break;
		}

		return array($output_width, $output_height);
	}

	/**
	 * Calculate width using new height
	 *
	 * @access private
	 * @param int $new_height
	 * @return float $output_width
	 */
	private function calculate_size_by_height($new_height) {
		$ratio = $this->width / $this->height;
		return $new_height * $ratio;
	}

	/**
	 * Calculate height using new width
	 *
	 * @access private
	 * @param int $new_width
	 * @return float $output_height
	 */
	private function calculate_size_by_width($new_width) {
		$ratio = $this->height / $this->width;
		return $new_width * $ratio;
	}

	/**
	 * Calculate output dimensions
	 *
	 * @access private
	 * @param int $new_width
	 * @param int $new_height
	 * @return array $output_dimensions
	 */
	private function calculate_size($new_width, $new_height) {
		if ($this->height < $this->width) {
			$output_width = $new_width;
			$output_height = $this->calculate_size_by_width($new_width);
		} elseif ($this->height > $this->width) {
			$output_width = $this->calculate_size_by_height($new_height);
			$output_height = $new_height;
		} else {
			if ($new_height < $new_width) {
				$output_width = $new_width;
				$output_height = $this->calculate_size_by_width($new_width);
			} else if ($new_height > $new_width) {
				$output_width = $this->calculate_size_by_height($new_height);
				$output_height = $new_height;
			} else {
				$output_width = $new_width;
				$output_height = $new_height;
			}
		}

		return array($output_width, $output_height);
	}

	/**
	 * Calculate output dimensions for cropping
	 *
	 * @access private
	 * @param int $new_width
	 * @param int $new_height
	 * @return array $output_dimensions
	 */
	private function calculate_crop_size($new_width, $new_height) {
		$height_ratio = $this->height / $new_height;
		$width_ratio = $this->width / $new_width;

		if ($height_ratio < $width_ratio) {
			$output_ratio = $height_ratio;
		} else {
			$output_ratio = $width_ratio;
		}

		$output_height = $this->height / $output_ratio;
		$output_width = $this->width / $output_ratio;

		return array($output_width, $output_height);
	}

	/**
	 * Get dimensions
	 *
	 * @access public
	 * @param mixed $path
	 * @return array $dimensions
	 */
	public function get_dimensions($path = null) {
		if (!is_null($path)) {
			$this->path = $path;
		}

		return getimagesize($this->path);
	}

	/**
	 * Get mime types
	 *
	 * @access public
	 * @param mixed $path
	 * @return string $mime_type
	 */
	public function get_mime_type($path = null) {
		if (!is_null($path)) {
			$this->path = $path;
		}

		return Util::get_mime_type($this->path);
	}

	/**
	 * Resize image
	 *
	 * @access public
	 * @param int $new_width (px)
	 * @param int $new_height (px)
	 * @proportional bool $proportional
	 */
	public function resize($new_width = null, $new_height = null, $mode = 'auto') {
		if (is_null($new_width) AND is_null($new_height)) {
			throw new Exception('specifiy output dimensions');
		}

		list($output_width, $output_height) = $this->get_output_dimensions($new_width, $new_height, $mode);
		
		$this->image_resized = imagecreatetruecolor($output_width, $output_height);

		if ($this->mime_type == 'image/gif' OR $this->mime_type == 'image/png') {
			$transparent_index = imagecolortransparent($this->image);
			if ($transparent_index >= 0) { // GIF
				imagepalettecopy($this->image, $this->image_resized);
				imagefill($this->image_resized, 0, 0, $transparent_index);
				imagecolortransparent($this->image_resized, $transparent_index);
				imagetruecolortopalette($this->image_resized, true, 256);
			} else { // PNG
				imagealphablending($this->image_resized, false);
				imagesavealpha($this->image_resized,true);
				$transparent = imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127);
				imagefilledrectangle($this->image_resized, 0, 0, $output_width, $output_height, $transparent);
			}
		}

		imagecopyresampled($this->image_resized, $this->image, 0, 0, 0, 0, $output_width, $output_height, $this->width, $this->height);
		
		if ($mode == 'crop') {
			$this->crop($output_width, $output_height, $new_width, $new_height);
		}
	}

	/**
	 * Crop image
	 *
	 * @access private
	 * @param int $output_width (px)
	 * @param int $output_height (px)
	 * @param int $new_width (px)
	 * @param int $new_height (px)
	 */
	private function crop($output_width, $output_height, $new_width, $new_height) {
		$offset_x = ($output_width / 2) - ($new_width / 2);
		$offset_y = ($output_height / 2) - ($new_height / 2);

		$crop = $this->image_resized;
		$this->image_resized = imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($this->image_resized, $crop , 0, 0, $offset_x, $offset_y, $output_width, $output_height , $output_width, $output_height);
	}
}
{'fraction_non_alphanumeric': 0.1519157791651365, 'fraction_numerical': 0.0046517321581588935, 'mean_word_length': 3.256056264652253, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 142, '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': '5907617', 'n_tokens_mistral': 6748, 'n_tokens_neox': 5521, 'n_words': 1180}