<commit_before><?php
/**
	 * Changes image to new size, by shrinking/enlarging
	 * then cropping to respect new ratio.
	 *
	 * Arguments:
	 * - width of new image
	 * - height of new image
	 * - crop method
	 */
class TimberImageOperationResize extends TimberImageOperation {

   private $w, $h, $crop;

	/**
	 * @param int    $w    width of new image
	 * @param int    $h    height of new image
	 * @param string $crop cropping method, one of: 'default', 'center', 'top', 'bottom', 'left', 'right'.
	 */
	function __construct($w, $h, $crop) {
		$this->w = $w;
		$this->h = $h;
		// Sanitize crop position
		$allowed_crop_positions = array( 'default', 'center', 'top', 'bottom', 'left', 'right' );
		if ( $crop !== false && !in_array( $crop, $allowed_crop_positions ) ) {
			$crop = $allowed_crop_positions[0];
		}
		$this->crop = $crop;
	}

	/**
	 * @param   string    $src_filename     the basename of the file (ex: my-awesome-pic)
	 * @param   string    $src_extension    the extension (ex: .jpg)
	 * @return  string    the final filename to be used (ex: my-awesome-pic-300x200-c-default.jpg)
	 */
	public function filename($src_filename, $src_extension) {
		$result = $src_filename . '-' . $this->w . 'x' . $this->h . '-c-' . ( $this->crop ? $this->crop : 'f' ); // Crop will be either user named or f (false)
		if($src_extension) {
			$result .= '.'.$src_extension;
		}
		return $result;
	}

	protected function run_animated_gif( $load_filename, $save_filename ) {
		$image = wp_get_image_editor( $load_filename );
		$current_size = $image->get_size();
		$src_w = $current_size['width'];
		$src_h = $current_size['height'];
		$w = $this->w;
		$h = $this->h;
		if ( !class_exists('Imagick') ) {
			return false;
		}
		$image = new Imagick($load_filename);
		$image = $image->coalesceImages();
		$crop = self::get_target_sizes( $load_filename );
		foreach ($image as $frame) {
			$frame->cropImage($crop['src_w'], $crop['src_h'], $crop['x'], $crop['y']);
			$frame->thumbnailImage($w, $h);
			$frame->setImagePage($w, $h, 0, 0);
		}
		$image = $image->deconstructImages();
		return $image->writeImages($save_filename, true);
	}

	protected function get_target_sizes( $load_filename ) {
		$image = wp_get_image_editor( $load_filename );
		$w = $this->w;
		$h = $this->h;
		$crop = $this->crop;

		$current_size = $image->get_size();
		$src_w = $current_size['width'];
		$src_h = $current_size['height'];
		$src_ratio = $src_w / $src_h;
		if ( !$h ) {
			$h = round( $w / $src_ratio );
		}
		if ( !$w ) {
			//the user wants to resize based on constant height
			$w = round( $h * $src_ratio );
		}
		// Get ratios
		$dest_ratio = $w / $h;
		$src_wt = $src_h * $dest_ratio;
		$src_ht = $src_w / $dest_ratio;
		if ( !$crop ) {
			return array('x' => 0, 'y' => 0, 'src_w' => $src_w, 'src_h' => $src_h, 'target_w' => $w, 'target_h' => $h);
		}
		$src_x = $src_w / 2 - $src_wt / 2;
		$src_y = ( $src_h - $src_ht ) / 6;
		//now specific overrides based on options:
		if ( $crop == 'center' ) {
			// Get source x and y
			$src_x = round( ( $src_w - $src_wt ) / 2 );
			$src_y = round( ( $src_h - $src_ht ) / 2 );
		} else if ( $crop == 'top' ) {
				$src_y = 0;
			} else if ( $crop == 'bottom' ) {
				$src_y = $src_h - $src_ht;
			} else if ( $crop == 'left' ) {
				$src_x = 0;
			} else if ( $crop == 'right' ) {
				$src_x = $src_w - $src_wt;
			}
		// Crop the image
		if ( $dest_ratio > $src_ratio ) {
			return array('x' => 0, 'y' => $src_y, 'src_w' => $src_w, 'src_h' => $src_ht, 'target_w' => $w, 'target_h' => $h);
		}
		return array('x' => $src_x, 'y' => 0, 'src_w' => $src_wt, 'src_h' => $src_h, 'target_w' => $w, 'target_h' => $h);
	}

	/**
	 * Performs the actual image manipulation,
	 * including saving the target file.
	 *
	 * @param  string $load_filename filepath (not URL) to source file<commit_msg>adjust tabbing for target size generation<commit_after><?php
/**
	 * Changes image to new size, by shrinking/enlarging
	 * then cropping to respect new ratio.
	 *
	 * Arguments:
	 * - width of new image
	 * - height of new image
	 * - crop method
	 */
class TimberImageOperationResize extends TimberImageOperation {

