/** * Won't work with soil-relative-urls */ private static function get_banner_tint($url) { $banner_tint = '#5c5d5f'; $thumb_request = wp_remote_head($url); if (is_wp_error($url)) { return $banner_tint; } $tonesque_exists = class_exists('Tonesque'); $is_404_error = 404 === $thumb_request['response']['code']; if (!$tonesque_exists || $is_404_error) { return $banner_tint; } $tonesque = new \Tonesque($thumb); return '#' . $tonesque->color('hex'); }
function wp_ajax_get_image_colors() { $attachment_id = filter_input(INPUT_POST, 'attachment_id'); $contrast = '255, 255, 255'; $color = '#000000'; if (intval($attachment_id) > 0 && attachment_exists($attachment_id)) { try { $tonesque = new \Tonesque(wp_get_attachment_url($attachment_id)); $contrast = $tonesque->contrast() ? $tonesque->contrast() : $contrast; $color = '#' . $tonesque->color('hex'); } catch (Exception $e) { // Disable tonesque } } echo json_encode(array($contrast, $color)); wp_die(); }