PHP: How to split a blob-picture in nine parts?
I got a real hard question now: I have a blob-Picture saved in my database. Now, I want to split it into 9 Parts (like the tic-tac-toe-grid) an开发者_开发百科d then I want to hide the one down right. Now I want to display these 8 parts in a random order, so you can move them and solve the puzzle (the moving could be solved with javascript).
How can I make the php-functionality? Are there existing functions for that?
Thx for help ;)
You can utilize GD library. There is a function imagecopy()
http://www.php.net/manual/en/function.imagecopy.php within which you may copy the parts of image. For example if you have 90x90 pixels image you'll have to call it with 0,0,30,30 so that you'll have first square. Save that parts as separate images or put directly into browser and then you'll only need to manipulate them in a proper way [here: puzzle solver].
Ok so this would be a combination of JavaScript and PHP, being able split the images in PHP would not be to hard, but you would have to remember that you need some organization with the files so that the JavaScript knows the order they should go in to be a complete puzzle.
Instead of splitting the image itself, your would be better of taking slices of it using mathematical methods.
1 2 3
-------------------
1 | | | |
-------------------
2 | | | |
-------------------
3 | | | |
-------------------
Ok so an image is 300 x 300 pixels you would need to divide the width and height by the amount of images required dived by 2, for example:
$ImagesRequired = 9;
$GlobalImageWidth = 900;
$GlobalImageHeight = 900;
$EachImageWidth = $GlobalImageWidth / ($ImagesRequired / 2);
$EachImageHeight = $GlobalImageHeight / ($ImagesRequired / 2);
Both of the values should be 66~ pixels, you would then move around the image taking chunks of the image using crop.
for($x=0;$x<=$GlobalImageWidth;$x = $x+=$EachImageWidth)
{
}
Looking at the graph above you would you take images in incremental order so you would accumulative the following dimensions.
- 1.1 = 0 x 66.6
- 1.2 = 0 x 133.33
- 1.3 = 0 x 196.66
- 2.1 = 66.6 x 66.6
- 2.2 = 66.6 x 133.33
- 2.3 = 66.6 x 196.66
- ...
So now you have calculated the dimensions that each image should be cropped at, you would use GD or Imagik libraries to slice the image, creating an array of images that you can then store in the cache.
The above may not be 100% correct as it's late here and im not an expert in maths or image manipulation but hopefully this will get you started, also to see some great work done in Imagik and image manipulation see the following link, it should help inspire you.
- Generated Mosaic
- The explanation and source codes
精彩评论