<commit_before><?php

class SaltedCropperExt extends DataExtension {
	protected static $db = array(
		'SaltedCropperScheduled'	=>	'Boolean'
	);
	
	public function updateCMSFields( FieldList $fields ) {
		$fields->addFieldsToTab(
			'Root.Main',
			array(
				CheckboxField::create('SaltedCropperScheduled')
			)
		);
	}
	
	public function onBeforeWrite() {
		parent::onBeforeWrite();
		if (Controller::curr() && $this->owner->SaltedCropperScheduled) {
			
			//Debugger::inspect('here', false);
			$request = Controller::curr()->request;
			if ($bindings = $request->postVar('cropper_bindings')) {
				
				//Debugger::inspect($request->postVars());
				
				foreach($bindings as $binding) {
					$chain = explode(':', $binding);
					$source = $chain[0];
					$target = $chain[1];
					$source = $this->owner->$source();
					
					$canvas_x = $request->postVar($target . '_salted_container_x');
					$canvas_y = $request->postVar($target . '_salted_container_y');
					$canvas_w = $request->postVar($target . '_salted_container_width');
					$canvas_h = $request->postVar($target . '_salted_container_height');
					$cropper_x = $request->postVar($target . '_salted_cropper_x');
					$cropper_y = $request->postVar($target . '_salted_cropper_y');
					$cropper_w = $request->postVar($target . '_salted_cropper_width');
					$cropper_h = $request->postVar($target . '_salted_cropper_height');
					
					if ($this->owner->$target()->exists()) {
						$this->owner->$target()->delete();
					}
					
					$target = $target . 'ID';
					
					$this->cropImage($source, $target, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h);
				}
				
				$this->owner->SaltedCropperScheduled = false;
			}
		}
	}
	
	private function cropImage($source, $target, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h) {
		$cropped = $this->duplicateImage($source);
		$cropped->write();
		$cropped->updateFilesystem();
		$this->owner->$target = $cropped->ID;
		if (extension_loaded('imagick')) {
			
			$this->scaleCropImagick($cropped->getFullPath(), $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h);
			
		} else {
			$this->scaleCropGD();
		}
	}
	
	private function scaleCropImagick($image_path, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h) {
		
		
		$imagick = new Imagick($image_path);
		$original_width = $imagick->getImageWidth();
		$x = $cropper_x + $canvas_x;
		$y = $cropper_y + $canvas_y;
		if ($original_width != $canvas_w) {
			$ratio = $original_width / $canvas_w;
			$cropper_w = $cropper_w * $ratio;
			$cropper_h = $cropper_h * $ratio;
			$x = $x * $ratio;
			$y = $y * $ratio;
		}
		//$imagick->resizeImage($canvas_w, $canvas_h, Imagick::FILTER_LANCZOS, 1);
		$imagick->cropImage ( $cropper_w , $cropper_h , $x , $y );
		$imagick->writeImage($image_path);
		$imagick->destroy();
	}
	
	private function scaleCropGD($image_path, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h) {
		
	}
	
	private function duplicateImage($src_image) {
		
		$folder = Folder::find_or_make('cropper_resamples');
		$dest_image = new Image();
		$newFileName = ltrim($this->getUniqueFileName($folder->getRelativePath(), $src_image->Name, $src_image->ID, $folder),'_');
		$newFileName = strtolower(str_replace('_','-', $newFileName));
		
		copy($src_image->getFullPath(), $folder->getFullPath().$newFileName);
		
		$dest_image->setName($newFileName);
		$dest_image->setParentID($folder->ID);
		return $dest_image;
	}
	
