-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolorize.php
More file actions
233 lines (201 loc) · 5.3 KB
/
colorize.php
File metadata and controls
233 lines (201 loc) · 5.3 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* colorize
* Jesse Baird
<jebaird@gmail.com>
* 9/21/2010
*
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* simple php class to manipulate colors in an iamge and replace them
*
* http://stackoverflow.com/questions/456044/can-i-swap-colors-in-image-using-gd-library-in-php
* http://php.net/manual/en/function.imagefilter.php
* http://stackoverflow.com/questions/254388/how-do-you-convert-an-image-to-black-and-white-in-php
*
* TODO: remove save from repalace and tint add method called save. only modify the image resosources in memory allowing you to do multipule "passes / calls on your set of images"
*
**/
class colorize {
private $images = array();
const CACHE_PATH = './cache/';
/**
* colorizer::__construct()
*
* @param mixed $images array of iamges and paths | directory path
* @return void
*/
public function __construct($images = array()) {
//check for directory
if (is_string($images)) {
$path = $images;
if( substr($path, -1) != '/' ){
$path.='/';
}
$images = array();
if (is_dir($path)) {
if ($dh = opendir($path)) {
while (($file = readdir($dh)) !== false) {
$ext = $this -> getExtention($file);
//find all of the images in the path
if (in_array($ext, array('png', 'gif', 'jpg', 'jpeg'))) {
$images[] = $path.$file;
}
}
closedir($dh);
unset($dh, $file, $ext);
// free memory
//print_r( $images );
}
}
}
foreach ($images as $img) {
$this -> images[basename($img)] = $this -> createImage($img);
}
unset($img);
}
/**
* colorizer::tint()
*
* tint all non-trasparent pixels in the supplied images with color
* @since 8/10/2011
* @author Jesse Baird
<jebaird@gmail.com>
* @param mixed $tint
* @param mixed $opacity
* @param string $fileNamePrefix
* @return void
*/
public function tint($tint = 0000000, $opacity = 0, $fileNamePrefix = '') {
$tint = $this -> hexToRGB($tint);
foreach ($this->images as $filename => $img) {
imagefilter($img, IMG_FILTER_COLORIZE, $tint['r'], $tint['g'], $tint['b'], $opacity);
$this -> saveImage((($fileNamePrefix != '') ? $fileNamePrefix . '_' : '') . $filename, $img);
imagedestroy($img);
}
}
/**
* colorizer::replace()
*
* find all colors in images and replace them
*
* @param mixed $find
* @param mixed $replace
* @param string $fileNamePrefix
* @return void
*/
public function replace($find = 000000, $replace = 000000, $fileNamePrefix = '') {
$frgb = $rrgb = array();
if (is_array($find)) {
foreach ($find as $c) {
$frgb[] = $this -> hexToRGB($c);
}
} else {
$frgb[] = $this -> hexToRGB($find);
}
if (is_array($replace)) {
foreach ($replace as $c) {
$rrgb[] = $this -> hexToRGB($c);
}
} else {
$rrgb[] = $this -> hexToRGB($replace);
}
foreach ($this->images as $filename => $img) {
foreach ($frgb as $index => $f) {
//get the current replace color in find with the index or the last color in the array
$r = (isset($rrgb[$index])) ? $rrgb[$index] : $rrgb[(count($rrgb) - 1)];
$index = imagecolorclosest($img, $f['r'], $f['g'], $f['b']);
//find
imagecolorset($img, $index, $r['r'], $r['g'], $r['b']);
//replace
}
$this -> saveImage((($fileNamePrefix != '') ? $fileNamePrefix . '_' : '') . $filename, $img);
imagedestroy($img);
}
}
/**
* colorizer::hexToRGB()
*
* turn #333 to rgb code
*
* @param mixed $hex
* @return array
*/
private function hexToRGB($hex) {
$hex = str_replace("#", "", $hex);
$color = array();
if (strlen($hex) == 3) {
$color['r'] = hexdec(substr($hex, 0, 1));
$color['g'] = hexdec(substr($hex, 1, 1));
$color['b'] = hexdec(substr($hex, 2, 1));
} else if (strlen($hex) == 6) {
$color['r'] = hexdec(substr($hex, 0, 2));
$color['g'] = hexdec(substr($hex, 2, 2));
$color['b'] = hexdec(substr($hex, 4, 2));
}
return $color;
}
/**
* colorizer::getExtention()
*
* @param mixed $file
* @return string
*/
private function getExtention($file) {
$parts = pathinfo($file);
return strtolower($parts['extension']);
}
/**
* colorizer::createImage()
*
* @param mixed $filename
* @return iamge object
*/
private function createImage($filename) {
$ret = '';
switch($this->getExtention($filename)) {
case 'png' :
$ret = imagecreatefrompng($filename);
break;
case 'jpg' :
case 'jpeg' :
$ret = imagecreatefromjpeg($filename);
break;
case 'bmp' :
$ret = imagecreatefrombmp($filename);
break;
case 'gif' :
$ret = imagecreatefromgif($filename);
break;
default :
throw new Exception('unknown file extension');
break;
}
return $ret;
}
private function saveImage($filename, $img) {
switch($this->getExtention($filename)) {
case 'png' :
//save the alpha - prevent transparent px from turning black
imagealphablending($img, false);
imagesavealpha($img, true);
imagepng($img, self::CACHE_PATH . $filename);
break;
case 'jpg' :
case 'jpeg' :
imagejpeg($img, self::CACHE_PATH . $filename);
break;
case 'bmp' :
imgbmp($img, v::CACHE_PATH . $filename);
break;
case 'gif' :
imagegif($img, self::CACHE_PATH . $filename);
break;
default :
throw new Exception('unknown file extension');
break;
}
}
}
?>