   private $w, $h, $crop;

	/**
	 * @param int    $w    width of new image
	 * @param int    $h    height of new image
	 * @param string $crop cropping method, one of: 'default', 'center', 'top', 'bottom', 'left', 'right'.
	 */
	function __construct($w, $h, $crop) {
		$this->w = $w;
		$this->h = $h;
		// Sanitize crop position
		$allowed_crop_positions = array( 'default', 'center', 'top', 'bottom', 'left', 'right' );
		if ( $crop !== false && !in_array( $crop, $allowed_crop_positions ) ) {
			$crop = $allowed_crop_positions[0];
		}
		$this->crop = $crop;
	}

	/**
	 * @param   string    $src_filename     the basename of the file (ex: my-awesome-pic)
	 * @param   string    $src_extension    the extension (ex: .jpg)
	 * @return  string    the final filename to be used (ex: my-awesome-pic-300x200-c-default.jpg)
	 */
	public function filename($src_filename, $src_extension) {
		$result = $src_filename . '-' . $this->w . 'x' . $this->h . '-c-' . ( $this->crop ? $this->crop : 'f' ); // Crop will be either user named or f (false)
		if($src_extension) {
			$result .= '.'.$src_extension;
		}
		return $result;
	}

	protected function run_animated_gif( $load_filename, $save_filename ) {
		$image = wp_get_image_editor( $load_filename );
		$current_size = $image->get_size();
		$src_w = $current_size['width'];
		$src_h = $current_size['height'];
		$w = $this->w;
		$h = $this->h;
		if ( !class_exists('Imagick') ) {
			return false;
		}
		$image = new Imagick($load_filename);
		$image = $image->coalesceImages();
		$crop = self::get_target_sizes( $load_filename );
		foreach ($image as $frame) {
			$frame->cropImage($crop['src_w'], $crop['src_h'], $crop['x'], $crop['y']);
			$frame->thumbnailImage($w, $h);
			$frame->setImagePage($w, $h, 0, 0);
		}
		$image = $image->deconstructImages();
		return $image->writeImages($save_filename, true);
	}

	protected function get_target_sizes( $load_filename ) {
		$image = wp_get_image_editor( $load_filename );
		$w = $this->w;
		$h = $this->h;
		$crop = $this->crop;

		$current_size = $image->get_size();
		$src_w = $current_size['width'];
		$src_h = $current_size['height'];
		$src_ratio = $src_w / $src_h;
		if ( !$h ) {
			$h = round( $w / $src_ratio );
		}
		if ( !$w ) {
			//the user wants to resize based on constant height
			$w = round( $h * $src_ratio );
		}
		if ( !$crop ) {
			return array(
				'x' => 0, 'y' => 0,
				'src_w' => $src_w, 'src_h' => $src_h,
				'target_w' => $w, 'target_h' => $h
			);
		}
		// Get ratios
		$dest_ratio = $w / $h;
		$src_wt = $src_h * $dest_ratio;
		$src_ht = $src_w / $dest_ratio;
		$src_x = $src_w / 2 - $src_wt / 2;
		$src_y = ( $src_h - $src_ht ) / 6;
		//now specific overrides based on options:
		if ( $crop == 'center' ) {
			// Get source x and y
			$src_x = round( ( $src_w - $src_wt ) / 2 );
			$src_y = round( ( $src_h - $src_ht ) / 2 );
		} else if ( $crop == 'top' ) {
			$src_y = 0;
		} else if ( $crop == 'bottom' ) {
			$src_y = $src_h - $src_ht;
		} else if ( $crop == 'left' ) {
			$src_x = 0;
		} else if ( $crop == 'right' ) {
			$src_x = $src_w - $src_wt;
		}
		// Crop the image
		if ( $dest_ratio > $src_ratio ) {
			return array(
				'x' => 0, 'y' => $src_y,
				'src_w' => $src_w, 'src_h' => $src_ht,
				'target_w' => $w, 'target_h' => $h
			);
		}
		return array(
			'x' => $src_x, 'y' => 0,
			'src_w' => $src_wt, 'src_h' => $src_h,
			'target_w' => $w, 'target_h' => $h
		);
	}

	/**
	 * Performs the actual image manipulation,
	 * including saving the target file.
	 *
	 * @param  string $load_filename filepath (not URL) to source file
{'fraction_non_alphanumeric': 0.19202286605170846, 'fraction_numerical': 0.005196829933740418, 'mean_word_length': 3.070861977789529, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 81, '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': '6428695', 'n_tokens_mistral': 3369, 'n_tokens_neox': 3047, 'n_words': 749}