-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shuffle.class.php
106 lines (80 loc) · 2.52 KB
/
Shuffle.class.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Description of shuffle
*
* @package shuffle
* @subpackage shuffle
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.blackcrystal.net
* @version
* @since Mar 24, 2013
* @author Sergei Miami <[email protected]>
*/
class Shuffle
{
private $src = null,
$dst = null,
$type = null,
$width = 0,
$height = 0,
$w,$h,
$html = array(),
$map = array();
public function __construct($src = null)
{
if (!is_null($src))
$this->load($src);
}
public function load($filename)
{
$this->map = array(); // clean shuffle map
list($this->width, $this->height, $this->type) = getimagesize($filename); // get image params
// load image by type
switch ($this->type) {
case 1: $this->src = imagecreatefromgif($filename); break; //gif
case 2: $this->src = imagecreatefromjpeg($filename); break; //jpeg
case 3: $this->src = imagecreatefrompng($filename); break; //png
}
return $this;
}
public function save($filename)
{
$id = uniqid('shuffle');
file_put_contents($filename . '.html', <<<STYLE
<style type="text/css">
#{$id} { display: inline; }
#{$id} span { width: {$this->w}px; height: {$this->h}px; display: inline-block; background-image: url('{$filename}'); }
#{$id} div { display: block; clear: both; }
</style>
<div id="{$id}">
{$this->html}
</div>
STYLE
);
imagejpeg($this->dst, $filename); //save new image
return $this;
}
public function shuffle( $div = 2 )
{
$this->w = $w = $this->width/$div;
$this->h = $h = $this->height/$div;
$this->dst = imagecreatetruecolor($this->width, $this->height);
// fill array with parts of source image
for( $y = 0; $y < $div; $y++)
for( $x = 0; $x < $div; $x++)
$map[] = array($x,$y);
$this->html = '';
for( $sy = 0; $sy < $div; $sy++) {
$this->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
$this->html .= '<span style="background-position: -'.$dx*$w.'px -'.$dy*$h.'px;"></span>';
imagecopyresampled($this->dst,$this->src,$dx*$w,$dy*$h,$sx*$w,$sy*$h,$w,$h,$w,$h); // place it to dest image
}
$this->html .= '</div>';
}
return $this;
}
}