I am bored at the moment and have decided to try making some more examples for my website.
I am working on one at the moment and have hit a brick wall![]()
I am breaking an image up into pieces and making a new image from the pieces to look like photos setout side by side.
If I hard code in the positions etc. it all works OK but what I want is to create the positions depending on the user input of qty per row and amount of rows. I also want to add a small random amount in the x and y to vary the image a bit.
This is what I have so far:
Everything works OK down to TESTING CODE and you can see the final image here:Final imagePHP Code:<?php
// Original image
$image = '../_imagemagick/sunflower.jpg';
// Amount of images in x
$qty_x = '4';
// Amount of images in y
$qty_y = '3';
// Get the size of the original image
$size = getimagesize($image);
// Size to crop the images to
$crop_x = ( $size[0]/$qty_x);
$crop_y = ( $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("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);
}
// Setup the random offsets for the images in the final image
$random = rand(2, 25);
$second = '+'.( $crop_x + $random ) .'+'. $random ;
$random = rand(2, 25);
$third = '+'.( ( $crop_x *2 ) + $random ) .'+'. $random ;
$random = rand(22, 25);
$fourth = '+'.( ( $crop_x *3 ) + $random ) .'+'. $random ;
$random_x = rand(22, 25);
$random_y = rand(12, 35);;
$fith = '+'. $random .'+'. ( $crop_y + $random_y ) ;
$random_x = rand(22, 25);
$random_y = rand(12, 35);
$sixth = '+'.( $crop_x + $random_x ) .'+'. ( $crop_y + $random_y ) ;
$random_x = rand(22, 25);
$random_y = rand(12, 35);
$seventh = '+'.( ( $crop_x *2 ) + $random_x ) .'+'. ( $crop_y + $random_y ) ;
$random_x = rand(22, 25);
$random_y = rand(12, 35);
$eighth = '+'.( ( $crop_x *3 ) + $random_x ) .'+'. ( $crop_y + $random_y ) ;
$random_x = rand(22, 25);
$random_y = rand(22, 45);
$ninth = '+'. $random_x .'+'. ( ( $crop_y *2 ) + $random_y ) ;
$random_x = rand(22, 25);
$random_y = rand(22, 45);
$tenth = '+'.( $crop_x + $random_x ) .'+'. ( ( $crop_y *2 ) + $random_y ) ;
$random_x = rand(22, 25);
$random_y = rand(22, 45);
$eleventh = '+'.( ( $crop_x *2 ) + $random_x ) .'+'. ( ( $crop_y *2 ) + $random_y ) ;
$random_x = rand(22, 25);
$random_y = rand(22, 45);
$twelth = '+'.( ( $crop_x *3 ) + $random_x ) .'+'. ( ( $crop_y *2 ) + $random_y ) ;
// Generate the background combined image
system("convert -size {$width}x{$height} xc:white image-0.png -geometry +{$random}+{$random} -composite image-1.png -geometry $second -composite image-2.png -geometry $third -composite image-3.png -geometry $fourth -composite image-4.png -geometry $fith -composite image-5.png -geometry $sixth -composite image-6.png -geometry $seventh -composite image-7.png -geometry $eighth -composite image-8.png -geometry $ninth -composite image-9.png -geometry $tenth -composite image-10.png -geometry $eleventh -composite image-11.png -geometry $twelth -composite -trim combined.jpg");
// Delete tempory images
foreach (glob("*.png") as $filename) {
unlink($filename);
}
/* ***************** TESTING CODE *********************** */
for ($i = 0; $i <= ( $qty_x - 1 ); $i++) {
echo "image-$i.png";
}
echo "<br>";
for ($i = $qty_x; $i <= ( ( $qty_x * 2 ) - 1 ); $i++) {
echo "image-$i.png";
}
echo "<br>";
for ($i = ( $qty_x * 2 ); $i <= ( ( $qty_x * 3 ) - 1 ); $i++) {
echo "image-$i.png";
}
echo "<br>";
/* **************************************** */
?>
<img src="combined.jpg" border="1">
But as I say this is hard coded and I want to make the final image dependent on the values of $qty_x & $qty_y Has anyone any cunning ideas that can help me out with this ? I was looking at the method in the TESTING CODE part of the code posted above but am unsure of the way to go especialy when the random part comes into it as well.







Bookmarks