1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CSSPrites; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Finder\Finder; |
6
|
|
|
|
7
|
|
|
class ImagesCollection |
8
|
|
|
{ |
9
|
|
|
protected $processor; |
10
|
|
|
protected $directory; |
11
|
|
|
|
12
|
|
|
protected $mask = ''; |
13
|
|
|
protected $ignore = ''; |
14
|
|
|
|
15
|
|
|
protected $images = null; |
16
|
|
|
|
17
|
24 |
|
public function __construct($processor, $directory, $mask, $ignore) |
18
|
|
|
{ |
19
|
24 |
|
$this->processor = $processor; |
20
|
24 |
|
$this->directory = $directory; |
21
|
24 |
|
$this->mask = $mask; |
22
|
24 |
|
$this->ignore = $ignore; |
23
|
|
|
|
24
|
24 |
|
$this->populateImages(); |
25
|
21 |
|
} |
26
|
|
|
|
27
|
3 |
|
public function count() |
28
|
|
|
{ |
29
|
3 |
|
return count($this->images); |
30
|
|
|
} |
31
|
|
|
|
32
|
21 |
|
public function sort($order) |
33
|
|
|
{ |
34
|
21 |
|
if ($order == 'biggest') { |
35
|
21 |
|
usort($this->images, function (Image $a, Image $b) { |
36
|
21 |
|
if ($a->getSurface() == $b->getSurface()) { |
37
|
|
|
// Add some tricks for https://bugs.php.net/bug.php?id=69158 |
38
|
21 |
|
return strcmp($b->getSimpleName(), $a->getSimpleName()); |
39
|
|
|
} |
40
|
|
|
|
41
|
21 |
|
return ($a->getSurface() > $b->getSurface()) ? -1 : 1; |
42
|
21 |
|
}); |
43
|
21 |
|
} |
44
|
|
|
|
45
|
21 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
51 |
|
public function get($key = null) |
49
|
|
|
{ |
50
|
51 |
|
if (!is_null($key)) { |
51
|
33 |
|
return $this->images[$key]; |
52
|
|
|
} |
53
|
|
|
|
54
|
21 |
|
return $this->images; |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function maxWidth() |
58
|
|
|
{ |
59
|
3 |
|
$max = 0; |
60
|
3 |
|
foreach ($this->images as $image) { |
61
|
3 |
|
$max = max($max, $image->getWidth()); |
62
|
3 |
|
} |
63
|
|
|
|
64
|
3 |
|
return $max; |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
public function maxHeight() |
68
|
|
|
{ |
69
|
3 |
|
$max = 0; |
70
|
3 |
|
foreach ($this->images as $image) { |
71
|
3 |
|
$max = max($max, $image->getHeight()); |
72
|
3 |
|
} |
73
|
|
|
|
74
|
3 |
|
return $max; |
75
|
|
|
} |
76
|
|
|
|
77
|
24 |
|
protected function populateImages() |
78
|
|
|
{ |
79
|
24 |
|
$files = Finder::create()->in($this->directory)->name($this->mask)->notName($this->ignore)->depth(0)->files(); |
80
|
|
|
|
81
|
24 |
|
if ($files->count() <= 0) { |
82
|
3 |
|
throw new \Exception('No image found in the directory '.$this->directory); |
83
|
|
|
} |
84
|
|
|
|
85
|
21 |
|
foreach ($files as $file) { |
86
|
21 |
|
$this->images[] = new Image($file->getRealpath(), $this->processor->load($file->getRealpath())); |
87
|
21 |
|
} |
88
|
|
|
|
89
|
21 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|