This repository has been archived by the owner on Oct 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwpthumb.background-fill.php
146 lines (98 loc) · 4.63 KB
/
wpthumb.background-fill.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
<?php
class WP_Thumb_Background_Fill {
private $args = array();
private $editor;
public function __construct( $editor, $args ) {
$this->editor = $editor;
$defaults = array(
'background_fill' => false,
);
$this->args = wp_parse_args( $args, $defaults );
if ( $this->args['background_fill'] && $this->args['background_fill'] !== 'auto' ) {
$this->fill_with_color( $this->args['background_fill'] );
} else if ( $this->args['background_fill'] && $this->args['background_fill'] === 'auto' ) {
if ( $color = $this->get_background_color() ) {
$this->fill_with_color( $color );
}
}
}
/**
* Background fill an image using the provided color
*/
public function fill_with_color( $color ) {
if ( ! is_array( $color ) && strlen( $color ) == 3 ) {
$color = (float) str_pad( (string) $color, 9, $color ) . '000';
}
if ( ! is_array( $color ) ) {
$color = array( 'top' => $color, 'bottom' => $color, 'left' => $color, 'right' => $color );
}
$this->fill_color( $color );
}
/**
* Background fill an image using the provided color
*
* @param int $width The desired width of the new image
* @param int $height The desired height of the new image
* @param Array $colors The desired pad colors in RGB format, array should be array( 'top' => '', 'right' => '', 'bottom' => '', 'left' => '' );
*/
private function fill_color( array $colors ) {
$current_size = $this->editor->get_size();
$size = array( 'width' => $this->args['width'], 'height' => $this->args['height'] );
$offsetLeft = ( $size['width'] - $current_size['width'] ) / 2;
$offsetTop = ( $size['height'] - $current_size['height'] ) / 2;
$new_image = imagecreatetruecolor( $size['width'], $size['height'] );
// This is needed to support alpha
imagesavealpha( $new_image, true );
imagealphablending( $new_image, false );
// Check if we are padding vertically or horizontally
if ( $current_size['width'] != $size['width'] ) {
$colorToPaint = imagecolorallocatealpha( $new_image, substr( $colors['left'], 0, 3 ), substr( $colors['left'], 3, 3 ), substr( $colors['left'], 6, 3 ), substr( $colors['left'], 9, 3 ) );
// Fill left color
imagefilledrectangle( $new_image, 0, 0, $offsetLeft + 5, $size['height'], $colorToPaint );
$colorToPaint = imagecolorallocatealpha( $new_image, substr( $colors['right'], 0, 3 ), substr( $colors['right'], 3, 3 ), substr( $colors['right'], 6, 3 ), substr( $colors['left'], 9, 3 ) );
// Fill right color
imagefilledrectangle( $new_image, $offsetLeft + $current_size['width'] - 5, 0, $size['width'], $size['height'], $colorToPaint );
}
if ( $current_size['height'] != $size['height'] ) {
$colorToPaint = imagecolorallocatealpha( $new_image, substr( $colors['top'], 0, 3 ), substr( $colors['top'], 3, 3 ), substr( $colors['top'], 6, 3 ), substr( $colors['left'], 9, 3 ) );
// Fill top color
imagefilledrectangle( $new_image, 0, 0, $size['width'], $offsetTop + 5, $colorToPaint );
$colorToPaint = imagecolorallocatealpha( $new_image, substr( $colors['bottom'], 0, 3 ), substr( $colors['bottom'], 3, 3 ), substr( $colors['bottom'], 6, 3 ), substr( $colors['left'], 9, 3 ) );
// Fill bottom color
imagefilledrectangle( $new_image, 0, $offsetTop - 5 + $current_size['height'], $size['width'], $size['height'], $colorToPaint );
}
imagecopy( $new_image, $this->editor->get_image(), $offsetLeft, $offsetTop, 0, 0, $current_size['width'], $current_size['height'] );
$this->editor->update_image( $new_image );
$this->editor->update_size();
}
public function get_background_color() {
$current_size = $this->editor->get_size();
$coords = array(
array( 0, 0 ),
array( $current_size['width'] - 1, 0 ),
array( $current_size['width'] - 1, $current_size['height'] - 1 ),
array( 0, $current_size['height'] - 1 ),
);
$colors = array();
$color = 0;
foreach ( $coords as $coord ) {
$rgb = imagecolorat( $this->editor->get_image(), $coord[0], $coord[1] );
$c = imagecolorsforindex( $this->editor->get_image(), $rgb );
$colors[] = $c['red'] + $c['green'] + $c['blue'] + $c['alpha'];
$color = str_pad( $c['red'], 3, '0', STR_PAD_LEFT ) . str_pad( $c['green'], 3, '0', STR_PAD_LEFT ) . str_pad( $c['blue'], 3, '0', STR_PAD_LEFT ) . str_pad( $c['alpha'], 3, '0', STR_PAD_LEFT );
}
if ( max( $colors ) > min( $colors ) + 15 ) {
return false;
}
return $color;
}
}
function wpthumb_background_fill( $editor, $args ) {
// currently only supports GD
if ( ! is_a( $editor, 'WP_Thumb_Image_Editor_GD' ) ) {
return $editor;
}
$bg = new WP_Thumb_Background_Fill( $editor, $args );
return $editor;
}
add_filter( 'wpthumb_image_post', 'wpthumb_background_fill', 10, 2 );