echo "Directory: $argv[1]\n";

$dir = $argv[1];

$files1 = scandir($dir);

foreach ($files1 as $file1) {

if (endsWith($file1, ".jpeg"))

{

$filesImages[] = $dir . "/" . $file1 . "\n";

}

}

$filesImagesCount = count($filesImages);

echo "Images: $filesImagesCount\n";

$imageDestination = @imagecreatetruecolor(4000, 3000);

$imagePieceWidth = 4000 / $filesImagesCount;

$imagesPlaced = 0;

foreach ($files1 as $imageSourceFilename) {

if (endsWith($imageSourceFilename, ".jpeg"))

{

echo "Processing: $imageSourceFilename\n";

$imageSource = imagecreatefromjpeg ($dir . "/" . $imageSourceFilename);

imagecopy ($imageDestination, $imageSource, $imagesPlaced * $imagePieceWidth, 0, $imagesPlaced * $imagePieceWidth, 0, $imagePieceWidth + 1, 3000);

$imagesPlaced++;

}

}

imagedestroy($imageSource );

imagejpeg($imageDestination, "Final-Collage.jpeg", 100);

imagedestroy($imageDestination);

function endsWith($haystack, $needle)

{

$length = strlen($needle);

if ($length == 0) {

return true;

}

return (substr($haystack, -$length) === $needle);

}

?>