Thanks for your detailed answers it's much appreciated!
As I am still discovering ImageMagick, I didn't want to dive too quickly into the APIs but I guess I will if I want to achieve my goal!
By the way I'd like to thank you a lot for all your examples Anthony, it's an amazing resource for the beginner!
Bonzo has some fantastic examples as well, especially
this one.
But the code is a little outdated for the new PHP versions, so I corrected this and added the "$path" variable because on my server, for some reason some of the commands didn't work because I was calling "convert" and not "/usr/local/bin/convert"...
Code: Select all
<?php
/*
Source: http://www.rubblewebs.co.uk./imagemagick/examples/example_b.php
Note: Some care needs using on how to divide the image as sometimes a small image is created
that causes a problem.
*/
// Absolute path to the generated images without the trailing slash
$path = "/var/www/vhosts/domain.com/httpdocs";
// If the form has been submitted do this
if ( @$_POST['Submit'] ) {
// Temporary upload image name
$image = $_FILES['filename']['tmp_name'];
// Name to save the image as - in this case the same as the original
$new_image = $path.'/'.$_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 images - it 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("/usr/local/bin/convert $image -crop {$crop_x}x{$crop_y} $path/image-%d.jpg");
// Remove the .jpg from the filenames
foreach (glob("$path/*.jpg") as $filename) {
$file = str_replace('.jpg', '', $filename );
system("/usr/local/bin/convert $filename -background black +polaroid $file.png");
}
// 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 .= " $path/image-$n.png -geometry +$offset_x+$offset_y -composite" ; }
}
// Produce the combined image
system ( "/usr/local/bin/convert -size {$width}x{$height} xc:silver $command -trim $new_image");
// Delete tempory images
foreach (glob("$path/*.jpg") as $filename) {
if($filename != $new_image) unlink($filename);
}
foreach (glob("$path/*.png") as $filename) {
if($filename != $new_image) unlink($filename);
}
// Output the image to the browser
header( 'Content-Type: image/png' );
readfile("$new_image");
}
else { ?>
<form method="post" action="" 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" />
</form>
<?php } ?>