<?php

// ez hibas
function kepmeretezesvagas($file,$mappa,$ujfile,$cw=200,$ch=150){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( file_exists($mappa."/".$ujfile) || !file_exists($file) ){
		print ", kmv - file: ".$file;
		return;
	}
	
	$vals = getimagesize($file);
	$tipus = $vals['2'];
	
	switch($tipus){
		case 1 :
			$orig = imagecreatefromgif($file);
			break;
		case 2 :
			$orig = imagecreatefromjpeg($file);
			break;
		case 3 :
			$orig = imagecreatefrompng($file);
			break;
		default:
			$orig = imagecreatefrombmp($file);
		//	$orig = imagecreatefromwbmp($file);
			break;
	}
	
	$crop = imagecreatetruecolor($cw, $ch);
	
	list($ow,$oh) = getimagesize($file);
	
	//ezek akkor igazak, ha négyzet
	$top = 0;
	$left = 0;
	$ratio = $ch/$oh;
	
	
	if($ow > $oh){//fekvő
		$left = 1;
	}else if($oh > $ow){//álló
		$ratio = $cw/$ow;
		$top = 1;
	}
	
	$rw = round($ow * $ratio);
	$rh = round($oh * $ratio);
	
	//az előbbi feltételből következik
	if($left == 1){//fekvő
		$left = round(($rw - $cw)/2);
	}else if($top == 1){//álló
		$top = round(($rh - $ch)/2);
	}
	
	$res = imagecreatetruecolor($rw, $rh);
	//előbb összenyomjuk
	imagecopyresampled($res, $orig, 0, 0, 0, 0, $rw, $rh, $ow, $oh);
	//utána kimásoljuk a közepét
	imagecopy($crop, $res, 0, 0, $left, $top, $rw, $rh);
	
	switch($tipus){
		case 1 :
			$orig = imagegif($crop,$mappa."/".$ujfile);
			break;
		case 2 :
			$orig = imagejpeg($crop,$mappa."/".$ujfile,99);
			break;
		case 3 :
			$orig = imagepng($crop,$mappa."/".$ujfile);
			break;
		default :
			$orig = imagebmp($crop,$mappa."/".$ujfile);
			break;
	}
	
	imagedestroy($res);
	imagedestroy($crop);
	imagedestroy($orig);
}

function kontraszt($file,$newfile,$value){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( file_exists($newfile) || !file_exists($file) )
		return;
	
	$vals = getimagesize($file);
	$tipus = $vals['2'];
	
	switch($tipus){
		case 1 :
			$orig = imagecreatefromgif($file);
			break;
		case 2 :
			$orig = imagecreatefromjpeg($file);
			break;
		case 3 :
			$orig = imagecreatefrompng($file);
			break;
		default:
			$orig = imagecreatefrombmp($file);
			break;
	}
	
	imagefilter($orig, IMG_FILTER_CONTRAST, $value);
	
	switch($tipus){
		case 1 :
			$orig = imagegif($orig,$newfile);
			break;
		case 2 :
			$orig = imagejpeg($orig,$newfile,99);
			break;
		case 3 :
			$orig = imagepng($orig,$newfile);
			break;
		default :
			$orig = imagebmp($orig,$newfile);
			break;
	}
	
	imagedestroy($orig);
	
}

function fenyero($file,$newfile,$value){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( file_exists($newfile) || !file_exists($file) )
		return;
	
	$vals = getimagesize($file);
	$tipus = $vals['2'];
	
	switch($tipus){
		case 1 :
			$orig = imagecreatefromgif($file);
			break;
		case 2 :
			$orig = imagecreatefromjpeg($file);
			break;
		case 3 :
			$orig = imagecreatefrompng($file);
			break;
		default:
			$orig = imagecreatefrombmp($file);
			break;
	}
	
	imagefilter($orig, IMG_FILTER_BRIGHTNESS, $value);
	
	switch($tipus){
		case 1 :
			$orig = imagegif($orig,$newfile);
			break;
		case 2 :
			$orig = imagejpeg($orig,$newfile,99);
			break;
		case 3 :
			$orig = imagepng($orig,$newfile);
			break;
		default :
			$orig = imagebmp($orig,$newfile);
			break;
	}
	
	imagedestroy($orig);
}

function fenyerosh($file,$newfile,$value,$rewrite=false){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( !file_exists($file) )
		return;
	
	if( file_exists($newfile) && !$rewrite )
		return;
	
	$v = 100 + $value;
	exec('convert -modulate '.$v.',100,100 '.$file.' '.$newfile);
}

function kontrasztsh($file,$newfile,$value,$rewrite=false){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( !file_exists($file) )
		return;
	
	if( file_exists($newfile) && !$rewrite )
		return;
	
	$eredetiv = $value;
	
	/////////////////////////////////////////////////
	
	if($value<0){
		$s = "-";
		$value *= -1;
	}
	else
		$s = "+";
	
	$v = "";
	for($i=0;$i<$value;$i++)
		$v .= $s."contrast ";
	
	exec('convert '.$v.' '.$file.' '.$newfile);
	
	return $eredetiv.": ".$v."<br>";
}

