-
Notifications
You must be signed in to change notification settings - Fork 87
/
helper.php
181 lines (158 loc) · 4.67 KB
/
helper.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
<?php
/**
* Helper functions used for Image Placeholders.
*
* @package dominant-color-images
*
* @since 1.0.0
*/
/**
* Overloads wp_image_editors() to load the extended classes.
*
* @since 1.0.0
*
* @param string[] $editors Array of available image editor class names. Defaults are 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
* @return string[] Registered image editors class names.
*/
function dominant_color_set_image_editors( $editors ) {
if ( ! class_exists( 'Dominant_Color_Image_Editor_GD' ) ) {
require_once __DIR__ . '/class-dominant-color-image-editor-gd.php';
}
if ( ! class_exists( 'Dominant_Color_Image_Editor_Imagick' ) ) {
require_once __DIR__ . '/class-dominant-color-image-editor-imagick.php';
}
$replaces = array(
'WP_Image_Editor_GD' => 'Dominant_Color_Image_Editor_GD',
'WP_Image_Editor_Imagick' => 'Dominant_Color_Image_Editor_Imagick',
);
foreach ( $replaces as $old => $new ) {
$key = array_search( $old, $editors, true );
if ( false !== $key ) {
$editors[ $key ] = $new;
}
}
return $editors;
}
/**
* Computes the dominant color of the given attachment image and whether it has transparency.
*
* @since 1.0.0
*
* @access private
*
* @param int $attachment_id The attachment ID.
* @return array|WP_Error Array with the dominant color and has transparency values or WP_Error on error.
*/
function dominant_color_get_dominant_color_data( $attachment_id ) {
$mime_type = get_post_mime_type( $attachment_id );
if ( 'application/pdf' === $mime_type ) {
return new WP_Error( 'no_image_found', __( 'Unable to load image.', 'dominant-color-images' ) );
}
$file = dominant_color_get_attachment_file_path( $attachment_id );
if ( ! $file ) {
$file = get_attached_file( $attachment_id );
}
add_filter( 'wp_image_editors', 'dominant_color_set_image_editors' );
$editor = wp_get_image_editor(
$file,
array(
'methods' => array(
'get_dominant_color',
'has_transparency',
),
)
);
remove_filter( 'wp_image_editors', 'dominant_color_set_image_editors' );
if ( is_wp_error( $editor ) ) {
return $editor;
}
$has_transparency = $editor->has_transparency();
if ( is_wp_error( $has_transparency ) ) {
return $has_transparency;
}
$dominant_color_data['has_transparency'] = $has_transparency;
$dominant_color = $editor->get_dominant_color();
if ( is_wp_error( $dominant_color ) ) {
return $dominant_color;
}
$dominant_color_data['dominant_color'] = $dominant_color;
return $dominant_color_data;
}
/**
* Gets file path of image based on size.
*
* @since 1.0.0
*
* @param int $attachment_id Attachment ID for image.
* @param string $size Optional. Image size. Default 'medium'.
* @return false|string Path to an image or false if not found.
*/
function dominant_color_get_attachment_file_path( $attachment_id, $size = 'medium' ) {
$imagedata = wp_get_attachment_metadata( $attachment_id );
if ( ! is_array( $imagedata ) ) {
return false;
}
if ( ! isset( $imagedata['sizes'][ $size ] ) ) {
return false;
}
$file = get_attached_file( $attachment_id );
$filepath = str_replace( wp_basename( $file ), $imagedata['sizes'][ $size ]['file'], $file );
return $filepath;
}
/**
* Gets the dominant color for an image attachment.
*
* @since 1.0.0
*
* @param int $attachment_id Attachment ID for image.
* @return string|null Hex value of dominant color or null if not set.
*/
function dominant_color_get_dominant_color( $attachment_id ) {
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return null;
}
$image_meta = wp_get_attachment_metadata( $attachment_id );
if ( ! is_array( $image_meta ) ) {
return null;
}
if ( ! isset( $image_meta['dominant_color'] ) ) {
return null;
}
return $image_meta['dominant_color'];
}
/**
* Returns whether an image attachment has transparency.
*
* @since 1.0.0
*
* @param int $attachment_id Attachment ID for image.
* @return bool|null Whether the image has transparency, or null if not set.
*/
function dominant_color_has_transparency( $attachment_id ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
if ( ! is_array( $image_meta ) ) {
return null;
}
if ( ! isset( $image_meta['has_transparency'] ) ) {
return null;
}
return $image_meta['has_transparency'];
}
/**
* Gets hex color from RGB.
*
* @since 1.0.0
*
* @param int $red Red 0-255.
* @param int $green Green 0-255.
* @param int $blue Blue 0-255.
*
* @return string|null Hex color or null if error.
*/
function dominant_color_rgb_to_hex( $red, $green, $blue ) {
$range = range( 0, 255 );
if ( ! in_array( $red, $range, true ) || ! in_array( $green, $range, true ) || ! in_array( $blue, $range, true ) ) {
return null;
}
return sprintf( '%02x%02x%02x', $red, $green, $blue );
}