forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneImageForAll.class.php
194 lines (166 loc) · 4.63 KB
/
OneImageForAll.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
/**
* Contains OneImageForAll class definitions
*/
/**
* Merges all images of a gallery in only one and serve it as a sprite
* Important: every image have the same sizes and jpeg quality.
* @author etessore
* @version 1.0.0
* @package classes
*/
class OneImageForAll extends GalleryHelper{
/**
* @var string stores the name of the cache file
*/
public $cache_name;
/**
* @var string stores the base directory of the cache file
*/
public $cache_dir;
/**
* @var string stores the url of the cache file
*/
public $cache_url;
/**
* @var string stores the path of the cache file
*/
public $cache_path;
/**
* @var boolean true if you want to force cache refresh
*/
public $force_image_refresh;
/**
* @var array stores the configuration
*/
public $config;
/**
* @var string to be returned when no image is available
*/
public $no_images;
/**
* Initializes the object with default values
*/
public function __construct(){
$this->force_image_refresh = false;
$this->no_images = 'No Images!';
}
/**
* (non-PHPdoc)
* @see GalleryHelper::get_markup()
*/
public function get_markup(){
$this
->check_config()
->cache_names()
->merge_images();
$tpl = new SubstitutionTemplate();
$tpl->set_tpl('<div class="slideshow_image" title="%title%" style="%style%"></div>');
$toret = '<div class="cycle" style="width:'.$this->config['w'].'px; height:'.$this->config['h'].'px; overflow:hidden;">';
foreach($this->images as $index => $image){
$toret .= $tpl
->set_markup('title', $this->get_image_alt($index))
->set_markup('style', $this->get_style($index))
->replace_markup();
}
$toret .= '</div>';
return $toret;
}
/**
* Calculates the style for the $image_id
* @param int $image_id the index of the image
* @return string the style html attribute
*/
public function get_style($image_id){
$toret = 'background:url(\''.$this->cache_url.'\') no-repeat scroll -'.($this->config['w']*$image_id).'px 0 transparent; ';
$toret .= 'width:'.$this->config['w'].'px; ';
$toret .= 'height:'.$this->config['h'].'px; ';
return $toret;
}
/**
* Checks if the config has all the paramaters needed.
* @return OneImageForAll $this for chainability
*/
public function check_config(){
if(is_null($this->config))
$this->config = array();
$this->config = array_merge(
$this->config,
array(
'w' => $this->get_image_width(0),
'h' => $this->get_image_height(0),
'q' => '80',
'r' => false
)
);
return $this;
}
/**
* Calculates the cache name, dir, url and path.
* @return OneImageForAll $this for chainability
*/
public function cache_names(){
$this->cache_name = '';
foreach($this->images as $image){
$this->cache_name .= $image->ID.';';
}
$this->cache_name .= serialize($this->config);
$this->cache_name = md5($this->cache_name);
$this->cache_dir = get_stylesheet_directory().'/cache/';
if (!@is_dir($this->cache_dir)){
if (!@mkdir($this->cache_dir)){
wp_die('Couldn\'t create cache dir: '.$this->cache_dir.'<br> Please check the permissions', 'Check your permissions!');
}
}
$this->cache_url = get_stylesheet_directory_uri().'/cache/'.$this->cache_name.'.jpg';
$this->cache_path = $this->cache_dir.$this->cache_name.'.jpg';
return $this;
}
/**
* Merges the images in the current object and returns the url to the big image.
* @return OneImageForAll $this for chainability
*/
protected function merge_images(){
if(empty($this->images))
return $this->no_images;
if(file_exists($this->cache_path) && $this->force_image_refresh!==true)
return $this;
$combined_image = imagecreatetruecolor(
$this->config['w'] * count($this->images),
$this->config['h']
);
foreach($this->images as $array_index => $image){
$src = $this->get_image_path($array_index);
$info = getimagesize($src);
switch($info['mime']){
case 'image/jpeg':
$image = imagecreatefromjpeg($src);
break;
case 'image/png':
$image = imagecreatefrompng($src);
break;
case 'image/gif':
$image = imagecreatefromgif($src);
break;
default:
die('unknow mime type');
}
imagecopymerge(
$combined_image,
$image,
$array_index*$this->config['w'],
0, 0, 0,
$this->config['w'],
$this->config['h'],
100
);
imagejpeg(
$combined_image,
$this->cache_path,
$this->config['q']
);
imagedestroy($image);
}
return $this;
}
}