function meretezsh($file,$newfile,$value,$felulir = false){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( !file_exists($file) ){
//		print "nincs meg az alap";
		return;
	}
	if( file_exists($newfile) && !$felulir ){
//		print "nem irja felul";
		return;
	}
	exec('convert -resize '.$value.' '.$file.' '.$newfile);
}



function kontrasztsh2($file,$newfile,$value){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( file_exists($newfile) || !file_exists($file) )
		return;
	
	
/*	if($value>0)
		$v = (50+(1.0*$value))."%";
	elseif($value<0)
		$v = (50-(-1.0*$value))."%";
	else
		$v = "50%";*/
	
	$v = (50-(-1.0*$value))."%";
	
	exec('convert -contrast-stretch '.$v.' '.$file.' '.$newfile);
	
	return "v2: ".$value.": ".$v."&nbsp;&nbsp;|&nbsp;&nbsp;";
}


function kontrasztsh3($file,$newfile,$value){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	if( file_exists($newfile) || !file_exists($file) )
		return;
	
	$eredetiv = $value;
	
	if($value<0){
		$s = "-";
		$value *= -1;
	}
	else
		$s = "+";
	
	$v = $value.'.0x40%';
	
	if($v!=0)
		exec('convert '.$s.'sigmoidal-contrast '.$v.' '.$file.' '.$newfile);
	else
		exec('convert '.$file.' '.$newfile);
	
	return "v3: ".$eredetiv.": ".$v."&nbsp;&nbsp;|&nbsp;&nbsp;";
}




function imagebmp(&$img, $filename = false)
{
	$wid = imagesx($img);
	$hei = imagesy($img);
	$wid_pad = str_pad('', $wid % 4, "\0");
	
	$size = 54 + ($wid + $wid_pad) * $hei * 3; //fixed
	
	//prepare & save header
	$header['identifier']		= 'BM';
	$header['file_size']		= dword($size);
	$header['reserved']			= dword(0);
	$header['bitmap_data']		= dword(54);
	$header['header_size']		= dword(40);
	$header['width']			= dword($wid);
	$header['height']			= dword($hei);
	$header['planes']			= word(1);
	$header['bits_per_pixel']	= word(24);
	$header['compression']		= dword(0);
	$header['data_size']		= dword(0);
	$header['h_resolution']		= dword(0);
	$header['v_resolution']		= dword(0);
	$header['colors']			= dword(0);
	$header['important_colors']	= dword(0);

	if ($filename)
	{
		$f = fopen($filename, "wb");
		foreach ($header AS $h){
			fwrite($f, $h);
		}
		
		//save pixels
		for ($y=$hei-1; $y>=0; $y--){
			for ($x=0; $x<$wid; $x++){
				$rgb = imagecolorat($img, $x, $y);
				fwrite($f, byte3($rgb));
			}
			fwrite($f, $wid_pad);
		}
		fclose($f);
	}
	else
	{
		foreach ($header AS $h){
			echo $h;
		}
		
		//save pixels
		for ($y=$hei-1; $y>=0; $y--)
		{
			for ($x=0; $x<$wid; $x++)
			{
				$rgb = imagecolorat($img, $x, $y);
				echo byte3($rgb);
			}
			echo $wid_pad;
		}
	}
}

function imagecreatefrombmp($filename)
{
	$f = fopen($filename, "rb");
	
	//read header
	$header = fread($f, 54);
	$header = unpack(	'c2identifier/Vfile_size/Vreserved/Vbitmap_data/Vheader_size/' .
						'Vwidth/Vheight/vplanes/vbits_per_pixel/Vcompression/Vdata_size/'.
						'Vh_resolution/Vv_resolution/Vcolors/Vimportant_colors', $header);

	if ($header['identifier1'] != 66 or $header['identifier2'] != 77)
	{
		die('Not a valid bmp file');
	}
	
	if ($header['bits_per_pixel'] != 24)
	{
		die('Only 24bit BMP images are supported');
	}
	
	$wid2 = ceil((3*$header['width']) / 4) * 4;
		
	$wid = $header['width'];
	$hei = $header['height'];

	$img = imagecreatetruecolor($header['width'], $header['height']);

	//read pixels
	for ($y=$hei-1; $y>=0; $y--)
	{
		$row = fread($f, $wid2);
		$pixels = str_split($row, 3);
		for ($x=0; $x<$wid; $x++){
			imagesetpixel($img, $x, $y, dwordize($pixels[$x]));
    	}
    }
	fclose($f);
	
	return $img;
}

function dwordize($str){
	$a = ord($str[0]);
	$b = ord($str[1]);
	$c = ord($str[2]);
	return $c*256*256 + $b*256 + $a;
}

function byte3($n){
	return chr($n & 255) . chr(($n >> 8) & 255) . chr(($n >> 16) & 255);
}

function dword($n){
	return pack("V", $n);
}

