<?php
/* This file is meant to run from the CLI/Shell
 * it splits images on the middle, and saves individual pages
 * It creates 240px + 64px wide thumbs of the first page right side
 * Author Carsten Jensen aka Tomse @ http://awesome.commodore.me
 * Copyright (C) 2013 Carsten Jensen
 * This is distributed under GNU GPLv2
 */

/* Requirements:
 * php (cli/shell version) / or as apache site
 * imagemagick module
 */

/* Usage:
 * Run the file from the browser (apache setup)
 * or from cli : php -f split 
 * thumb files will be saved in same dir as pdf files
 */
 
// Edit the path here where your files to be split are stored.
$input = "./input/";

// Edit the path here where your result files will be ssaved.
$output = "./output/";

// Counting from 0 where 0 is the front page, to the first "page 1"
$offset = 0;

// Sequential pages = true or if clip has been removed and it's the paper page
$sequential = false;


// Replace false with true to make the script work
$work = true;


// code is here.. nothing more to modify
// -------------------------------------
if ($work == true)
{
	if ($sequential === false)
	{
		$zero = "0";
		$files = glob($input . "*.[pP][nN][gG]");
		$count = count($files);
		// $pagenum = 0;
		$lowpage = 1;
		
		$highpage = ($count * 2) - $offset;
		foreach($files as $k=>$v)
		{
			$leadzeros = $offset;
			if ($k % 2 == 0)
			{
				$odd = false;
			}
			else
			{
				$odd = true;
			}
			
			// leading zeros for pages prior the numbered page 1
			if ($offset > $k)
			{
				$zeros = "";

				if ($odd === false)
				{
					for ($i = 0; $leadzeros-$k > $i; $leadzeros--)
					{
						$zeros = $zeros . $zero;
					}
					$rightfilename = "page_0" . $zeros . "1";
					$leftfilename = "page_". str_pad($highpage, strlen($count), 0, STR_PAD_LEFT);
				}
				else
				{
					for ($i = 0; $leadzeros-$k > $i; $leadzeros--)
					{
						$zeros = $zeros . $zero;
					}
					$leftfilename = "page_0" . $zeros . "1";
					$rightfilename = "page_". str_pad($highpage, strlen($count), 0, STR_PAD_LEFT);
				}
			}
			else
			{

				if ($odd === true)
				{
				// Right page = high, highpage - $offset
				// left page = low, $k + lowpage 
					$leftfilename = "page_". str_pad($lowpage, strlen($count), 0, STR_PAD_LEFT);
					$rightfilename = "page_". str_pad($highpage, strlen($count), 0, STR_PAD_LEFT);
				}
				else
				{
					$rightfilename = "page_". str_pad($lowpage, strlen($count), 0, STR_PAD_LEFT);
					$leftfilename = "page_". str_pad($highpage, strlen($count), 0, STR_PAD_LEFT);
				}
				$lowpage++;
			}
	print "Processing $k => $v \n";
	print "Saving $rightfilename \n";
	print "Saving $leftfilename \n";
	print "\n";

			
			$highpage--;
			

			
			$thumb = new Imagick();
			$thumb->readImage($v);
			$thumb->setImageFormat('png');
//			$thumb->setImagecompression(imagick::COMPRESSION_JPEG);
	//		$thumb->setImageCompressionQuality(90);

			// Copy image so we can crop left and right sides
			$left = new Imagick();
			$left->readImage($v);
			$left->setImageFormat('png');
//			$left->setImagecompression(imagick::COMPRESSION_JPEG);
	//		$left->setImageCompressionQuality(90);

			
			// Get geometry of image
			$geometry = $thumb->getImageGeometry();
			$x = $geometry['width'];
			$y = $geometry['height'];
			$cropsize = round($x / 2);

			$geox = $cropsize-1;

	//print ($cropsize . " " . $y . " " . $geox . " " . $x ."\n");		
			// Cropping left side
			$left->setImagePage(0,0,0,0);
			$left->cropImage($cropsize, $y, 0, 0);

			// Cropping right side
			$thumb->setImagePage(0,0,0,0);
			$thumb->cropImage($cropsize, $y, $geox, 0);

			
			if ($k == 0)
			{
				$max = ($count * 2) - 1;
				//$rightfilename = "page_". str_pad($pagenum, strlen($count), 0, STR_PAD_LEFT);
				//$leftfilename = "page_". str_pad($max, strlen($count), 0, STR_PAD_LEFT);
				$thumb->writeImage($output . $rightfilename . ".png");
				$left->writeImage($output . $leftfilename . ".png");
				$thumb->resizeImage(240, 1024, imagick::FILTER_LANCZOS, 1, true);
				$thumb->writeImage($output . "_thumb.jpg");
				$thumb->resizeImage(64, 1024, imagick::FILTER_LANCZOS, 1, true);
				$thumb->writeImage($output . "_thumb64.jpg");
				
			}
			else
			{
				//$leftpage = ($i * 2) - 1;
				//$rightpage = ($i * 2);
				//$rightfilename = "page_". str_pad($rightpage, strlen($count), 0, STR_PAD_LEFT);
				//$leftfilename = "page_". str_pad($leftpage, strlen($count), 0, STR_PAD_LEFT);
				$thumb->writeImage($output . $rightfilename . ".png");
				$left->writeImage($output . $leftfilename . ".png");
			}
			
			$thumb->clear();
			$thumb->destroy();
			$left->clear();
			$left->destroy();
		}
	}
	else
	{
		$zero = "0";
		$files = glob($input . "*.[pP][nN][gG]");
		$count = count($files);
		// $pagenum = 0;
		$lowpage = 1;
		
		$highpage = $lowpage + 1;
		foreach($files as $k=>$v)
		{
			$leadzeros = $offset;
			if ($k % 2 == 0)
			{
				$odd = false;
			}
			else
			{
				$odd = true;
			}
			
			// leading zeros for pages prior the numbered page 1
			if ($offset > $k)
			{
				$zeros = "";

				for ($i = 0; $leadzeros-$k > $i; $leadzeros--)
				{
					$zeros = $zeros . $zero;
				}
				$leftfilename = "page_0" . $zeros . "1";
				$rightfilename = "page_". str_pad($highpage, strlen($count), 0, STR_PAD_LEFT);
			}
			else
			{
				$leftfilename = "page_". str_pad($lowpage, strlen($count), 0, STR_PAD_LEFT);
				$rightfilename = "page_". str_pad($highpage, strlen($count), 0, STR_PAD_LEFT);
			}
			$lowpage += 2;
			$highpage += 2;
	print "Processing $k => $v \n";
	print "Saving $rightfilename \n";
	print "Saving $leftfilename \n";
	print "\n";
			$thumb = new Imagick();
			$thumb->readImage($v);
			$thumb->setImageFormat('png');
//			$thumb->setImagecompression(imagick::COMPRESSION_JPEG);
	//		$thumb->setImageCompressionQuality(90);

			// Copy image so we can crop left and right sides
			$left = new Imagick();
			$left->readImage($v);
			$left->setImageFormat('png');
//			$left->setImagecompression(imagick::COMPRESSION_JPEG);
	//		$left->setImageCompressionQuality(90);

			
			// Get geometry of image
			$geometry = $thumb->getImageGeometry();
			$x = $geometry['width'];
			$y = $geometry['height'];
			$cropsize = round($x / 2);

			$geox = $cropsize-1;

	//print ($cropsize . " " . $y . " " . $geox . " " . $x ."\n");		
			// Cropping left side
			$left->setImagePage(0,0,0,0);
			$left->cropImage($cropsize, $y, 0, 0);

			// Cropping right side
			$thumb->setImagePage(0,0,0,0);
			$thumb->cropImage($cropsize, $y, $geox, 0);

			
			if ($k == 0)
			{
				$max = ($count * 2) - 1;
				//$rightfilename = "page_". str_pad($pagenum, strlen($count), 0, STR_PAD_LEFT);
				//$leftfilename = "page_". str_pad($max, strlen($count), 0, STR_PAD_LEFT);
				$thumb->writeImage($output . $rightfilename . ".png");
				$left->writeImage($output . $leftfilename . ".png");
				$thumb->resizeImage(240, 1024, imagick::FILTER_LANCZOS, 1, true);
				$thumb->writeImage($output . "_thumb.jpg");
				$thumb->resizeImage(64, 1024, imagick::FILTER_LANCZOS, 1, true);
				$thumb->writeImage($output . "_thumb64.jpg");
				
			}
			else
			{
				//$leftpage = ($i * 2) - 1;
				//$rightpage = ($i * 2);
				//$rightfilename = "page_". str_pad($rightpage, strlen($count), 0, STR_PAD_LEFT);
				//$leftfilename = "page_". str_pad($leftpage, strlen($count), 0, STR_PAD_LEFT);
				$thumb->writeImage($output . $rightfilename . ".png");
				$left->writeImage($output . $leftfilename . ".png");
			}
			
			$thumb->clear();
			$thumb->destroy();
			$left->clear();
			$left->destroy();
		}

	}
}
?>
