Rubblewebs

THESE CODES ARE PROVIDED FOR AN EXAMPLE OF HOW TO USE IMAGEMAGICK WITH PHP. CARE SHOULD BE TAKEN WHEN ACCEPTING USER INPUT.

I TAKE NO RESPONSABILTY FOR ANY PROBLEMS THAT MAY OCCURE WHEN USING ANY OF THIS CODE.

IT IS UP TO YOU TO SECURE THE CODE AND VALIDATE USER INPUT.

Dividing an image up so it looks like it is made up of a lot of smaller photos.

The input image The output image

/*
Note:
Some care needs using on how to divide the imageas sometimes a small image is created that causes a problem.
*/
// If the form has been submitted do this
if ( $Submit ) { 
// Temporary upload image name
$image = $_FILES['filename']['tmp_name'];
// Get the image dimensions
$size=GetImageSize( $original_image );
// Name to save the image asin this case the same as the original
$new_image = $_FILES['filename']['name'];
// Amount of images in x
$qty_x = $_POST['x'];
// Amount of images in y
$qty_y = $_POST['y'];
// Get the size of the original image
$size = getimagesize($image);
// Size to crop the images to
$crop_x = round( $size[0]/$qty_x);
$crop_y = round( $size[1]/$qty_y);
// Canvas for combined imagesit will be trimmed to the finished image size in the code
$width = ( $size[0] * 1.5 );
$height = ( $size[1] *1.5 );
// Crop the image
system("convert $image -crop {$crop_x}x{$crop_y} image-%d.jpg"); 
// Remove the .jpg from the filenames
foreach (glob("*.jpg") as $filename) {
$file = str_replace('.jpg', '', $filename );
system("convert $filename -background black +polaroid $file.png"); 
}
// Delete tempory images
foreach (glob("*.jpg") as $filename) {
unlink($filename); 
}
// Set $command variable
$command = "";
// Loop through generating the image layout
for ($j = 0; $j < $qty_y; $j++) { 
  for ($i = 0; $i < $qty_x; $i++) {    $n = $j<0 ? $qty_x*($j)+$i : $i; 
  $offset_x= ( ($crop_x * $i ) + rand(2, 25));
  $offset_y=( ($crop_y * $j ) + rand(2, 25));
  $command .= " image-$n.png -geometry +$offset_x+$offset_y -composite" ;  } 
  }
// Produce the combined image
system ( "convert -size {$width}x{$height} xc:silver $command -trim $new_image"); 
// Delete tempory images
foreach (glob("*.png") as $filename) {
unlink($filename); 
}
}
else { ?>
<form method="post" action="<?php echo $PHP_SELF; ?>" enctype="multipart/form-data"> 
<label>File to upload:</label>
<input type="file" name="filename"  /><br />
<label>Amount of images in X</label>
<input type="text" name="x">
<label>Amount of images in Y</label>
<input type="text" name="y">
<input type="Submit" name="Submit" value="Submit" />