	private function getUniqueFileName($path, $fileName, $fileID, $parentFolder) {
		$pathinfo = pathinfo($fileName);
		$file = File::get()->filter(array(<commit_msg>now using image's own folder to store cropped samples<commit_after><?php

class SaltedCropperExt extends DataExtension {
	protected static $db = array(
		'SaltedCropperScheduled'	=>	'Boolean'
	);
	
	public function updateCMSFields( FieldList $fields ) {
		$fields->addFieldsToTab(
			'Root.Main',
			array(
				CheckboxField::create('SaltedCropperScheduled')
			)
		);
	}
	
	public function onBeforeWrite() {
		parent::onBeforeWrite();
		if (Controller::curr() && $this->owner->SaltedCropperScheduled) {
			
			//Debugger::inspect('here', false);
			$request = Controller::curr()->request;
			if ($bindings = $request->postVar('cropper_bindings')) {
				
				//Debugger::inspect($request->postVars());
				
				foreach($bindings as $binding) {
					$chain = explode(':', $binding);
					$source = $chain[0];
					$target = $chain[1];
					$source = $this->owner->$source();
					
					$canvas_x = $request->postVar($target . '_salted_container_x');
					$canvas_y = $request->postVar($target . '_salted_container_y');
					$canvas_w = $request->postVar($target . '_salted_container_width');
					$canvas_h = $request->postVar($target . '_salted_container_height');
					$cropper_x = $request->postVar($target . '_salted_cropper_x');
					$cropper_y = $request->postVar($target . '_salted_cropper_y');
					$cropper_w = $request->postVar($target . '_salted_cropper_width');
					$cropper_h = $request->postVar($target . '_salted_cropper_height');
					
					if ($this->owner->$target()->exists()) {
						$this->owner->$target()->delete();
					}
					
					$target = $target . 'ID';
					
					$this->cropImage($source, $target, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h);
				}
				
				$this->owner->SaltedCropperScheduled = false;
			}
		}
	}
	
	private function cropImage($source, $target, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h) {
		$cropped = $this->duplicateImage($source);
		$cropped->write();
		$cropped->updateFilesystem();
		$this->owner->$target = $cropped->ID;
		if (extension_loaded('imagick')) {
			
			$this->scaleCropImagick($cropped->getFullPath(), $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h);
			
		} else {
			$this->scaleCropGD();
		}
	}
	
	private function scaleCropImagick($image_path, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h) {
		
		
		$imagick = new Imagick($image_path);
		$original_width = $imagick->getImageWidth();
		$x = $cropper_x + $canvas_x;
		$y = $cropper_y + $canvas_y;
		if ($original_width != $canvas_w) {
			$ratio = $original_width / $canvas_w;
			$cropper_w = $cropper_w * $ratio;
			$cropper_h = $cropper_h * $ratio;
			$x = $x * $ratio;
			$y = $y * $ratio;
		}
		//$imagick->resizeImage($canvas_w, $canvas_h, Imagick::FILTER_LANCZOS, 1);
		$imagick->cropImage ( $cropper_w , $cropper_h , $x , $y );
		$imagick->writeImage($image_path);
		$imagick->destroy();
	}
	
	private function scaleCropGD($image_path, $canvas_x, $canvas_y, $canvas_w, $canvas_h, $cropper_x, $cropper_y, $cropper_w, $cropper_h) {
		
	}
	
	private function duplicateImage($src_image) {
		$currenFolder = str_replace('assets/', '', $src_image->Parent()->getRelativePath());
		$folder = Folder::find_or_make($currenFolder.'cropper_resamples');
		$dest_image = new Image();
		$newFileName = ltrim($this->getUniqueFileName($folder->getRelativePath(), $src_image->Name, $src_image->ID, $folder),'_');
		$newFileName = strtolower(str_replace('_','-', $newFileName));
		
		copy($src_image->getFullPath(), $folder->getFullPath().$newFileName);
		
		$dest_image->setName($newFileName);
		$dest_image->setParentID($folder->ID);
		return $dest_image;
	}
	
	private function getUniqueFileName($path, $fileName, $fileID, $parentFolder) {
		$pathinfo = pathinfo($fileName);
		$file = File::get()->filter(array(
{'fraction_non_alphanumeric': 0.17964071856287425, 'fraction_numerical': 0.0007810466024472793, 'mean_word_length': 4.547292418772563, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 105, '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': '6782241', 'n_tokens_mistral': 3302, 'n_tokens_neox': 2765, 'n_words': 438}