function word($n){
	return pack("v", $n);
}





function kepmeretezesvagasmax($file,$mappa,$ujfile,$cw=200,$ch=150){
	// ha a leendő már létezik  vagy  nem létezik az alap 
	/*if( file_exists($mappa."/".$ujfile) || !file_exists($file) )
		return;
	
	$vals = getimagesize($file);
	$tipus = $vals['2'];
	
	switch($tipus){
		case 1 :
			$orig = imagecreatefromgif($file);
			break;
		case 2 :
			$orig = imagecreatefromjpeg($file);
			break;
		case 3 :
			$orig = imagecreatefrompng($file);
			break;
		default:
			$orig = imagecreatefrombmp($file);
			break;
	}
	
	$crop = imagecreatetruecolor($cw, $ch);
	
	list($ow,$oh) = getimagesize($file);
	
	if($ow > $cw){//szelesebb
		$left = 1;
		$ratio = 1-($cw/$ow);
		
		
		$rw = round($ow * $ratio);
		$rh = round($oh * $ratio);
		
		
		$res = imagecreatetruecolor($rw, $rh);
		//összenyomás
		imagecopyresampled($res, $orig, 0, 0, 0, 0, $rw, $rh, $ow, $oh);
	}
	
	
	switch($tipus){
		case 1 :
			$orig = imagegif($crop,$mappa."/".$ujfile);
			break;
		case 2 :
			$orig = imagejpeg($crop,$mappa."/".$ujfile,99);
			break;
		case 3 :
			$orig = imagepng($crop,$mappa."/".$ujfile);
			break;
		default :
			$orig = imagebmp($crop,$mappa."/".$ujfile);
			break;
	}
	
	imagedestroy($res);
	imagedestroy($crop);
	imagedestroy($orig);
	
	
	
	
	// magasság szerinti összenyomás
	
	$file = $mappa."/".$ujfile;
	$vals2 = getimagesize($file);
	$tipus = $vals2['2'];
	
	switch($tipus){
		case 1 :
			$orig = imagecreatefromgif($file);
			break;
		case 2 :
			$orig = imagecreatefromjpeg($file);
			break;
		case 3 :
			$orig = imagecreatefrompng($file);
			break;
		default:
			$orig = imagecreatefrombmp($file);
			break;
	}
	
	$crop = imagecreatetruecolor($cw, $ch);
	
	list($ow,$oh) = getimagesize($file);
	
	if($oh > $ch){//magasabb
		$ratio = 1-($ch/$oh);
		$top = 1;
		
		$rw2 = round($ow * $ratio);
		$rh2 = round($oh * $ratio);
		
		
		$res = imagecreatetruecolor($rw2, $rh2);
		//összenyomás még 1x
		imagecopyresampled($res, $orig, 0, 0, 0, 0, $rw2, $rh2, $ow, $oh);
		
		switch($tipus){
			case 1 :
				$orig = imagegif($crop,$mappa."/".$ujfile);
				break;
			case 2 :
				$orig = imagejpeg($crop,$mappa."/".$ujfile,99);
				break;
			case 3 :
				$orig = imagepng($crop,$mappa."/".$ujfile);
				break;
			default :
				$orig = imagebmp($crop,$mappa."/".$ujfile);
				break;
		}
		
		imagedestroy($res);
		imagedestroy($crop);
		imagedestroy($orig);
	}*/
}

function kemeretezesvagassh($file,$ujfile,$cw=200,$ch=150){
	// egy pelda: convert -resize 300x300 image-2 temp.jpg && convert -gravity Center -crop 100x100+0+0 +repage temp.jpg ujkozep2.jpg && rm temp.jpg
	
	// tesztelni: (temp fajl nelkul)
	// $parancs = "convert {$file} -resize {$cw}x{$ch}^     -gravity Center -crop {$cw}x{$ch}+0+0 +repage {$ujfile}   >> public/bugfajlok.txt";
	
	//$file = str_replace(' ','\ ',$file);
	//$ujfile = str_replace(' ','\ ',$ujfile);
	
	//$parancs = "convert -resize {$cw}x{$ch}^ {$file} public/img/collection/milora/temp.jpg  &&  convert -gravity Center -crop {$cw}x{$ch}+0+0 +repage public/img/collection/milora/temp.jpg {$ujfile}  &&  rm public/img/collection/milora/temp.jpg >> public/bugfajlok.txt";
	
	$parancs = "convert '{$file}' -resize {$cw}x{$ch}^     -gravity Center -crop {$cw}x{$ch}+0+0 +repage '{$ujfile}'";
	
	exec($parancs);
}

function kemeretezesvagasshbg($file,$ujfile,$cw=200,$ch=150,$bg="white"){
	
	$file = str_replace(' ','\ ',$file);
	$ujfile = str_replace(' ','\ ',$ujfile);
	
	$parancs = "convert {$file} -resize {$cw}x{$ch} -background {$bg} -gravity center -extent {$cw}x{$ch} {$ujfile}";

	exec($parancs);
}

?>