Skip to content

Commit

Permalink
Filters editor images
Browse files Browse the repository at this point in the history
  • Loading branch information
paul de wouters committed Jul 2, 2015
1 parent 2296511 commit 510af47
Showing 1 changed file with 58 additions and 6 deletions.
64 changes: 58 additions & 6 deletions inc/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,43 @@ public function plugins_loaded() {
add_filter( 'wp_generate_attachment_metadata', array( $this, 'filter_wp_generate_attachment_metadata' ), 10, 2 );

add_filter( 'wp_get_attachment_image_attributes', array( $this, 'filter_wp_get_attachment_image_attributes' ), 10, 3 );

add_filter( 'image_send_to_editor', array( $this, 'filter_image_send_to_editor' ), 10, 8 );
}

/**
* Adds the style attribute to the image HTML.
*
* @param $html
* @param $id
* @param $caption
* @param $title
* @param $align
* @param $url
* @param $size
* @param $alt
*
* @return mixed
*/
public function filter_image_send_to_editor( $html, $id, $caption, $title, $align, $url, $size, $alt ) {

$colors_hex = $this->get_colors_for_attachment( $id );

if ( ! $colors_hex ) {
return $html;
}

$display_type = get_site_option( 'hmip_placeholder_type' );

if ( 'gradient' === $display_type ) {
$style = $this->get_gradient_style( $colors_hex );
} else {
$style = $this->get_solid_style( $colors_hex );
}

$html = preg_replace( '/<img/', '<img style="' . $style . '" ', $html );

return $html;
}

/**
Expand All @@ -54,7 +91,7 @@ public function plugins_loaded() {
*
* @return mixed
*/
function filter_wp_get_attachment_image_attributes( $attr, $attachment, $size ) {
public function filter_wp_get_attachment_image_attributes( $attr, $attachment, $size ) {

$colors_hex = $this->get_colors_for_attachment( $attachment->ID );

Expand All @@ -73,6 +110,13 @@ function filter_wp_get_attachment_image_attributes( $attr, $attachment, $size )
return $attr;
}

/**
* Style attribute for gradient background.
*
* @param $hex_colors
*
* @return string
*/
public function get_gradient_style( $hex_colors ) {

foreach ( $hex_colors as $hex ) {
Expand All @@ -84,14 +128,21 @@ public function get_gradient_style( $hex_colors ) {
$gradient_angles = array( '90', '0', '-90', '-180' );

foreach ( $gradient_angles as $key => $gradient_angle ) {
$gradients[] = sprintf( "linear-gradient(%sdeg, rgba(%s) 0%%, rgba(%s) 100%%, rgba(%s))", $gradient_angle, $colors[ $key ], $colors[ $key + 1 ], $colors[ $key + 1 ] );
$gradients[] = sprintf( "linear-gradient(%sdeg, rgba(%s) 0%%, rgba(%s) 100%%, rgba(%s) 100%%)", $gradient_angle, $colors[ $key ], $colors[ $key + 1 ], $colors[ $key + 1 ] );
}

$style = 'background:' . implode( $gradients, ', ' ) . ';';

return $style;
}

/**
* Style attribute for solid backgrounds.
*
* @param $colors
*
* @return string
*/
public function get_solid_style( $colors ) {

return 'background:' . reset( $colors ) . ';';
Expand Down Expand Up @@ -133,9 +184,9 @@ public function filter_wp_generate_attachment_metadata( $metadata, $attachment_i

/**
* Get the stored colors for the image
*
* @param $id
* @param array $hex_colors
*
* @return mixed
*/
public function get_colors_for_attachment( $id ) {

Expand All @@ -147,7 +198,7 @@ public function get_colors_for_attachment( $id ) {
* Extract the colors from the image
*
* @param $id
* @param $image_path
* @param array $colors
*/
public function save_colors_for_attachment( $id, $colors = array() ) {
update_post_meta( $id, 'hmgp_image_colors', $colors );
Expand All @@ -157,7 +208,8 @@ public function save_colors_for_attachment( $id, $colors = array() ) {
* Calculate the colors from the image
*
* @param $id
* @param $image_path
*
* @return array|void
*/
public function calculate_colors_for_attachment( $id ) {

Expand Down

0 comments on commit 510af47

Please sign in to comment.