Create a gist now

Instantly share code, notes, and snippets.

Creates some kind of combined image from a given set of images. Used @ cacodaemon.de
<?php
/**
* Creates some kind of combined image from a given set of images.
* All images should have the same resolution.
* Used and explained at www.cacodaemon.de
*
* Autor Guido Krömer
* E-Mail: mail<at>cacodaemon.de
*
*/
$images = array();
$width = 0;
$height = 0;
foreach ($_FILES['images']['tmp_name'] as $key => $tmpName) {
if (!isset($tmpName) || empty($tmpName)) {
continue;
}
switch ($_FILES['images']['type'][$key]) {
case 'image/jpeg':
$images[$key] = imagecreatefromjpeg($tmpName);
break;
case 'image/gif':
$images[$key] = imagecreatefromgif($tmpName);
break;
case 'image/png':
$images[$key] = imagecreatefrompng($tmpName);
break;
default:
continue;
}
$size = @getimagesize($_FILES['images']['tmp_name'][$key]);
$width = max($width, $size[0]);
$height = max($height, $size[1]);
}
$newImage = imagecreatetruecolor($width, $height);
$partWidth = intval($width / count($images));
$partHeight = intval($height / count($images));
$lineColor = imagecolorallocate($newImage, 255, 0, 0);
$i = 0;
foreach ($images as $key => $image) {
switch ($_POST['orientation']) {
case 'vertical':
imagecopyresampled( $newImage, $image, $partWidth * $i, 0, $partWidth * $i, 0, $partWidth, $height, $partWidth, $height);
if ($_POST['lines'] == 'yes' && !empty($i)) {
imageline($newImage, $partWidth * $i, 0, $partWidth * $i, $height, $lineColor);
}
break;
case 'horizontal':
imagecopyresampled( $newImage, $image, 0, $partHeight * $i, 0, $partHeight * $i, $width, $partHeight, $width, $partHeight);
if ($_POST['lines'] == 'yes' && !empty($i)) {
imageline($newImage, 0, $partHeight * $i, $width, $partHeight * $i, $lineColor);
}
default:
break;
}
++$i;
}
imagepng($newImage, $imageUrl = uniqid() . '.png');
imagedestroy($newImage);
header('Location: http://' .$_SERVER["HTTP_HOST"] . '/' . $imageUrl);
exit;
<html>
<head>
<title>Split Image Generator</title>
</head>
<body>
<h1>Split Image Generator</h1>
<form action="split_image.php" method="post" enctype="multipart/form-data">
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<input type="file" name="images[]"><br />
<label for="orientation">Orientation</label>
<select name="orientation" id="orientation">
<option value="vertical" >Vertical</option>
<option value="horizontal">Horizontal</option>
</select><br />
<label for="lines">Lines</label>
<select name="lines" id="lines">
<option value="yes" >Yes</option>
<option value="no">No</option>
</select><br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment