Skip to content

Instantly share code, notes, and snippets.

@Cacodaimon
Created December 17, 2012 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cacodaimon/4317980 to your computer and use it in GitHub Desktop.
Save Cacodaimon/4317980 to your computer and use it in GitHub Desktop.
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