gd-duotoner
Code (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
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
<?php
function duotone(&$img, $hex){
//strip #
$hex = ltrim($hex, '#');
//shift RGB values
$dec = hexdec($hex);
$r = $dec >> 16 & 0xFF;
$g = $dec >> 8 & 0xFF;
$b = $dec & 0xFF;
//dimensions:
$w = imagesx($img);
$h = imagesy($img);
//new resource
$new = imagecreatetruecolor($w, $h);
//center color
$c = ($r+$g+$b)/3;
//container
$t = array();
//create pallet
$i =0;
while($i < 256){
$nr = ($i <= $c) ? (($i / $c) * $r) : (255 - ((255 - $i) / (255 - $c) * (255 - $r)));
$ng = ($i <= $c) ? (($i / $c) * $g) : (255 - ((255 - $i) / (255 - $c) * (255 - $g)));
$nb = ($i <= $c) ? (($i / $c) * $b) : (255 - ((255 - $i) / (255 - $c) * (255 - $b)));
$t[$i] = imagecolorallocate($new, $nr, $ng, $nb);
$i++;
}
//pixel dance, loop all pixels and change them
$x=0;
while($x < $w){
$y = 0;
while($y < $h){
$rgb = imagecolorat($img, $x, $y);
//$a = ($rgb >> 24) << 1; WRONG!
$a = $rgb >> 24 & 0x7F;
$r = $rgb >> 16 & 0xFF;
$g = $rgb >> 8 & 0xFF;
$b = $rgb & 0xFF;
$bw = ($r+$g+$b)/3;
imagesetpixel($new, $x, $y, $t[$bw]);
$y++;
}
$x++;
}
//copy new
$img = $new;
//free memory
//imagedestroy will not work because of reference.
//unset is faster anyway too!
unset($new);
}
/************ DEBUG ******************/
ini_set('display_errors', true);
ini_set('error_reporting', 8191);
/*********** EXAMPLE(S) ******************/
$jpg = imagecreatefromjpeg('0.jpg');
duotone($jpg, '#704214'); //sepia
//duotone($jpg, '#7F7F7F'); // grey
//duotone($jpg, '#336699'); // blue
//duotone($jpg, '#9900CC'); // purple
//duotone($jpg, '#7FFF00'); // lime
if(!headers_sent()){
header('Cache-control: private');
header('Content-type: image/jpeg');
header('Content-Disposition: filename="'.basename(__FILE__).'.jpg"');
imagejpeg($jpg, NULL, 100);
exit;
}
?>
function duotone(&$img, $hex){
//strip #
$hex = ltrim($hex, '#');
//shift RGB values
$dec = hexdec($hex);
$r = $dec >> 16 & 0xFF;
$g = $dec >> 8 & 0xFF;
$b = $dec & 0xFF;
//dimensions:
$w = imagesx($img);
$h = imagesy($img);
//new resource
$new = imagecreatetruecolor($w, $h);
//center color
$c = ($r+$g+$b)/3;
//container
$t = array();
//create pallet
$i =0;
while($i < 256){
$nr = ($i <= $c) ? (($i / $c) * $r) : (255 - ((255 - $i) / (255 - $c) * (255 - $r)));
$ng = ($i <= $c) ? (($i / $c) * $g) : (255 - ((255 - $i) / (255 - $c) * (255 - $g)));
$nb = ($i <= $c) ? (($i / $c) * $b) : (255 - ((255 - $i) / (255 - $c) * (255 - $b)));
$t[$i] = imagecolorallocate($new, $nr, $ng, $nb);
$i++;
}
//pixel dance, loop all pixels and change them
$x=0;
while($x < $w){
$y = 0;
while($y < $h){
$rgb = imagecolorat($img, $x, $y);
//$a = ($rgb >> 24) << 1; WRONG!
$a = $rgb >> 24 & 0x7F;
$r = $rgb >> 16 & 0xFF;
$g = $rgb >> 8 & 0xFF;
$b = $rgb & 0xFF;
$bw = ($r+$g+$b)/3;
imagesetpixel($new, $x, $y, $t[$bw]);
$y++;
}
$x++;
}
//copy new
$img = $new;
//free memory
//imagedestroy will not work because of reference.
//unset is faster anyway too!
unset($new);
}
/************ DEBUG ******************/
ini_set('display_errors', true);
ini_set('error_reporting', 8191);
/*********** EXAMPLE(S) ******************/
$jpg = imagecreatefromjpeg('0.jpg');
duotone($jpg, '#704214'); //sepia
//duotone($jpg, '#7F7F7F'); // grey
//duotone($jpg, '#336699'); // blue
//duotone($jpg, '#9900CC'); // purple
//duotone($jpg, '#7FFF00'); // lime
if(!headers_sent()){
header('Cache-control: private');
header('Content-type: image/jpeg');
header('Content-Disposition: filename="'.basename(__FILE__).'.jpg"');
imagejpeg($jpg, NULL, 100);
exit;
}
?>