-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFIC-core.php
145 lines (110 loc) · 4.21 KB
/
FIC-core.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
<?php
/*
* load ColorExtractor library
*/
require_once 'ColorExtractor/Color.php' ;
require_once 'ColorExtractor/ColorExtractor.php' ;
require_once 'ColorExtractor/Palette.php' ;
use League\ColorExtractor\Color;
use League\ColorExtractor\ColorExtractor;
use League\ColorExtractor\Palette;
// helpter to transform int to hex
function fromIntToHex($int){
return Color::fromIntToHex($int);
}
// given a URL and a target number of palette colors, generate array palette
function getColors($url, $number = 5){
$palette = Palette::fromFilename($url);
$extractor = new ColorExtractor($palette);
$colorArrayInt = $extractor->extract($number);
$colorArray = array_map('fromIntToHex', $colorArrayInt);
return array_values($colorArray);
}
/*
* Define a few core functions
*/
// helper function to convert rgb array to hex
/*
function rgb2hex($rgb) {
$hex = "#";
$hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
$hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);
return $hex;
}
*/
// helper function to get all image attachments in WP that don't have a color set
function FIC_get_all_wp_attachments($only_get_unset_images = true){
$meta_query = false;
if ( $only_get_unset_images ){
$meta_query = array(
'key' => 'FIC_color',
'compare' => 'NOT EXISTS'
);
}
$accepted_mimes = array( 'image/jpeg', 'image/gif', 'image/png', 'image/bmp' );
$args = array(
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'attachment',
'post_mime_type' => $accepted_mimes,
'post_status' => 'any',
'fields' => 'ids'
);
if( $meta_query !== false ){
$args['meta_query'] = $meta_query;
}
return get_posts($args);
}
// Helper function to detect one single image
// $image can be either a server file path or an attachment ID
function FIC_detect_single_image($attachment_id, $file_path = null){
$imageURL = $file_path;
// if no path provided, attempt to create it
if ( !$imageURL ){
// get data from image
$image_data = wp_get_attachment_metadata( $attachment_id );
// no image data came back?
if ( !$image_data ){
$path = get_attached_file($attachment_id);
// attempt to create new attachment data, return false on failure
if ( ! $image_data = wp_generate_attachment_metadata($attachment_id, $path) ){
return false;
}
}
// make server file path from data
$thumb_path = path_join( dirname($image_data['file']), $image_data['sizes']['thumbnail']['file'] );
// get wp upload directory
$upload_dir = wp_upload_dir();
if ( $upload_dir['basedir'] )
$imageURL = trailingslashit($upload_dir['basedir']) . $thumb_path;
}
if ( !$imageURL || !file_exists($imageURL) ) return false;
try {
$colorArray = getColors($imageURL, 5);
$primary = isset($colorArray[0]) ? $colorArray[0] : false;
// save palette & primary as meta
if ( class_exists('ACF') ) {
update_field("primary_color", $primary, $attachment_id);
} else {
update_post_meta($attachment_id, 'FIC_palette', $colorArray);
update_post_meta($attachment_id, 'FIC_color', $primary);
}
$output = $colorArray;
} catch (Exception $e) {
$output = false;
}
// return true/false
return $output;
}
// Primary function to loop all images and set colors
function FIC_detect_all_images(){
// get all attachments without a color
$attachment_ids = FIC_get_all_wp_attachments();
// loop and set a color for each
foreach ($attachment_ids as $attachment_id)
FIC_detect_single_image($attachment_id);
return;
}
?>