-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shuffle.function.php
55 lines (40 loc) · 1.47 KB
/
Shuffle.function.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
function shuffle_image( $filename, $div = 2, $saveas = null )
{
list($sw, $sh) = getimagesize($filename);
$src = imagecreatefromjpeg($filename);
$w = round($sw/$div);
$h = round($sh/$div);
$dst = imagecreatetruecolor($sw, $sh);
$map = array();
// fill array with parts of source image
for( $y = 0; $y < $div; $y++)
for( $x = 0; $x < $div; $x++)
$map[] = array($x,$y);
$html = '';
for( $sy = 0; $sy < $div; $sy++) {
$html .= '<div>';
for( $sx = 0; $sx < $div; $sx++) {
$elnum = array_rand( $map ); // get random part index
list($dx, $dy) = $map[$elnum]; // get this part
unset($map[$elnum]); // remove from stack
$html .= '<span style="background-position: -'.$dx*$w.'px -'.$dy*$h.'px;"></span>';
imagecopyresampled($dst,$src,$dx*$w,$dy*$h,$sx*$w,$sy*$h,$w,$h,$w,$h); // place it to dest image
}
$html .= '</div>';
}
$filename = is_null($saveas) ? 'shuffle_'. $filename : $saveas;
$id = uniqid('shuffle');
file_put_contents($filename . '.html', <<<STYLE
<style type="text/css">
#{$id} { display: inline; }
#{$id} span { width: {$w}px; height: {$h}px; display: inline-block; background-image: url('{$filename}'); }
#{$id} div { display: block; clear: both; }
</style>
<div id="{$id}">
{$html}
</div>
STYLE
);
imagejpeg($dst, $filename); //save new image
}